Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: json from js parsing

NTFSvolume975989cfb836
diff --git a/cyberdrop_dl/scraper/crawlers/spankbang_crawler.py b/cyberdrop_dl/scraper/crawlers/spankbang_crawler.py
index 149751caa..dbe7dff69 100644
--- a/cyberdrop_dl/scraper/crawlers/spankbang_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/spankbang_crawler.py
@@ -167,14 +167,19 @@ def get_info_dict(soup: BeautifulSoup) -> VideoInfo:
info_js_script = soup.select_one(JS_SELECTOR)
extended_info_js_script = soup.select_one(EXTENDED_JS_SELECTOR)
+ info_js_script_text: str = info_js_script.text # type: ignore
+ extended_info_js_script_text: str = extended_info_js_script.text # type: ignore
+
title_tag = soup.select_one("div#video h1")
title: str = title_tag.get("title") or title_tag.text.replace("\n", "") # type: ignore
- info: dict[str, str | dict] = javascript.parse_js_vars(info_js_script.text) # type: ignore
- extended_info_dict = javascript.parse_json_to_dict(extended_info_js_script.text) # type: ignore
- embed_url = URL(info["embedUrl"]) # type: ignore
+ del soup
+ info: dict[str, str | dict] = javascript.parse_js_vars(info_js_script_text)
+ extended_info_dict = javascript.parse_json_to_dict(extended_info_js_script_text)
+ # type: ignore
info["title"] = title.strip()
info = info | extended_info_dict
+ embed_url = URL(info["embedUrl"])
info["video_id"] = embed_url.parts[1]
javascript.clean_dict(info, "stream_data")
log_debug(json.dumps(info, indent=4))
diff --git a/cyberdrop_dl/utils/javascript.py b/cyberdrop_dl/utils/javascript.py
index ccb64eed6..099867d68 100644
--- a/cyberdrop_dl/utils/javascript.py
+++ b/cyberdrop_dl/utils/javascript.py
@@ -17,7 +17,7 @@ def recover_urls(js_text: str) -> str:
return js_text.replace(HTTPS_PLACEHOLDER, "https:").replace(HTTP_PLACEHOLDER, "http:")
-def parse_js_vars(js_text: str) -> dict:
+def parse_js_vars(js_text: str, use_regex: bool = False) -> dict:
data = {}
lines = js_text.split(";")
for line in lines:
@@ -30,11 +30,11 @@ def parse_js_vars(js_text: str) -> dict:
value = value.strip()
data[name] = value
if value.startswith("{") or value.startswith("["):
- data[name] = parse_json_to_dict(value)
+ data[name] = parse_json_to_dict(value, use_regex)
return data
-def parse_json_to_dict(js_text: str, use_regex: bool = True) -> dict:
+def parse_json_to_dict(js_text: str, use_regex: bool = False) -> dict:
json_str = js_text.replace("\t", "").replace("\n", "").strip()
json_str = replace_quotes(json_str)
if use_regex:
@@ -46,7 +46,24 @@ def parse_json_to_dict(js_text: str, use_regex: bool = True) -> dict:
def replace_quotes(js_text: str) -> str:
- return js_text.replace(",'", ',"').replace("':", '":').replace(", '", ', "').replace("' :", '" :')
+ # We can't just replace every single ' with " because it will brake if the json has english words like: it's
+
+ replace_pairs = [
+ ("{'", '{"'),
+ ("'}", '"}'),
+ ("['", '["'),
+ ("']", '"]'),
+ (",'", ',"'),
+ ("':", '":'),
+ (", '", ', "'),
+ ("' :", '" :'),
+ ("',", '",'),
+ (": '", ': "'),
+ ]
+ clean_js_text = js_text
+ for old, new in replace_pairs:
+ clean_js_text = clean_js_text.replace(old, new)
+ return clean_js_text
def is_valid_key(key: str) -> bool: