Skip to content

Commit 3d07ffb

Browse files
authored
Merge pull request #781 from OMEGARAZER/requests
2 parents 5cf2c81 + 8f9bed0 commit 3d07ffb

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

bdfr/__main__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ def wrap(func):
7777
return wrap
7878

7979

80-
def _check_version(context, param, value):
80+
def _check_version(context, _param, value):
8181
if not value or context.resilient_parsing:
8282
return
8383
current = __version__
84-
latest = requests.get("https://pypi.org/pypi/bdfr/json", timeout=10).json()["info"]["version"]
85-
print(f"You are currently using v{current} the latest is v{latest}")
84+
try:
85+
latest = requests.get("https://pypi.org/pypi/bdfr/json", timeout=10).json()["info"]["version"]
86+
print(f"You are currently using v{current} the latest is v{latest}")
87+
except TimeoutError:
88+
logger.exception(f"Timeout reached fetching current version from Pypi - BDFR v{current}")
89+
raise
8690
context.exit()
8791

8892

bdfr/oauth2.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ def __init__(self, wanted_scopes: set[str], client_id: str, client_secret: str):
2424

2525
@staticmethod
2626
def _check_scopes(wanted_scopes: set[str]):
27-
response = requests.get(
28-
"https://www.reddit.com/api/v1/scopes.json",
29-
headers={"User-Agent": "fetch-scopes test"},
30-
timeout=10,
31-
)
27+
try:
28+
response = requests.get(
29+
"https://www.reddit.com/api/v1/scopes.json",
30+
headers={"User-Agent": "fetch-scopes test"},
31+
timeout=10,
32+
)
33+
except TimeoutError:
34+
raise BulkDownloaderException("Reached timeout fetching scopes")
3235
known_scopes = [scope for scope, data in response.json().items()]
3336
known_scopes.append("*")
3437
for scope in wanted_scopes:

bdfr/site_downloaders/base_downloader.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ def retrieve_url(url: str, cookies: dict = None, headers: dict = None) -> reques
3131
except requests.exceptions.RequestException as e:
3232
logger.exception(e)
3333
raise SiteDownloaderError(f"Failed to get page {url}")
34+
except TimeoutError as e:
35+
logger.exception(e)
36+
raise SiteDownloaderError(f"Timeout reached attempting to get page {url}")
3437
if res.status_code != 200:
35-
raise ResourceNotFound(f"Server responded with {res.status_code} to {url}")
38+
raise ResourceNotFound(f"Server responded with {res.status_code} at {url}")
3639
return res
3740

3841
@staticmethod
@@ -42,6 +45,21 @@ def post_url(url: str, cookies: dict = None, headers: dict = None, payload: dict
4245
except requests.exceptions.RequestException as e:
4346
logger.exception(e)
4447
raise SiteDownloaderError(f"Failed to post to {url}")
48+
except TimeoutError as e:
49+
logger.exception(e)
50+
raise SiteDownloaderError(f"Timeout reached attempting to post to page {url}")
4551
if res.status_code != 200:
4652
raise ResourceNotFound(f"Server responded with {res.status_code} to {url}")
4753
return res
54+
55+
@staticmethod
56+
def head_url(url: str, cookies: dict = None, headers: dict = None) -> requests.Response:
57+
try:
58+
res = requests.head(url, cookies=cookies, headers=headers, timeout=10)
59+
except requests.exceptions.RequestException as e:
60+
logger.exception(e)
61+
raise SiteDownloaderError(f"Failed to check head at {url}")
62+
except TimeoutError as e:
63+
logger.exception(e)
64+
raise SiteDownloaderError(f"Timeout reached attempting to check head at {url}")
65+
return res

bdfr/site_downloaders/gallery.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
from typing import Optional
55

6-
import requests
76
from praw.models import Submission
87

98
from bdfr.exceptions import SiteDownloaderError
@@ -41,8 +40,7 @@ def _get_links(id_dict: list[dict]) -> list[str]:
4140
possible_extensions = (".jpg", ".png", ".gif", ".gifv", ".jpeg")
4241
for extension in possible_extensions:
4342
test_url = f"https://i.redd.it/{image_id}{extension}"
44-
response = requests.head(test_url, timeout=10)
45-
if response.status_code == 200:
43+
if Gallery.head_url(test_url).status_code == 200:
4644
out.append(test_url)
4745
break
4846
return out

bdfr/site_downloaders/redgifs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import re
55
from typing import Optional
66

7-
import requests
87
from cachetools import TTLCache, cached
98
from praw.models import Submission
109

@@ -67,7 +66,7 @@ def _get_link(url: str) -> set[str]:
6766
out = set()
6867
try:
6968
if response_json["gif"]["type"] == 1: # type 1 is a video
70-
if requests.head(response_json["gif"]["urls"]["hd"], headers=headers, timeout=10).ok:
69+
if Redgifs.head_url(response_json["gif"]["urls"]["hd"], headers=headers).status_code == 200:
7170
out.add(response_json["gif"]["urls"]["hd"])
7271
else:
7372
out.add(response_json["gif"]["urls"]["sd"])

bdfr/site_downloaders/vidble.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Optional
77

88
import bs4
9-
import requests
109
from praw.models import Submission
1110

1211
from bdfr.exceptions import SiteDownloaderError
@@ -36,7 +35,7 @@ def get_links(url: str) -> set[str]:
3635
if not re.search(r"vidble.com/(show/|album/|watch\?v)", url):
3736
url = re.sub(r"/(\w*?)$", r"/show/\1", url)
3837

39-
page = requests.get(url, timeout=10)
38+
page = Vidble.retrieve_url(url)
4039
soup = bs4.BeautifulSoup(page.text, "html.parser")
4140
content_div = soup.find("div", attrs={"id": "ContentPlaceHolder1_divContent"})
4241
images = content_div.find_all("img")

0 commit comments

Comments
 (0)