Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: handle access token errors (twitch)

NTFSvolume7b628ac7308f
diff --git a/cyberdrop_dl/crawlers/twitch.py b/cyberdrop_dl/crawlers/twitch.py
index ba02dc345..4d412699d 100644
--- a/cyberdrop_dl/crawlers/twitch.py
+++ b/cyberdrop_dl/crawlers/twitch.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import base64
import dataclasses
import json
-from typing import TYPE_CHECKING, Any, ClassVar, Literal
+from typing import TYPE_CHECKING, Any, ClassVar, Literal, Unpack
from cyberdrop_dl.crawlers.crawler import API, Crawler, SupportedPaths
from cyberdrop_dl.exceptions import ScrapeError
@@ -13,8 +13,11 @@ from cyberdrop_dl.utils import parse_url
from cyberdrop_dl.utils.errors import error_handling_wrapper
if TYPE_CHECKING:
- from collections.abc import Generator, Mapping
+ from collections.abc import Generator
+ from curl_cffi.requests.session import HttpMethod
+
+ from cyberdrop_dl.clients.request import RequestParams
from cyberdrop_dl.url_objects import ScrapeItem
from cyberdrop_dl.utils import m3u8
from cyberdrop_dl.utils.m3u8 import M3U8
@@ -72,10 +75,11 @@ class TwitchCrawler(Crawler):
self,
url: AbsoluteHttpURL,
/,
- headers: Mapping[str, str] | None = None,
+ method: HttpMethod = "GET",
media_type: Literal["video", "audio", "subtitle"] | None = None,
+ **kwargs: Unpack[RequestParams],
) -> m3u8.M3U8:
- m3u8_obj = await super()._request_m3u8(url, headers=headers, media_type=media_type)
+ m3u8_obj = await super()._request_m3u8(url, method, media_type, **kwargs)
# Some formats are "hidden" unless the user is logged in (1080p+ resolutions)
# We can extract and parse them manually, bypassing the logging requirement
@@ -249,7 +253,15 @@ class TwitchAPI(API):
},
"ed230aa1e33e07eebb8928504583da78a5173989fadfb1ac94be06a04f3cdbe9",
)
- return resp["data"]["videoPlaybackAccessToken"]
+ token = resp["data"]["videoPlaybackAccessToken"]
+ try:
+ data = json.loads(token)
+ except ValueError:
+ pass
+ else:
+ if type(data) is dict and data.get("forbidden"):
+ raise ScrapeError(403, data.get("reason"))
+ return token
@dataclasses.dataclass(slots=True, order=True, frozen=True)
diff --git a/tests/crawlers/test_cases/twitch.py b/tests/crawlers/test_cases/twitch.py
index 0d9f2660d..382d27fa6 100644
--- a/tests/crawlers/test_cases/twitch.py
+++ b/tests/crawlers/test_cases/twitch.py
@@ -4,7 +4,7 @@ TEST_CASES = [
"url": "https://www.twitch.tv/fanfan/clip/GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM",
"results": [
{
- "url": "re:/h0073-YuUM9o4kAK7QUTjA/AT-cm%7Ch0073-YuUM9o4kAK7QUTjA.mp4",
+ "url": "re:expires=",
"filename": "not scared btw [GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM][60fps][1080p].mp4",
"debrid_url": None,
"original_filename": "not scared btw",
@@ -19,7 +19,7 @@ TEST_CASES = [
"url": "https://clips.twitch.tv/GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM",
"results": [
{
- "url": "re:/h0073-YuUM9o4kAK7QUTjA/AT-cm%7Ch0073-YuUM9o4kAK7QUTjA.mp4",
+ "url": "re:expires=",
"filename": "not scared btw [GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM][60fps][1080p].mp4",
"referer": "https://clips.twitch.tv/GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM",
}
@@ -39,7 +39,7 @@ TEST_CASES = [
"url": "https://m.twitch.tv/clip/GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM",
"results": [
{
- "url": "re:/h0073-YuUM9o4kAK7QUTjA/AT-cm%7Ch0073-YuUM9o4kAK7QUTjA.mp4",
+ "url": "ANY",
"filename": "not scared btw [GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM][60fps][1080p].mp4",
"referer": "https://clips.twitch.tv/GeniusDifficultSproutPhilosoraptor-5i5Qz2jiQArtBROM",
}
@@ -47,6 +47,7 @@ TEST_CASES = [
},
{
"url": "https://www.twitch.tv/videos/1744169736",
+ "description": "hidden m3u8 data",
"results": [
{
"url": "re:https://usher.ttvnw.net/vod/1744169736.m3u8",