-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathImdbParser.py
More file actions
84 lines (69 loc) · 2.94 KB
/
ImdbParser.py
File metadata and controls
84 lines (69 loc) · 2.94 KB
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
#!/usr/bin/env python
# encoding: utf-8
"""
ImdbParser.py
Created by Ichabond on 2012-07-01.
Module for Pythonbits to provide proper and clean
imdb parsing.
"""
import sys
import json
try:
import imdbpie
except ImportError:
print >> sys.stderr, "IMDbPie is required for Pythonbits to function"
sys.exit(1)
class IMDB(object):
def __init__(self):
self.imdb = imdbpie.Imdb()
self.results = None
self.movie = None
def search(self, title):
try:
results = self.imdb.find_by_title(title)
except imdb.IMDbError, e:
print >> sys.stderr, "You probably don't have an internet connection. Complete error report:"
print >> sys.stderr, e
sys.exit(3)
self.results = results
def movieSelector(self):
try:
print "Movies found:"
for (counter, movie) in enumerate(self.results):
outp = u'%s: %s (%s)' % (counter, movie['title'], movie['year'])
print outp
selection = int(raw_input('Select the correct movie [0-%s]: ' % (len(self.results) - 1)))
self.movie = self.imdb.find_movie_by_id(self.results[selection]['imdb_id'])
except ValueError as e:
try:
selection = int(
raw_input("This is not a correct movie-identifier, try again [0-%s]: " % (len(self.results) - 1)))
self.movie = self.imdb.find_movie_by_id(self.results[selection]['imdb_id'])
except (ValueError, IndexError) as e:
print >> sys.stderr, "You failed"
print >> sys.stderr, e
except IndexError as e:
try:
selection = int(
raw_input("Your chosen value does not match a movie, try again [0-%s]: " % (len(self.results) - 1)))
self.movie = self.imdb.find_movie_by_id(self.results[selection]['imdb_id'])
except (ValueError, IndexError) as e:
print >> sys.stderr, "You failed"
print >> sys.stderr, e
def summary(self):
if self.movie:
return {'director': u" | ".join([director.name for director in self.movie.directors_summary]),
'runtime': self.movie.runtime, 'rating': self.movie.rating,
'name': self.movie.title, 'votes': self.movie.votes, 'cover': self.movie.cover_url,
'genre': u" | ".join([genre for genre in self.movie.genres]),
'writers': u" | ".join([writer.name for writer in self.movie.writers_summary]),
'mpaa': self.movie.certification,
'description': self.movie.plot_outline,
'url': u"http://www.imdb.com/title/%s" % self.movie.imdb_id,
'year': self.movie.year}
if __name__ == "__main__":
imdb = IMDB()
imdb.search("Tinker Tailor Soldier Spy")
imdb.movieSelector()
summary = imdb.summary()
print summary