add channel nav

This commit is contained in:
Simon
2024-08-03 00:51:02 +02:00
parent 38d74657ec
commit fc99a2b532
3 changed files with 96 additions and 0 deletions

View File

@@ -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"])

View File

@@ -24,4 +24,9 @@ urlpatterns = [
views.ChannelAggsApiView.as_view(),
name="api-channel-aggs",
),
path(
"<slug:channel_id>/nav/",
views.ChannelNavApiView.as_view(),
name="api-channel-nav",
),
]

View File

@@ -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/<channel_id>/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