diff --git a/backend/common/src/watched.py b/backend/common/src/watched.py index 5cdef464..27e135c2 100644 --- a/backend/common/src/watched.py +++ b/backend/common/src/watched.py @@ -28,6 +28,11 @@ class WatchState: self.change_vid_state() return + if url_type == "channel": + self.reset_channel_progress() + if url_type == "playlist": + self.reset_playlist_progress() + self._add_pipeline() path = f"ta_video/_update_by_query?pipeline=watch_{self.youtube_id}" data = self._build_update_data(url_type) @@ -53,6 +58,31 @@ class WatchState: print(response) raise ValueError("failed to mark video as watched") + def reset_channel_progress(self): + """reset channel progress positions""" + from channel.src.index import YoutubeChannel + + videos = YoutubeChannel(self.youtube_id).get_channel_videos() + video_ids = [i["youtube_id"] for i in videos] + self._reset_list(video_ids) + + def reset_playlist_progress(self): + """reset playlist progress positions""" + from playlist.src.index import YoutubePlaylist + + videos = YoutubePlaylist(self.youtube_id).get_playlist_videos() + video_ids = [i["youtube_id"] for i in videos] + self._reset_list(video_ids) + + def _reset_list(self, video_ids: list[str]): + """reset list of video ids""" + redis_con = RedisArchivist() + all_ids = redis_con.list_keys(f"{self.user_id}:progress") + for progress_id in all_ids: + video_id = progress_id.split(":")[-1] + if video_id in video_ids: + redis_con.del_message(progress_id) + def _build_update_data(self, url_type): """build update by query data based on url_type""" term_key_map = { diff --git a/backend/playlist/src/index.py b/backend/playlist/src/index.py index 493ad6ac..d0d938d3 100644 --- a/backend/playlist/src/index.py +++ b/backend/playlist/src/index.py @@ -93,6 +93,18 @@ class YoutubePlaylist(YouTubeItem): channel_handler = YoutubeChannel(channel_id) channel_handler.build_json(upload=True) + def get_playlist_videos(self): + """get all playlist videos""" + data = { + "query": { + "term": {"playlist.keyword": {"value": self.youtube_id}} + }, + "_source": ["youtube_id"], + } + result = IndexPaginate("ta_video", data).get_results() + + return result + def get_local_vids(self) -> list[str]: """get local video ids from youtube entries""" entries = self.youtube_meta["entries"] @@ -158,13 +170,7 @@ class YoutubePlaylist(YouTubeItem): def remove_vids_from_playlist(self): """remove playlist ids from videos if needed""" needed = [i["youtube_id"] for i in self.json_data["playlist_entries"]] - data = { - "query": { - "term": {"playlist.keyword": {"value": self.youtube_id}} - }, - "_source": ["youtube_id"], - } - result = IndexPaginate("ta_video", data).get_results() + result = self.get_playlist_videos() to_remove = [ i["youtube_id"] for i in result if i["youtube_id"] not in needed ] diff --git a/backend/video/views.py b/backend/video/views.py index a679552c..ef5c011a 100644 --- a/backend/video/views.py +++ b/backend/video/views.py @@ -227,7 +227,8 @@ class VideoProgressView(ApiBaseView): expire = False current_progress.update({"watched": watched}) - redis_con.set_message(key, current_progress, expire=expire) + if position > 5: + redis_con.set_message(key, current_progress, expire=expire) response_serializer = PlayerSerializer(current_progress)