add mark_playlist_watched and refactor mark_channel_watched to update_by_query

This commit is contained in:
simon 2021-11-20 10:58:25 +07:00
parent 85d12d6c68
commit 8d708fcdc8
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 60 additions and 29 deletions

View File

@ -654,6 +654,8 @@ class WatchState:
self.mark_vid_watched() self.mark_vid_watched()
elif url_type == "channel": elif url_type == "channel":
self.mark_channel_watched() self.mark_channel_watched()
elif url_type == "playlist":
self.mark_playlist_watched()
print(f"marked {self.youtube_id} as watched") print(f"marked {self.youtube_id} as watched")
@ -662,13 +664,12 @@ class WatchState:
url_type = self.dedect_type() url_type = self.dedect_type()
if url_type == "video": if url_type == "video":
self.mark_vid_watched(revert=True) self.mark_vid_watched(revert=True)
elif url_type == "channel":
self.mark_channel_watched(revert=True)
print(f"revert {self.youtube_id} as unwatched") print(f"revert {self.youtube_id} as unwatched")
def dedect_type(self): def dedect_type(self):
"""find youtube id type""" """find youtube id type"""
print(self.youtube_id)
url_process = UrlListParser(self.youtube_id).process_list() url_process = UrlListParser(self.youtube_id).process_list()
url_type = url_process[0]["type"] url_type = url_process[0]["type"]
return url_type return url_type
@ -689,41 +690,60 @@ class WatchState:
if not request.ok: if not request.ok:
print(request.text) print(request.text)
def mark_channel_watched(self, revert=False): def mark_channel_watched(self):
"""change watched status of every video in channel""" """change watched status of every video in channel"""
es_url = self.ES_URL
headers = self.HEADERS
youtube_id = self.youtube_id
# create pipeline
data = { data = {
"description": youtube_id, "query": {
"processors": [ "bool": {
{"set": {"field": "player.watched", "value": True}}, "must": [
{"set": {"field": "player.watched_date", "value": self.stamp}}, {
], "term": {
"channel.channel_id": {
"value": self.youtube_id
}
}
},
{"term": {"player.watched": {"value": False}}},
]
}
},
"script": {
"source": "ctx._source.player['watched'] = true",
"lang": "painless",
},
} }
if revert:
data["processors"][0]["set"]["value"] = False
payload = json.dumps(data) payload = json.dumps(data)
url = f"{es_url}/_ingest/pipeline/{youtube_id}" url = f"{self.ES_URL}/ta_video/_update_by_query"
request = requests.put( request = requests.post(
url, data=payload, headers=headers, auth=self.ES_AUTH url, data=payload, headers=self.HEADERS, auth=self.ES_AUTH
) )
if not request.ok: if not request.ok:
print(request.text) print(request.text)
raise ValueError("failed to post ingest pipeline")
# apply pipeline def mark_playlist_watched(self):
must_list = [ """change watched state of all videos in playlist"""
{"term": {"channel.channel_id": {"value": youtube_id}}}, data = {
{"term": {"player.watched": {"value": False}}}, "query": {
] "bool": {
data = {"query": {"bool": {"must": must_list}}} "must": [
{
"term": {
"playlist.keyword": {"value": self.youtube_id}
}
},
{"term": {"player.watched": {"value": False}}},
]
}
},
"script": {
"source": "ctx._source.player['watched'] = true",
"lang": "painless",
},
}
payload = json.dumps(data) payload = json.dumps(data)
url = f"{es_url}/ta_video/_update_by_query?pipeline={youtube_id}" url = f"{self.ES_URL}/ta_video/_update_by_query"
request = requests.post( request = requests.post(
url, data=payload, headers=headers, auth=self.ES_AUTH url, data=payload, headers=self.HEADERS, auth=self.ES_AUTH
) )
if not request.ok: if not request.ok:
print(request.text) print(request.text)

View File

@ -47,7 +47,7 @@
{% endif %} {% endif %}
{% if max_hits %} {% if max_hits %}
<p>Total Videos archived: {{ max_hits }}</p> <p>Total Videos archived: {{ max_hits }}</p>
<p>Watched: <button title="Mark all videos from {{ channel_info.channel_name }} as watched" type="button" id="{{ channel_info.channel_id }}" onclick="isWatched(this.id)">Mark as watched</button></p> <p>Watched: <button title="Mark all videos from {{ channel_info.channel_name }} as watched" type="button" id="watched-button" data-id="{{ channel_info.channel_id }}" onclick="isWatchedButton(this)">Mark as watched</button></p>
{% endif %} {% endif %}
<button title="Search for playlists on YouTube for {{ channel_info.channel_name }}" type="button" id="find-playlists-button" data-id="{{ channel_info.channel_id }}" onclick="findPlaylists(this)">Find Playlists</button> <button title="Search for playlists on YouTube for {{ channel_info.channel_name }}" type="button" id="find-playlists-button" data-id="{{ channel_info.channel_id }}" onclick="findPlaylists(this)">Find Playlists</button>
<a href="/playlist/?search={{ channel_info.channel_id }}" title="Show all playlists belonging to {{ channel_info.channel_name }}"><button>Show Playlists</button></a> <a href="/playlist/?search={{ channel_info.channel_id }}" title="Show all playlists belonging to {{ channel_info.channel_name }}"><button>Show Playlists</button></a>

View File

@ -37,7 +37,7 @@
<div> <div>
{% if max_hits %} {% if max_hits %}
<p>Total Videos archived: {{ playlist_info.playlist_entries|length }}/{{ max_hits }}</p> <p>Total Videos archived: {{ playlist_info.playlist_entries|length }}/{{ max_hits }}</p>
<p>Watched: <button title="Mark all videos from {{ playlist_info.playlist_name }} as watched" type="button" id="{{ playlist_info.playlist_id }}" onclick="isWatched(this.id)">Mark as watched</button></p> <p>Watched: <button title="Mark all videos from {{ playlist_info.playlist_name }} as watched" type="button" id="watched-button" data-id="{{ playlist_info.playlist_id }}" onclick="isWatchedButton(this)">Mark as watched</button></p>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View File

@ -21,6 +21,17 @@ function isWatched(youtube_id) {
document.getElementById(youtube_id).replaceWith(seenIcon); document.getElementById(youtube_id).replaceWith(seenIcon);
} }
function isWatchedButton(button) {
youtube_id = button.getAttribute("data-id");
var payload = JSON.stringify({'watched': youtube_id});
button.remove();
sendPost(payload);
setTimeout(function(){
location.reload();
return false;
}, 1000);
}
function isUnwatched(youtube_id) { function isUnwatched(youtube_id) {
var payload = JSON.stringify({'un_watched': youtube_id}); var payload = JSON.stringify({'un_watched': youtube_id});
sendPost(payload); sendPost(payload);