-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.py
58 lines (48 loc) · 2 KB
/
movie.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
import urllib.request
import json
import unidecode
import config as keys
class Movie:
def __init__(self, title, runtime, metascore, theatres, showtimes, date):
self.title = self.removeDateFromTitle(title)
self.posterUrl = self.findPoster(self.title)
self.runtime = runtime
self.metascore = metascore
self.theatres = [theatres]
self.showtimes = [showtimes]
self.date = date
print(self.posterUrl)
def removeDateFromTitle(self, title):
return title.split('(')[0].strip()
def addTheatre(self, theatre):
self.theatres.append(theatre)
def addShowtimes(self, showtimes):
self.showtimes.append(showtimes)
def setPosterURL(self, url):
self.posterUrl = url
def setMetaScore(self, score):
self.metascore = score
def setRuntime(self, runtime):
self.runtime = runtime
def makeApiCall(self, url, input, apiKey):
assembledUrl = url % {"apiKey" : apiKey, "input" : input}
response = urllib.request.urlopen(assembledUrl)
return json.loads(response.read())
def findPoster(self, name):
try:
name = unidecode.unidecode(name.split("(")[0].strip().replace(" ","%20"))
movieData = self.makeApiCall("https://api.themoviedb.org/3/search/movie?api_key=%(apiKey)s&language=en-US&query=%(input)s&page=1&include_adult=false", name, keys.config['movieDBApiKey'])
except:
return None
try:
index = self.findMostPopularMovie(movieData["results"])
return "http://image.tmdb.org/t/p/original" + movieData["results"][index]["poster_path"]
except:
return None
def findMostPopularMovie(self, movies):
mostPopularIndex = 0
for x in range(0, len(movies)):
if (movies[x]["title"] == self.title):
if (movies[x]["popularity"] > movies[mostPopularIndex]["popularity"]):
mostPopularIndex = x
return mostPopularIndex