mirror of
https://git.vectorsigma.ru/public/tubearchivist.git
synced 2026-08-05 00:09:18 +00:00
match video progress through API
This commit is contained in:
@@ -8,15 +8,17 @@ import urllib.parse
|
|||||||
|
|
||||||
from common.src.env_settings import EnvironmentSettings
|
from common.src.env_settings import EnvironmentSettings
|
||||||
from common.src.helper import date_parser, get_duration_str
|
from common.src.helper import date_parser, get_duration_str
|
||||||
|
from common.src.ta_redis import RedisArchivist
|
||||||
from download.src.thumbnails import ThumbManager
|
from download.src.thumbnails import ThumbManager
|
||||||
|
|
||||||
|
|
||||||
class SearchProcess:
|
class SearchProcess:
|
||||||
"""process search results"""
|
"""process search results"""
|
||||||
|
|
||||||
def __init__(self, response):
|
def __init__(self, response, match_video_user_progress: None | int = None):
|
||||||
self.response = response
|
self.response = response
|
||||||
self.processed = False
|
self.processed = False
|
||||||
|
self.position_index = self.get_user_progress(match_video_user_progress)
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
"""detect type and process"""
|
"""detect type and process"""
|
||||||
@@ -33,6 +35,19 @@ class SearchProcess:
|
|||||||
|
|
||||||
return self.processed
|
return self.processed
|
||||||
|
|
||||||
|
def get_user_progress(self, match_video_user_progress) -> dict | None:
|
||||||
|
"""get user video watch progress"""
|
||||||
|
if not match_video_user_progress:
|
||||||
|
return None
|
||||||
|
|
||||||
|
query = f"{match_video_user_progress}:progress:*"
|
||||||
|
all_positions = RedisArchivist().list_items(query)
|
||||||
|
if not all_positions:
|
||||||
|
return None
|
||||||
|
|
||||||
|
pos_index = {i["youtube_id"]: i["position"] for i in all_positions}
|
||||||
|
return pos_index
|
||||||
|
|
||||||
def _process_result(self, result):
|
def _process_result(self, result):
|
||||||
"""detect which type of data to process"""
|
"""detect which type of data to process"""
|
||||||
index = result["_index"]
|
index = result["_index"]
|
||||||
@@ -105,6 +120,13 @@ class SearchProcess:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.position_index:
|
||||||
|
player_position = self.position_index.get(video_id)
|
||||||
|
total = video_dict["player"].get("duration")
|
||||||
|
if player_position and total:
|
||||||
|
progress = 100 * (player_position / total)
|
||||||
|
video_dict["player"].update({"progress": progress})
|
||||||
|
|
||||||
return dict(sorted(video_dict.items()))
|
return dict(sorted(video_dict.items()))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -57,12 +57,14 @@ class ApiBaseView(APIView):
|
|||||||
self.context = False
|
self.context = False
|
||||||
self.pagination_handler = False
|
self.pagination_handler = False
|
||||||
|
|
||||||
def get_document(self, document_id):
|
def get_document(self, document_id, progress_match=None):
|
||||||
"""get single document from es"""
|
"""get single document from es"""
|
||||||
path = f"{self.search_base}{document_id}"
|
path = f"{self.search_base}{document_id}"
|
||||||
response, status_code = ElasticWrap(path).get()
|
response, status_code = ElasticWrap(path).get()
|
||||||
try:
|
try:
|
||||||
self.response["data"] = SearchProcess(response).process()
|
self.response["data"] = SearchProcess(
|
||||||
|
response, match_video_user_progress=progress_match
|
||||||
|
).process()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print(f"item not found: {document_id}")
|
print(f"item not found: {document_id}")
|
||||||
self.response["data"] = False
|
self.response["data"] = False
|
||||||
@@ -78,14 +80,16 @@ class ApiBaseView(APIView):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_document_list(self, request, pagination=True):
|
def get_document_list(self, request, pagination=True, progress_match=None):
|
||||||
"""get a list of results"""
|
"""get a list of results"""
|
||||||
if pagination:
|
if pagination:
|
||||||
self.initiate_pagination(request)
|
self.initiate_pagination(request)
|
||||||
|
|
||||||
es_handler = ElasticWrap(self.search_base)
|
es_handler = ElasticWrap(self.search_base)
|
||||||
response, status_code = es_handler.get(data=self.data)
|
response, status_code = es_handler.get(data=self.data)
|
||||||
self.response["data"] = SearchProcess(response).process()
|
self.response["data"] = SearchProcess(
|
||||||
|
response, match_video_user_progress=progress_match
|
||||||
|
).process()
|
||||||
if self.response["data"]:
|
if self.response["data"]:
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class VideoApiListView(ApiBaseView):
|
|||||||
return Response({"error": str(err)}, status=400)
|
return Response({"error": str(err)}, status=400)
|
||||||
|
|
||||||
self.data = data
|
self.data = data
|
||||||
self.get_document_list(request)
|
self.get_document_list(request, progress_match=request.user.id)
|
||||||
|
|
||||||
return Response(self.response)
|
return Response(self.response)
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ class VideoApiView(ApiBaseView):
|
|||||||
def get(self, request, video_id):
|
def get(self, request, video_id):
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
"""get request"""
|
"""get request"""
|
||||||
self.get_document(video_id)
|
self.get_document(video_id, progress_match=request.user.id)
|
||||||
return Response(self.response, status=self.status_code)
|
return Response(self.response, status=self.status_code)
|
||||||
|
|
||||||
def delete(self, request, video_id):
|
def delete(self, request, video_id):
|
||||||
|
|||||||
Reference in New Issue
Block a user