From c4d6bb35a376ca8d8cef642e027a2abb214172db Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Feb 2022 18:55:18 +0700 Subject: [PATCH 01/10] add list_items for wildcard matching --- tubearchivist/home/src/ta/ta_redis.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tubearchivist/home/src/ta/ta_redis.py b/tubearchivist/home/src/ta/ta_redis.py index d131c96..6a9efea 100644 --- a/tubearchivist/home/src/ta/ta_redis.py +++ b/tubearchivist/home/src/ta/ta_redis.py @@ -59,6 +59,19 @@ class RedisArchivist: return json_str + def list_items(self, query): + """list all matches""" + reply = self.redis_connection.execute_command( + "KEYS", self.NAME_SPACE + query + "*" + ) + all_matches = [i.decode().lstrip(self.NAME_SPACE) for i in reply] + all_results = [] + for match in all_matches: + json_str = self.get_message(match) + all_results.append(json_str) + + return all_results + def del_message(self, key): """delete key from redis""" response = self.redis_connection.execute_command( From 6078d8d27631271fdb0d2f95a022826ec5041286 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Feb 2022 18:55:52 +0700 Subject: [PATCH 02/10] add youtube_id to progress api key --- tubearchivist/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 5a71522..cfa1875 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -123,7 +123,7 @@ class VideoProgressView(ApiBaseView): """set progress position in redis""" position = request.data.get("position", 0) key = f"{request.user.id}:progress:{video_id}" - message = {"position": position} + message = {"position": position, "youtube_id": video_id} RedisArchivist().set_message(key, message, expire=False) self.response = request.data From d88d6d6a617a5e68e5bb72c9edac761fa3c136de Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Feb 2022 18:58:26 +0700 Subject: [PATCH 03/10] add video progress bar --- tubearchivist/home/templates/home/home.html | 3 +++ tubearchivist/home/views.py | 12 ++++++++++++ tubearchivist/static/css/style.css | 9 +++++++++ 3 files changed, 24 insertions(+) diff --git a/tubearchivist/home/templates/home/home.html b/tubearchivist/home/templates/home/home.html index 62d2e9a..6cf643a 100644 --- a/tubearchivist/home/templates/home/home.html +++ b/tubearchivist/home/templates/home/home.html @@ -49,6 +49,9 @@
video-thumb + {% if video.source.player.progress %} +
+ {% endif %}
play-icon diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 660ec73..131b78f 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -169,6 +169,17 @@ class ArchivistResultsView(ArchivistViewConfig): } self.data = data + def match_progress(self): + """add video progress to result context""" + results = RedisArchivist().list_items(f"{self.user_id}:progress:") + progress = {i["youtube_id"]: i["position"] for i in results} + for hit in self.context["results"]: + video = hit["source"] + if video["youtube_id"] in progress: + played_sec = progress.get(video["youtube_id"]) + total = video["player"]["duration"] + video["player"]["progress"] = 100 * (played_sec / total) + def single_lookup(self, es_path): """retrieve a single item from url""" search = SearchHandler(es_path, config=self.default_conf) @@ -212,6 +223,7 @@ class HomeView(ArchivistResultsView): self.initiate_vars(request) self._update_view_data() self.find_results() + self.match_progress() return render(request, "home/home.html", self.context) diff --git a/tubearchivist/static/css/style.css b/tubearchivist/static/css/style.css index 53a8882..10b219b 100644 --- a/tubearchivist/static/css/style.css +++ b/tubearchivist/static/css/style.css @@ -391,8 +391,17 @@ button:hover { grid-template-columns: 25% auto; } +.video-progress-bar { + position: absolute; + background-color: var(--accent-font-light); + height: 7px; + left: 0; + bottom: 3px; +} + .video-thumb img { width: 100%; + position: relative; } .video-play img { From df777104afa90586be97c3274c080b1cbb18c2d0 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Feb 2022 19:30:12 +0700 Subject: [PATCH 04/10] add video progress bar to channel_id and playlist_id views --- tubearchivist/home/templates/home/channel_id.html | 3 +++ tubearchivist/home/templates/home/playlist_id.html | 3 +++ tubearchivist/home/views.py | 2 ++ 3 files changed, 8 insertions(+) diff --git a/tubearchivist/home/templates/home/channel_id.html b/tubearchivist/home/templates/home/channel_id.html index 6a42c10..0402e09 100644 --- a/tubearchivist/home/templates/home/channel_id.html +++ b/tubearchivist/home/templates/home/channel_id.html @@ -110,6 +110,9 @@
video-thumb + {% if video.source.player.progress %} +
+ {% endif %}
play-icon diff --git a/tubearchivist/home/templates/home/playlist_id.html b/tubearchivist/home/templates/home/playlist_id.html index 41d8268..efac62b 100644 --- a/tubearchivist/home/templates/home/playlist_id.html +++ b/tubearchivist/home/templates/home/playlist_id.html @@ -91,6 +91,9 @@
video-thumb + {% if video.source.player.progress %} +
+ {% endif %}
play-icon diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 131b78f..a196f6f 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -367,6 +367,7 @@ class ChannelIdView(ArchivistResultsView): self.initiate_vars(request) self._update_view_data(channel_id) self.find_results() + self.match_progress() if self.context["results"]: channel_info = self.context["results"][0]["source"]["channel"] @@ -468,6 +469,7 @@ class PlaylistIdView(ArchivistResultsView): playlist_name = playlist_info["playlist_name"] self._update_view_data(playlist_id, playlist_info) self.find_results() + self.match_progress() self.context.update( { "title": "Playlist: " + playlist_name, From 806448624de245b3aea17258bb7c64c5068438db Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Feb 2022 21:25:12 +0700 Subject: [PATCH 05/10] return if no in progress videos --- tubearchivist/home/views.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index a196f6f..2366f03 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -172,6 +172,9 @@ class ArchivistResultsView(ArchivistViewConfig): def match_progress(self): """add video progress to result context""" results = RedisArchivist().list_items(f"{self.user_id}:progress:") + if not results: + return + progress = {i["youtube_id"]: i["position"] for i in results} for hit in self.context["results"]: video = hit["source"] From 99c97a703fc02c5b197cba1fbea14785c63d7a4b Mon Sep 17 00:00:00 2001 From: Nathan DeTar Date: Thu, 24 Feb 2022 19:39:33 -0800 Subject: [PATCH 06/10] Reduce API Calls (#181) * Reduce API calls * Fix video id * Updated `createVideoTag()` description. * Fixed URL used for cast integration * Check video duration * Updates progress bar on watched and close. * Set progress bar width to 0% by default * Cleanup, function descriptions * Cleanup console logging * Update progress bar on cast progress every 10s * Catch short <30s videos and mark as watched --- .../home/templates/home/channel_id.html | 2 + tubearchivist/home/templates/home/home.html | 2 + .../home/templates/home/playlist_id.html | 2 + tubearchivist/home/templates/home/video.html | 4 +- tubearchivist/static/cast-videos.js | 1 + tubearchivist/static/script.js | 51 ++++++++++++++----- 6 files changed, 48 insertions(+), 14 deletions(-) diff --git a/tubearchivist/home/templates/home/channel_id.html b/tubearchivist/home/templates/home/channel_id.html index 0402e09..41ee11e 100644 --- a/tubearchivist/home/templates/home/channel_id.html +++ b/tubearchivist/home/templates/home/channel_id.html @@ -112,6 +112,8 @@ video-thumb {% if video.source.player.progress %}
+ {% else %} +
{% endif %}
diff --git a/tubearchivist/home/templates/home/home.html b/tubearchivist/home/templates/home/home.html index 6cf643a..d24263a 100644 --- a/tubearchivist/home/templates/home/home.html +++ b/tubearchivist/home/templates/home/home.html @@ -51,6 +51,8 @@ video-thumb {% if video.source.player.progress %}
+ {% else %} +
{% endif %}
diff --git a/tubearchivist/home/templates/home/playlist_id.html b/tubearchivist/home/templates/home/playlist_id.html index efac62b..687df98 100644 --- a/tubearchivist/home/templates/home/playlist_id.html +++ b/tubearchivist/home/templates/home/playlist_id.html @@ -93,6 +93,8 @@ video-thumb {% if video.source.player.progress %}
+ {% else %} +
{% endif %}
diff --git a/tubearchivist/home/templates/home/video.html b/tubearchivist/home/templates/home/video.html index 90e986d..7a8b522 100644 --- a/tubearchivist/home/templates/home/video.html +++ b/tubearchivist/home/templates/home/video.html @@ -113,6 +113,8 @@ {% endif %}
{% endblock content %} diff --git a/tubearchivist/static/cast-videos.js b/tubearchivist/static/cast-videos.js index 0a30af4..867093e 100644 --- a/tubearchivist/static/cast-videos.js +++ b/tubearchivist/static/cast-videos.js @@ -43,6 +43,7 @@ function castVideoProgress(player) { var duration = player.duration; if ((currentTime % 10) <= 1.0 && currentTime != 0 && duration != 0) { // Check progress every 10 seconds or else progress is checked a few times a second postVideoProgress(videoId, currentTime); + setProgressBar(videoId, currentTime, duration); if (!getVideoPlayerWatchStatus()) { // Check if video is already marked as watched if (watchedThreshold(currentTime, duration)) { isWatched(videoId); diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js index 162e5ab..8df9ffd 100644 --- a/tubearchivist/static/script.js +++ b/tubearchivist/static/script.js @@ -10,6 +10,7 @@ function sortChange(sortValue) { function isWatched(youtube_id) { postVideoProgress(youtube_id, 0); // Reset video progress on watched; + removeProgressBar(youtube_id); var payload = JSON.stringify({'watched': youtube_id}); sendPost(payload); var seenIcon = document.createElement('img'); @@ -22,6 +23,11 @@ function isWatched(youtube_id) { document.getElementById(youtube_id).replaceWith(seenIcon); } +// Removes the progress bar when passed a video id +function removeProgressBar(videoId) { + setProgressBar(videoId, 0, 1); +} + function isWatchedButton(button) { youtube_id = button.getAttribute("data-id"); var payload = JSON.stringify({'watched': youtube_id}); @@ -298,9 +304,10 @@ function cancelDelete() { function createPlayer(button) { var videoId = button.getAttribute('data-id'); var videoData = getVideoData(videoId); + var videoProgress = getVideoProgress(videoId).position; var videoName = videoData.data.title; - var videoTag = createVideoTag(videoId); + var videoTag = createVideoTag(videoData, videoProgress); var playlist = ''; var videoPlaylists = videoData.data.playlist; // Array of playlists the video is in @@ -369,19 +376,17 @@ function createPlayer(button) { } // Add video tag to video page when passed a video id, function loaded on page load `video.html (115-117)` -function insertVideoTag(videoId) { - var videoTag = createVideoTag(videoId); +function insertVideoTag(videoData, videoProgress) { + var videoTag = createVideoTag(videoData, videoProgress); var videoMain = document.getElementsByClassName("video-main"); videoMain[0].innerHTML = videoTag; } -// Generates a video tag with subtitles when passed a video id. -function createVideoTag(videoId) { - var videoData = getVideoData(videoId); - var videoProgress = getVideoProgress(videoId).position; +// Generates a video tag with subtitles when passed videoData and videoProgress. +function createVideoTag(videoData, videoProgress) { + var videoId = videoData.data.youtube_id; var videoUrl = videoData.data.media_url; var videoThumbUrl = videoData.data.vid_thumb_url; - var subtitles = ''; var videoSubtitles = videoData.data.subtitles; // Array of subtitles if (typeof(videoSubtitles) != 'undefined' && videoData.config.downloads.subtitle) { @@ -391,7 +396,7 @@ function createVideoTag(videoId) { } var videoTag = ` -