-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
54 lines (42 loc) · 1.28 KB
/
functions.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
import urllib.request
import urllib.parse
import json
import time
def load_json_data_from_url(base_url, url_params):
url = '%s?%s' % (base_url, urllib.parse.urlencode(url_params))
temp = urllib.request.urlopen(url)
if temp.getcode() == 429:
time.sleep(2)
response = temp.read().decode('utf-8')
return json.loads(response)
def make_tmdb_api_request(method, api_key, extra_params=None):
extra_params = extra_params or {}
url = 'https://api.themoviedb.org/3%s' % method
params = {
'api_key': api_key,
'language': 'ru',
}
params.update(extra_params)
return load_json_data_from_url(url, params)
def get_collect(film):
if film['belongs_to_collection']:
return film['belongs_to_collection']['name']
else:
return None
def func_genres(film):
if film['genres']:
return set(j['name'] for j in film['genres'])
else:
return set()
def find_director(film):
if film['credits']:
for j in film['credits']['crew']:
if j['job'].lower() == 'director'.lower():
return j['name']
else:
return None
def func_keywords(film):
if film['keyw']['keywords']:
return set(j['name'] for j in film['keyw']['keywords'])
else:
return set()