Repository: yt-dlp/yt-dlp
File: yt_dlp/downloader/soop.py (Python)

Code before:
import threading
import time

from .common import FileDownloader
from . import HlsFD
from ..extractor.afreecatv import _cloudfront_auth_request
from ..networking.exceptions import network_exceptions


class SoopVodFD(FileDownloader):
    """
    Downloads Soop subscription VODs with required cookie refresh requests
    Note, this is not a part of public API, and will be removed without notice.
    DO NOT USE
    """

    def real_download(self, filename, info_dict):
        self.to_screen(f'[{self.FD_NAME}] Downloading Soop subscription VOD HLS')
        fd = HlsFD(self.ydl, self.params)
        refresh_params = info_dict.get('_cookie_refresh_params')
        if not refresh_params:
            self.report_warning(
                'Cookie refresh parameters not found; falling back to standard HLS downloader')
            return fd.real_download(filename, info_dict)

        fd = HlsFD(self.ydl, self.params)

        stop_event = threading.Event()
        referer_url = info_dict.get('webpage_url') or ''
        refresh_thread = threading.Thread(
            target=self._cookie_refresh_thread,
            args=(stop_event, refresh_params, referer_url),
        )
        refresh_thread.start()

        try:
            return fd.real_download(filename, info_dict)
        finally:
            stop_event.set()

    def _cookie_refresh_thread(self, stop_event, refresh_params, referer_url):
        m3u8_url = refresh_params['m3u8_url']
        strm_id = refresh_params['strm_id']
        video_id = refresh_params['video_id']

        def _get_cloudfront_cookie_expiration(m3u8_url):
            cookies = self.ydl.cookiejar.get_cookies_for_url(m3u8_url)
            return min((cookie.expires for cookie in cookies if 'CloudFront' in cookie.name and cookie.expires), default=0)


Review comment (security):
Could you test what happens if you use `--write-info-json --skip-download` followed by `--load-info-json <filename>.info.json`?

I ask because if a user does this, then we will lose `_cookie_refresh_params`. The field will be sanitized from the written/loaded info JSON since we're not using `--no-clean-info`.

If you wait 90 seconds inbetween extraction and loading the info back then I assume the standard HlsFD  will immediately hit a 403 error and yt-dlp will re-extract.

But I'm curious what happens if you load the info back immediately (in under 90 seconds)... HlsFD will start to download and then hit the 403 after ~90 seconds without explanation, yeah?

Maybe it would be better to `raise ReExtractInfo` here instead of warning and returning a standard `HlsFD` download call?

Alternatively, we could smuggle the refresh params as a field in `downloader_options`, similar to what `NiconicoLiveFD` does

Code after:
import threading
import time

from .common import FileDownloader
from . import HlsFD
from ..extractor.afreecatv import _cloudfront_auth_request
from ..networking.exceptions import network_exceptions


class SoopVodFD(FileDownloader):
    """
    Downloads Soop subscription VODs with required cookie refresh requests
    Note, this is not a part of public API, and will be removed without notice.
    DO NOT USE
    """

    def real_download(self, filename, info_dict):
        self.to_screen(f'[{self.FD_NAME}] Downloading Soop subscription VOD HLS')
        fd = HlsFD(self.ydl, self.params)
        refresh_params = info_dict['_cookie_refresh_params']
        referer_url = info_dict['webpage_url']

        stop_event = threading.Event()
        refresh_thread = threading.Thread(
            target=self._cookie_refresh_thread,
            args=(stop_event, refresh_params, referer_url),
        )
        refresh_thread.start()

        try:
            return fd.real_download(filename, info_dict)
        finally:
            stop_event.set()

    def _cookie_refresh_thread(self, stop_event, refresh_params, referer_url):
        m3u8_url = refresh_params['m3u8_url']
        strm_id = refresh_params['strm_id']
        video_id = refresh_params['video_id']

        def _get_cloudfront_cookie_expiration(m3u8_url):
            cookies = self.ydl.cookiejar.get_cookies_for_url(m3u8_url)
            return min((cookie.expires for cookie in cookies if 'CloudFront' in cookie.name and cookie.expires), default=0)

        while not stop_event.wait(5):
            current_time = time.time()
            expiration_time = _get_cloudfront_cookie_expiration(m3u8_url)
            last_refresh_check = refresh_params.get('_last_refresh', 0)

            # Cookie TTL is 90 seconds, but let's give ourselves a 15-second cushion