Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: log best rendition selection (HLS) (#1907)

NTFSvolumeaa6b0304f622
diff --git a/cyberdrop_dl/crawlers/_hls.py b/cyberdrop_dl/crawlers/_hls.py
index 276d5c0cb..7fa4cb7aa 100644
--- a/cyberdrop_dl/crawlers/_hls.py
+++ b/cyberdrop_dl/crawlers/_hls.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
import dataclasses
+import logging
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Literal
@@ -16,6 +17,8 @@ if TYPE_CHECKING:
from cyberdrop_dl.url_objects import AbsoluteHttpURL
+logger = logging.getLogger(__name__)
+
class HLSMixin(ABC):
"""Class to fetch and parse HTTP live streams
@@ -36,7 +39,9 @@ class HLSMixin(ABC):
) -> tuple[m3u8.Rendition, m3u8.RenditionDetails | None]:
m3u8_obj = await self._request_m3u8(url, headers)
if m3u8_obj.is_variant:
+ logger.info("Selecting best rendition from %s", url)
rendition = m3u8.select_best_rendition(m3u8_obj, only=only, exclude=exclude)
+ logger.info("Selected best rendition for %s:\n%s", url, rendition)
return await self._resolve_rendition(rendition, headers)
m3u8_obj.media_type = "video"
return m3u8.Rendition(m3u8_obj, None, None), None
diff --git a/cyberdrop_dl/utils/m3u8.py b/cyberdrop_dl/utils/m3u8.py
index f4d5c4f83..8e57677f8 100644
--- a/cyberdrop_dl/utils/m3u8.py
+++ b/cyberdrop_dl/utils/m3u8.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import dataclasses
+import logging
from datetime import timedelta
from enum import StrEnum
from functools import cached_property
@@ -18,6 +19,8 @@ if TYPE_CHECKING:
from cyberdrop_dl.url_objects import AbsoluteHttpURL
+logger = logging.getLogger(__name__)
+
class MediaType(StrEnum):
audio = "AUDIO"
@@ -107,6 +110,9 @@ class RenditionDetails:
media: MediaList
urls: MediaURLs
+ def __json__(self) -> dict[str, Any]:
+ return {"stream": dataclasses.asdict(self.stream_info), "urls": self.urls._asdict()}
+
@staticmethod
def new(playlist: Playlist) -> RenditionDetails:
assert playlist.uri
@@ -161,13 +167,27 @@ class M3U8(_M3U8):
return timedelta(seconds=total_duration)
+class _LazyRenditionLog:
+ def __init__(self, groups: list[RenditionDetails]) -> None:
+ self.groups = groups
+
+ def __json__(self) -> tuple[dict[str, Any], ...]:
+ return tuple(rendition.__json__() for rendition in self.groups)
+
+ def __str__(self) -> str:
+ return str(self.__json__())
+
+
[email protected](slots=True)
class VariantM3U8Parser:
"""Parses groups inside a variant M3U8"""
- def __init__(self, m3u8: M3U8) -> None:
- assert m3u8.is_variant
- self._m3u8 = m3u8
- self.groups = sorted((RenditionDetails.new(playlist) for playlist in m3u8.playlists), reverse=True)
+ _m3u8: M3U8
+ groups: list[RenditionDetails] = dataclasses.field(init=False)
+
+ def __post_init__(self) -> None:
+ assert self._m3u8.is_variant
+ self.groups = sorted((RenditionDetails.new(playlist) for playlist in self._m3u8.playlists), reverse=True)
def get_rendition_groups(
self, only: Iterable[str] = (), *, exclude: Iterable[str] = ()
@@ -188,6 +208,7 @@ class VariantM3U8Parser:
yield group
def get_best_group(self, only: Iterable[str] = (), *, exclude: Iterable[str] = ()) -> RenditionDetails:
+ logger.debug("Available renditions from %s:\n%s", self._m3u8.base_uri, _LazyRenditionLog(self.groups))
return next(self.get_rendition_groups(only=only, exclude=exclude))