Skip to content

Commit 9e242d6

Browse files
committed
feat: enhance logging, improve error handling in downloader and GUI components, update strings
1 parent cd2fdbb commit 9e242d6

7 files changed

Lines changed: 129 additions & 96 deletions

File tree

spankbang_dl/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Main module for the SpankBang Downloader."""
22

33
from argparse import Namespace
4+
45
from core_helpers.updates import check_updates
56
from rich.traceback import install
67

@@ -15,6 +16,7 @@
1516

1617
def main() -> None:
1718
args: Namespace = get_parsed_args()
19+
install(show_locals=args.debug)
1820
logger.info("Start of session")
1921

2022
if GITHUB:
@@ -35,5 +37,4 @@ def main() -> None:
3537

3638

3739
if __name__ == "__main__":
38-
install(show_locals=False)
3940
main()

spankbang_dl/downloader.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,33 @@
1010
}
1111

1212

13-
def fetch_web_content(
14-
translations: dict[str, str], url: str, stream: bool = True
15-
) -> requests.Response:
13+
def fetch_web_content(url: str, stream: bool = True) -> requests.Response:
1614
"""Fetch the web content from the given URL.
1715
1816
Args:
19-
translations (dict[str, str]): A dictionary containing translation strings.
2017
url (str): The URL to fetch the content from.
21-
stream (bool, optional): Whether to stream the content or not. Defaults to True.
18+
stream (bool): Whether to stream the content or not. Defaults to True.
2219
2320
Returns:
2421
requests.Response: The response object.
25-
26-
Raises:
27-
requests.exceptions.RequestException: If there is an error during the request.
2822
"""
29-
try:
30-
scraper: cloudscraper.CloudScraper = cloudscraper.create_scraper()
23+
logger.debug("Fetching web content from URL: %s", url)
3124

32-
headers["Referer"] = url
25+
scraper: cloudscraper.CloudScraper = cloudscraper.create_scraper()
3326

34-
response: requests.Response = scraper.get(url, headers=headers, stream=stream)
35-
response.raise_for_status()
27+
headers["Referer"] = url
3628

37-
print(translations["success_message"].format(str(response.status_code)))
38-
return response
29+
response: requests.Response = scraper.get(url, headers=headers, stream=stream)
30+
response.raise_for_status()
3931

40-
except requests.exceptions.RequestException as e:
41-
print(translations["failure_message"].format(str(e)))
42-
logger.error(e)
43-
raise
32+
return response
4433

4534

46-
def extract_video_info(translations: dict[str, str], html: str) -> tuple[str, str]:
35+
def extract_video_info(html: str) -> tuple[str, str]:
4736
"""
4837
Extract video information from the HTML content.
4938
5039
Args:
51-
translations (dict[str, str]): A dictionary containing translation strings.
5240
html (str): The HTML content to extract information from.
5341
5442
Returns:
@@ -57,18 +45,15 @@ def extract_video_info(translations: dict[str, str], html: str) -> tuple[str, st
5745
Raises:
5846
Exception: If the video information cannot be extracted.
5947
"""
60-
try:
61-
result: re.Match[str] | None = re.search(
62-
'<video.*?src="(.*?)".*?>.*?</video>', html, re.S
63-
)
64-
src: str = result.group(1)
65-
result2: re.Match[str] | None = re.search(
66-
"<title.*?>Watch(.*?) - .*?</title.*?>", html, re.S
67-
)
68-
title: str = result2.group(1)
69-
70-
return title, src
71-
except Exception as e:
72-
print(translations["video_not_found"], e)
73-
logger.error(e)
74-
raise
48+
logger.debug("Extracting video information from HTML content")
49+
50+
result: re.Match[str] | None = re.search(
51+
'<video.*?src="(.*?)".*?>.*?</video>', html, re.S
52+
)
53+
src: str = result.group(1)
54+
result2: re.Match[str] | None = re.search(
55+
"<title.*?>Watch(.*?) - .*?</title.*?>", html, re.S
56+
)
57+
title: str = result2.group(1).strip()
58+
59+
return title, src

0 commit comments

Comments
 (0)