-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (39 loc) · 1.23 KB
/
utils.py
File metadata and controls
58 lines (39 loc) · 1.23 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
49
50
51
52
53
54
55
56
57
58
import re
import traceback
import requests
from parsel import Selector
FIND_ID_REGEX = re.compile("/([0-9]+)")
session = requests.Session()
session.trust_env = False
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
}
def download(url, additional_headers={}):
joined_headers = dict(headers)
joined_headers.update(additional_headers)
return session.get(url, headers=joined_headers).text
def download_multiple(urls):
return [download(url) for url in urls]
def remove_big_whitespaces(t):
return t.replace(" ", "").replace("\n", "")
def remove_big_whitespaces_selector(selector):
return Selector(text=remove_big_whitespaces(selector.get()))
def find_id_in_url(url):
result = FIND_ID_REGEX.search(url)
if result is not None:
return result[1]
return None
def reverse(s):
return s[::-1]
def get_last_part_url(url):
s = reverse(url)
for index, character in enumerate(s):
if character == "/":
return reverse(s[0:index])
return None
def catch_errors(f, data):
try:
return f(data)
except Exception:
print(traceback.print_exc())
return None