-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathimdbpie.py
129 lines (97 loc) · 4.74 KB
/
imdbpie.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import time
import requests
import urllib.parse
import hashlib
import re
base_uri = 'app.imdb.com'
api_key = '2wex6aeu6a8q9e49k7sfvufd6rhh0n'
sha1_key = hashlib.sha1(api_key.encode('utf-8')).hexdigest()
class Imdb:
def __init__( self, options={} ):
self.options = options
if 'anonymize' in options and options['anonymize'] is True:
global base_uri
base_uri = 'youtubeproxy.org/default.aspx?prx=https://' + base_uri
def build_url(self, path, params):
global base_uri
global api_key
global sha1_key
default_params = {"api" : "v1", "appid" : "iphone1_1", "apiPolicy" : "app1_1", "apiKey" : sha1_key, "locale" : "en_US", "timestamp" : int(time.time())}
query_params = dict(list(default_params.items()) + list(params.items()))
query_params = urllib.parse.urlencode(query_params)
return 'https://' + base_uri + path + '?' + query_params
def find_movie_by_id(self, imdb_id):
url = self.build_url('/title/maindetails', {'tconst' : imdb_id})
result = self.get(url)
movie = Movie(result["data"])
return movie
def filter_out(self, string):
if string != "id" and string != "title":
return True
else:
return False
def find_by_title(self, title):
default_find_by_title_params = {"json" : "1", "nr" : 1, "tt": "on", "q" : title}
query_params = urllib.parse.urlencode(default_find_by_title_params)
results = self.get('http://www.imdb.com/xml/find?' + query_params)
keys = ["title_popular", "title_exact", "title_approx", "title_substring"]
data = results
movie_results = dict()
for i, key in enumerate(keys):
if key in data:
for j,r in enumerate(filter(self.filter_out, data[key])):
year_match = re.findall(r'(\d{4})', r['title_description'])
year = year_match[0] if len(year_match) > 0 else None
movie_results[j] = {'title' : r["title"], 'year' : year, 'imdb_id' : r["id"]}
return movie_results
def top_250(self):
url = self.build_url('/chart/top', {})
result = self.get(url)
return result["data"]["list"]["list"]
def popular_shows(self):
url = self.build_url('/chart/tv', {})
result = self.get(url)
return result["data"]["list"]
def get(self, url):
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B5097d Safari/6531.22.7'})
return json.loads(r.text)
class Person:
def __init__( self, options={}):
self.name = options["name"]["name"]
self.imdb_id = options["name"]["nconst"]
self.role = options["char"] if "char" in options else None
class Movie:
def __init__( self, options={}):
#parse info
self.imdb_id = options["tconst"]
self.title = options["title"]
self.tagline = options["tagline"] if "tagline" in options else None
self.plot = options["plot"]["outline"] if "plot" in options else None
self.runtime = str(round((options["runtime"]["time"]/60))) + ' min' if "runtime" in options else None
self.rating = options["rating"] if "rating" in options else None
self.poster_url = options["image"]["url"] if "image" in options else None
self.release_date = options["release_date"]["normal"] if "release_date" in options and options["release_date"]["normal"] else None
self.certification = options["certificate"]["certificate"] if options["certificate"] and options["certificate"]["certificate"] else None
self.genres = options["genres"] or []
self.trailer_url = options["trailer"]["slates"][0]["url"] if("trailer" in options and options["trailer"]["slates"] and options["trailer"]["slates"][0]) else None
#parse directors
self.directors = {}
if options["directors_summary"]:
for key, value in enumerate(options["directors_summary"]):
self.directors[key] = Person(value)
#parse actors
self.actors = {}
if options["cast_summary"]:
for key, value in enumerate(options["cast_summary"]):
self.actors[key] = Person(value)
#parse writers
self.writers = {}
if options["cast_summary"]:
for key, value in enumerate(options["writers_summary"]):
self.writers[key] = Person(value)
#parse trailers
self.trailers = {}
if "trailer" in options and options["trailer"]["encodings"]:
for k, v in options["trailer"]["encodings"].items():
self.trailers[v["format"]] = v["url"]