From 9dfd967a32441c9392a74b4018a38512933fff10 Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 17 Oct 2022 13:29:21 +0700 Subject: [PATCH] implement downloads filter per channel --- .../home/templates/home/channel_id.html | 3 + .../home/templates/home/channel_id_about.html | 3 + .../templates/home/channel_id_playlist.html | 3 + .../home/templates/home/downloads.html | 4 +- tubearchivist/home/views.py | 60 +++++++++++++++---- 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/tubearchivist/home/templates/home/channel_id.html b/tubearchivist/home/templates/home/channel_id.html index 1e5b1b42..17d26db9 100644 --- a/tubearchivist/home/templates/home/channel_id.html +++ b/tubearchivist/home/templates/home/channel_id.html @@ -10,6 +10,9 @@

Videos

Playlists

About

+ {% if has_pending %} +

Downloads

+ {% endif %}
diff --git a/tubearchivist/home/templates/home/channel_id_about.html b/tubearchivist/home/templates/home/channel_id_about.html index d3bc9b69..5c6ac892 100644 --- a/tubearchivist/home/templates/home/channel_id_about.html +++ b/tubearchivist/home/templates/home/channel_id_about.html @@ -10,6 +10,9 @@

Videos

Playlists

About

+ {% if has_pending %} +

Downloads

+ {% endif %}
diff --git a/tubearchivist/home/templates/home/channel_id_playlist.html b/tubearchivist/home/templates/home/channel_id_playlist.html index b7f5891c..9fd51b58 100644 --- a/tubearchivist/home/templates/home/channel_id_playlist.html +++ b/tubearchivist/home/templates/home/channel_id_playlist.html @@ -10,6 +10,9 @@

Videos

Playlists

About

+ {% if has_pending %} +

Downloads

+ {% endif %}
diff --git a/tubearchivist/home/templates/home/downloads.html b/tubearchivist/home/templates/home/downloads.html index b3a3a706..85d61e7c 100644 --- a/tubearchivist/home/templates/home/downloads.html +++ b/tubearchivist/home/templates/home/downloads.html @@ -3,7 +3,7 @@ {% block content %}
-

Downloads

+

Downloads {% if channel_filter_id %} for {{ channel_filter_name }}{% endif %}

@@ -55,7 +55,7 @@ list view
-

Total videos: {{ max_hits }}{% if max_hits == 10000 %}+{% endif %}

+

Total videos: {{ max_hits }}{% if max_hits == 10000 %}+{% endif %} {% if channel_filter_id %} - from channel {{ channel_filter_name }}{% endif %}

diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 8533585b..29ee565c 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -31,7 +31,7 @@ from home.src.frontend.forms import ( UserSettingsForm, ) from home.src.frontend.searching import SearchHandler -from home.src.index.channel import channel_overwrites +from home.src.index.channel import YoutubeChannel, channel_overwrites from home.src.index.generic import Pagination from home.src.index.playlist import YoutubePlaylist from home.src.ta.config import AppConfig, ScheduleBuilder @@ -387,6 +387,15 @@ class DownloadView(ArchivistResultsView): {"term": {"channel_id": {"value": channel_filter}}} ) + channel = YoutubeChannel(channel_filter) + channel.get_from_es() + self.context.update( + { + "channel_filter_id": channel_filter, + "channel_filter_name": channel.json_data["channel_name"], + } + ) + self.data.update( { "query": {"bool": {"must": must_list}}, @@ -423,7 +432,37 @@ class DownloadView(ArchivistResultsView): return redirect("downloads", permanent=True) -class ChannelIdView(ArchivistResultsView): +class ChannelIdBaseView(ArchivistResultsView): + """base class for all channel-id views""" + + def get_channel_meta(self, channel_id): + """get metadata for channel""" + path = f"ta_channel/_doc/{channel_id}" + response, _ = ElasticWrap(path).get() + channel_info = SearchProcess(response).process() + + return channel_info + + def channel_has_pending(self, channel_id): + """check if channel has pending videos in queue""" + path = "ta_download/_search" + data = { + "size": 1, + "query": { + "bool": { + "must": [ + {"term": {"status": {"value": "pending"}}}, + {"term": {"channel_id": {"value": channel_id}}}, + ] + } + }, + } + response, _ = ElasticWrap(path).get(data=data) + + self.context.update({"has_pending": bool(response["hits"]["hits"])}) + + +class ChannelIdView(ChannelIdBaseView): """resolves to /channel// display single channel page from channel_id """ @@ -437,6 +476,7 @@ class ChannelIdView(ArchivistResultsView): self._update_view_data(channel_id) self.find_results() self.match_progress() + self.channel_has_pending(channel_id) if self.context["results"]: channel_info = self.context["results"][0]["source"]["channel"] @@ -487,7 +527,7 @@ class ChannelIdView(ArchivistResultsView): return redirect("channel_id", channel_id, permanent=True) -class ChannelIdAboutView(ArchivistResultsView): +class ChannelIdAboutView(ChannelIdBaseView): """resolves to /channel//about/ show metadata, handle per channel conf """ @@ -497,6 +537,7 @@ class ChannelIdAboutView(ArchivistResultsView): def get(self, request, channel_id): """handle get request""" self.initiate_vars(request) + self.channel_has_pending(channel_id) path = f"ta_channel/_doc/{channel_id}" response, _ = ElasticWrap(path).get() @@ -530,7 +571,7 @@ class ChannelIdAboutView(ArchivistResultsView): return redirect("channel_id_about", channel_id, permanent=True) -class ChannelIdPlaylistView(ArchivistResultsView): +class ChannelIdPlaylistView(ChannelIdBaseView): """resolves to /channel//playlist/ show all playlists of channel """ @@ -543,8 +584,9 @@ class ChannelIdPlaylistView(ArchivistResultsView): self.initiate_vars(request) self._update_view_data(channel_id) self.find_results() + self.channel_has_pending(channel_id) - channel_info = self._get_channel_meta(channel_id) + channel_info = self.get_channel_meta(channel_id) channel_name = channel_info["channel_name"] self.context.update( { @@ -565,14 +607,6 @@ class ChannelIdPlaylistView(ArchivistResultsView): self.data["query"] = {"bool": {"must": must_list}} - def _get_channel_meta(self, channel_id): - """get metadata for channel""" - path = f"ta_channel/_doc/{channel_id}" - response, _ = ElasticWrap(path).get() - channel_info = SearchProcess(response).process() - - return channel_info - class ChannelView(ArchivistResultsView): """resolves to /channel/