-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoviedb.py
154 lines (111 loc) · 3.71 KB
/
moviedb.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
import imdb
from metadata import Movie
class MovieDB:
def __init__(self, config, debug):
self.debug = debug
self.db = None
def connect(self):
if self.db is None:
self.db = imdb.IMDb()
# Returns all movies matching the search_text,
# but only the summary data (id, title, year, etc.)
def lookup_movies(self, search_text):
self.connect()
mlist = self.db.search_movie(search_text)
to_return = []
for m in mlist:
#self.db.update(m)
to_return.append(self.construct_movie_metadata(m))
return to_return
# Returns the movie best matching the search text
# Using the exact name and including the year
# helps search results
def lookup_movie(self, search_text):
self.connect()
mlist = self.db.search_movie(search_text)
# Assume its the first movie...if it isn't
# the user needs to provide a better search string
if len(mlist) == 0:
return None
movie = mlist[0]
self.db.update(movie)
return self.construct_movie_metadata(movie)
def get_movie(self, id):
self.connect()
try:
movie = self.db.get_movie(id)
self.db.update(movie)
except:
return None
return self.construct_movie_metadata(movie)
def construct_movie_metadata(self, movie):
to_return = Movie()
to_return.id = self.get_id(movie)
to_return.title = self.get_title(movie)
to_return.description = self.get_plot(movie)
to_return.movie_year = self.get_movie_year(movie)
to_return.writers = self.get_writers(movie)
to_return.actors = self.get_actors(movie)
to_return.directors = self.get_directors(movie)
to_return.producers = self.get_producers(movie)
to_return.genres = self.get_genres(movie)
to_return.rating = self.get_rating(movie)
to_return.mpaa_rating = self.get_mpaa_rating(movie)
return to_return
# Private methods for extraction of information
def get_id(self, movie):
return int(self.db.get_imdbID(movie))
def get_title(self, movie):
return movie['title']
def get_plot(self, movie):
try:
plot = movie['plot'][0]
author_location = plot.rfind('::')
to_return = plot
if author_location > 0:
to_return = plot[0:author_location]
return to_return
except:
return ''
def get_movie_year(self, movie):
return int(movie['year'])
def get_writers(self, movie):
try:
return [ w['name'] for w in movie['writer'] ]
except:
return []
def get_actors(self, movie):
try:
return [ a['name'] for a in movie['actors'] ]
except:
return []
def get_directors(self, movie):
try:
return [ d['name'] for d in movie['director'] ]
except:
return []
def get_producers(self, movie):
try:
return [ p['name'] for p in movie['producer'] ]
except:
return []
def get_genres(self, movie):
try:
return [ g for g in movie['genre'] ]
except:
return []
def get_rating(self, movie):
try:
return float(movie['rating'])
except:
return 0.0
def get_mpaa_rating(self, movie):
try:
for c in movie['certificates']:
split_c = c.split(':')
if split_c[0] == 'USA':
return split_c[1]
return ''
except:
return ''