From fc99a2b532d051e2da07b57df0f1f213d80a5181 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 3 Aug 2024 00:51:02 +0200 Subject: [PATCH] add channel nav --- tubearchivist/channel/src/nav.py | 78 ++++++++++++++++++++++++++++++++ tubearchivist/channel/urls.py | 5 ++ tubearchivist/channel/views.py | 13 ++++++ 3 files changed, 96 insertions(+) create mode 100644 tubearchivist/channel/src/nav.py diff --git a/tubearchivist/channel/src/nav.py b/tubearchivist/channel/src/nav.py new file mode 100644 index 00000000..6ff3e047 --- /dev/null +++ b/tubearchivist/channel/src/nav.py @@ -0,0 +1,78 @@ +"""build channel nav""" + +from common.src.es_connect import ElasticWrap + + +class ChannelNav: + """get all nav items""" + + def __init__(self, channel_id): + self.channel_id = channel_id + + def get_nav(self): + """build nav items""" + nav = { + "has_pending": self._get_has_pending(), + "has_playlists": self._get_has_playlists(), + } + nav.update(self._get_vid_types()) + return nav + + def _get_vid_types(self): + """get available vid_types in given channel""" + data = { + "size": 0, + "query": { + "term": {"channel.channel_id": {"value": self.channel_id}} + }, + "aggs": {"unique_values": {"terms": {"field": "vid_type"}}}, + } + response, _ = ElasticWrap("ta_video/_search").get(data) + buckets = response["aggregations"]["unique_values"]["buckets"] + + type_nav = { + "has_videos": False, + "has_streams": False, + "has_shorts": False, + } + for bucket in buckets: + if bucket["key"] == "videos": + type_nav["has_videos"] = True + if bucket["key"] == "streams": + type_nav["has_streams"] = True + if bucket["key"] == "shorts": + type_nav["has_shorts"] = True + + return type_nav + + def _get_has_pending(self): + """check if has pending videos in download queue""" + data = { + "size": 1, + "query": { + "bool": { + "must": [ + {"term": {"status": {"value": "pending"}}}, + {"term": {"channel_id": {"value": self.channel_id}}}, + ] + } + }, + "_source": False, + } + response, _ = ElasticWrap("ta_download/_search").get(data=data) + + return bool(response["hits"]["hits"]) + + def _get_has_playlists(self): + """check if channel has playlists""" + path = "ta_playlist/_search" + data = { + "size": 1, + "query": { + "term": {"playlist_channel_id": {"value": self.channel_id}} + }, + "_source": False, + } + response, _ = ElasticWrap(path).get(data=data) + + return bool(response["hits"]["hits"]) diff --git a/tubearchivist/channel/urls.py b/tubearchivist/channel/urls.py index 9b51671c..4b4ec53e 100644 --- a/tubearchivist/channel/urls.py +++ b/tubearchivist/channel/urls.py @@ -24,4 +24,9 @@ urlpatterns = [ views.ChannelAggsApiView.as_view(), name="api-channel-aggs", ), + path( + "/nav/", + views.ChannelNavApiView.as_view(), + name="api-channel-nav", + ), ] diff --git a/tubearchivist/channel/views.py b/tubearchivist/channel/views.py index 5b28267f..e5c7c8cd 100644 --- a/tubearchivist/channel/views.py +++ b/tubearchivist/channel/views.py @@ -1,6 +1,7 @@ """all channel API views""" from channel.src.index import YoutubeChannel +from channel.src.nav import ChannelNav from common.src.urlparser import Parser from common.views_base import AdminWriteOnly, ApiBaseView from download.src.subscriptions import ChannelSubscription @@ -127,6 +128,18 @@ class ChannelAggsApiView(ApiBaseView): return Response(self.response) +class ChannelNavApiView(ApiBaseView): + """resolves to /api/channel//nav/ + GET: get channel nav + """ + + def get(self, request, channel_id): + """get nav""" + + nav = ChannelNav(channel_id).get_nav() + return Response(nav) + + class ChannelApiSearchView(ApiBaseView): """resolves to /api/channel/search/ search for channel