From 0d34a96227a7dc354edff90ed02709afc2b9a263 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Mon, 27 Jun 2022 12:42:59 +0200 Subject: [PATCH 1/2] add downloads view fix yt-dlp cache not writable fix video count --- deploy/Dockerfile | 3 +- ucast/queue.py | 20 +++++- ucast/service/youtube.py | 14 +++- ucast/tasks/download.py | 10 +++ ucast/templates/bulma/base.html | 4 +- ucast/templates/ucast/download_errors.html | 50 ------------- ucast/templates/ucast/downloads.html | 81 ++++++++++++++++++++++ ucast/templates/ucast/downloads_items.html | 13 ++++ ucast/templates/ucast/videos.html | 2 +- ucast/tests/conftest.py | 8 +++ ucast/tests/tasks/test_download.py | 2 +- ucast/urls.py | 10 +-- ucast/views.py | 25 +++++-- 13 files changed, 175 insertions(+), 67 deletions(-) delete mode 100644 ucast/templates/ucast/download_errors.html create mode 100644 ucast/templates/ucast/downloads.html create mode 100644 ucast/templates/ucast/downloads_items.html diff --git a/deploy/Dockerfile b/deploy/Dockerfile index c997332..b22b23d 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -9,7 +9,8 @@ FROM python:3.10 ARG TARGETPLATFORM COPY --from=0 /build/dist /install -RUN pip install -- /install/*.whl gunicorn honcho +RUN pip install -- /install/*.whl gunicorn honcho && \ + rm -rf ~/.cache/pip # ffmpeg static source (https://johnvansickle.com/ffmpeg/) RUN set -e; \ diff --git a/ucast/queue.py b/ucast/queue.py index 5e56343..fbdf55e 100644 --- a/ucast/queue.py +++ b/ucast/queue.py @@ -4,6 +4,7 @@ import rq_scheduler from django.conf import settings from rq import registry +from ucast.models import Video from ucast.service import util @@ -33,8 +34,7 @@ def get_worker(**kwargs) -> rq.Worker: def enqueue(f, *args, **kwargs) -> rq.job.Job: queue = get_queue() - # return queue.enqueue(f, *args, **kwargs) - return queue.enqueue_call(f, args, kwargs) + return queue.enqueue(f, *args, **kwargs) def get_statistics() -> dict: @@ -90,3 +90,19 @@ def get_statistics() -> dict: def get_failed_job_registry(): queue = get_queue() return registry.FailedJobRegistry(queue.name, queue.connection) + + +def get_downloading_videos(): + queue = get_queue() + videos = {} + + for job in queue.jobs: + if ( + job.func_name == "ucast.tasks.download.download_video" + and job.args + and isinstance(job.args[0], Video) + ): + video = job.args[0] + videos[video.id] = video + + return list(videos.values()) diff --git a/ucast/service/youtube.py b/ucast/service/youtube.py index 1ae4479..670cc46 100644 --- a/ucast/service/youtube.py +++ b/ucast/service/youtube.py @@ -125,7 +125,19 @@ def download_thumbnail(vinfo: VideoDetails, download_path: Path): def get_video_details(video_id: str) -> VideoDetails: - with YoutubeDL() as ydl: + """ + Get the details of a YouTube video without downloading it. + + :param video_id: YouTube video ID + :return: VideoDetails + """ + cache = storage.Cache() + + ydl_params = { + "cachedir": str(cache.dir_ytdlp_cache), + } + + with YoutubeDL(ydl_params) as ydl: info = ydl.extract_info(video_id, download=False) return VideoDetails.from_vinfo(info) diff --git a/ucast/tasks/download.py b/ucast/tasks/download.py index a91ec4d..be14ddb 100644 --- a/ucast/tasks/download.py +++ b/ucast/tasks/download.py @@ -10,6 +10,13 @@ from ucast.service import controller, cover, storage, util, videoutil, youtube def _load_scraped_video(vid: youtube.VideoScraped, channel: Channel): + # Use Redis to ensure the same video is not processed multiple times + redis = queue.get_redis_connection() + lock_key = f"ucast:lock_load_video:{vid.id}" + + if not redis.set(lock_key, "1", 120, nx=True): + return + # Create video object if it does not exist try: video = Video.objects.get(video_id=vid.id) @@ -18,6 +25,7 @@ def _load_scraped_video(vid: youtube.VideoScraped, channel: Channel): # Dont load active livestreams if details.is_currently_live: + redis.delete(lock_key) return slug = Video.get_new_slug( @@ -44,6 +52,8 @@ def _load_scraped_video(vid: youtube.VideoScraped, channel: Channel): ): queue.enqueue(download_video, video) + redis.delete(lock_key) + def download_video(video: Video): """ diff --git a/ucast/templates/bulma/base.html b/ucast/templates/bulma/base.html index 431b8ce..c20531b 100644 --- a/ucast/templates/bulma/base.html +++ b/ucast/templates/bulma/base.html @@ -27,8 +27,8 @@