-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
53 lines (36 loc) · 1.08 KB
/
utils.py
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
import json
import re
import requests
def has_description(record):
return (
record is not None
and "description" in record
and isinstance(record["description"], str)
and len(record["description"].strip()) > 0
)
def load_json_ld(url):
try:
response = requests.get(url, timeout=60)
except requests.exceptions.ReadTimeout:
return None
if not response.ok:
return None
pattern = r'<script type="application/ld\+json">(.*?)</script>'
matches = re.findall(pattern, response.text, re.DOTALL)
if len(matches) == 0:
return None
match = matches[0]
try:
return json.loads(match.strip())
except (TypeError, json.JSONDecodeError):
pass
return None
def pick(path, data):
if len(path) == 0:
return data
key = path[0]
if isinstance(data, dict) and isinstance(key, str) and key in data:
return pick(path[1:], data[key])
if isinstance(data, list) and isinstance(key, int) and len(data) > key:
return pick(path[1:], data[key])
return None