This repository has been archived by the owner on Oct 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
tmdb.py
162 lines (130 loc) · 4.21 KB
/
tmdb.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
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#encoding:utf-8
#author:dbr/Ben
#project:themoviedb
"""An interface to the themoviedb.org API
"""
__author__ = "dbr/Ben"
__version__ = "0.1"
config = {}
config['apikey'] = "fake" #FIXME: get real API key
config['debug'] = True
config['urls'] = {}
config['urls']['movie.search'] = "http://api.themoviedb.org/2.0/Movie.search?title=%%s&api_key=%(apikey)s" % (config)
import urllib
try:
import xml.etree.cElementTree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree
if config['debug']:
from stub_urllib import stub_urllib
urllib = stub_urllib()
class TmdBaseError(Exception): pass
class TmdHttpError(TmdBaseError): pass
class TmdXmlError(TmdBaseError): pass
class XmlHandler:
def __init__(self, url):
self.url = url
def _grabUrl(self, url):
try:
urlhandle = urllib.urlopen(url)
except IOError:
raise TmdHttpError(errormsg)
return urlhandle.read()
def getEt(self):
xml = self._grabUrl(self.url)
try:
et = ElementTree.fromstring(xml)
except SyntaxError, errormsg:
raise TmdXmlError(errormsg)
return et
class SearchResults(list):
def __init__(self):
super(SearchResults, self).__init__()
def __repr__(self):
return "<Search results: %s>" % (list.__repr__(self))
class Movie(dict):
def __repr__(self):
return "<Movie: %s>" % self.get("title")
# return "<Movie: %s>" % (dict.__repr__(self))
class MovieAttribute(dict):
pass
class Poster(MovieAttribute):
"""Stores poster image URLs, each size is under the approriate dict key.
Common sizes are: cover, mid, original, thumb
"""
def __repr__(self):
return "<%s with sizes %s>" % (
self.__class__.__name__,
", ".join(
["'%s'" % x for x in sorted(self.keys())]
)
)
def set(self, poster_et):
"""Takes an elementtree Element ('poster') and stores the poster,
using the size as the dict key.
For example:
<backdrop size="original">
http://example.com/poster_original.jpg
</backdrop>
..becomes:
poster['original'] = 'http://example.com/poster_original.jpg'
"""
size = poster_et.get("size")
value = poster_et.text
self[size] = value
def largest(self):
"""Attempts to return largest image.
"""
for cur_size in ["original", "mid", "cover", "thumb"]:
if cur_size in self:
return self[cur_size]
class Backdrop(Poster):
"""Stores backdrop image URLs, each size under the approriate dict key.
Common sizes are: mid, original, thumb
"""
pass
class MovieDb:
def __init__(self):
pass
def _parseMovie(self, movie_element):
cur_movie = Movie()
cur_poster = Poster()
cur_backdrop = Backdrop()
for item in movie_element.getchildren():
if item.tag.lower() == "poster":
cur_poster.set(item)
elif item.tag.lower() == "backdrop":
cur_backdrop.set(item)
else:
cur_movie[item.tag] = item.text
cur_movie['poster'] = cur_poster
cur_movie['backdrop'] = cur_backdrop
return cur_movie
def search(self, title):
url = config['urls']['movie.search'] % (title)
etree = XmlHandler(url).getEt()
search_results = SearchResults()
for cur_result in etree.find("moviematches").findall("movie"):
cur_movie = self._parseMovie(cur_result)
search_results.append(cur_movie)
return search_results
def search(name = None):
"""Searches for a film by its title.
Returns SearchResults (a list) containing all matches (Movie instances)
Wraps MovieDb.search method, so you can do
>>> import tmd
>>> tmd.search("A title")
"""
mdb = MovieDb()
return mdb.search(name)
def main():
results = search("transformers")
film = results[0]
print(film.keys())
print(film['id'])
print(film['title'])
print(film['short_overview'])
print(film['backdrop'].largest())
if __name__ == '__main__':
main()