-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathrequest.py
More file actions
48 lines (38 loc) · 1.4 KB
/
Copy pathrequest.py
File metadata and controls
48 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import ssl
import time
from typing import Union
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from google_play_scraper.exceptions import ExtraHTTPError, NotFoundError
ssl._create_default_https_context = ssl._create_unverified_context
MAX_RETRIES = 3
RATE_LIMIT_DELAY = 5
def _urlopen(obj, timeout: int | None = None):
try:
resp = urlopen(obj, timeout=timeout)
except HTTPError as e:
if e.code == 404:
raise NotFoundError("App not found(404).")
else:
raise ExtraHTTPError(
"App not found. Status code {} returned.".format(e.code)
)
return resp.read().decode("UTF-8")
def post(url: str, data: Union[str, bytes], headers: dict) -> str:
last_exception = None
rate_exceeded_count = 0
for _ in range(MAX_RETRIES):
try:
resp = _urlopen(Request(url, data=data, headers=headers))
except Exception as e:
last_exception = e
continue
if "com.google.play.gateway.proto.PlayGatewayError" in resp:
rate_exceeded_count += 1
last_exception = Exception("com.google.play.gateway.proto.PlayGatewayError")
time.sleep(RATE_LIMIT_DELAY * rate_exceeded_count)
continue
return resp
raise last_exception
def get(url: str, timeout: int | None = None) -> str:
return _urlopen(url, timeout)