-
Notifications
You must be signed in to change notification settings - Fork 345
/
tmdb3.py
executable file
·184 lines (165 loc) · 7.31 KB
/
tmdb3.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# ----------------------
# Name: tmdb3.py
# Python Script
# Author: Raymond Wagner
# Purpose: This python script is intended to translate lookups between the
# TheMovieDB.org V3 API and MythTV's internal metadata format.
# http://www.mythtv.org/wiki/MythVideo_Grabber_Script_Format
# http://help.themoviedb.org/kb/api/about-3
#-----------------------
__title__ = "TheMovieDB.org V3"
__author__ = "Raymond Wagner"
__version__ = "0.3.0"
# 0.1.0 Initial version
# 0.2.0 Add language support, move cache to home directory
# 0.3.0 Enable version detection to allow use in MythTV
from MythTV.tmdb3 import searchMovie, Movie, Collection, set_key, set_cache, set_locale
from MythTV import VideoMetadata
from optparse import OptionParser
from lxml import etree
import sys
def buildSingle(inetref):
movie = Movie(inetref)
tree = etree.XML(u'<metadata></metadata>')
mapping = [['runtime', 'runtime'], ['title', 'originaltitle'],
['releasedate', 'releasedate'], ['tagline', 'tagline'],
['description', 'overview'], ['homepage', 'homepage'],
['userrating', 'userrating'], ['popularity', 'popularity'],
['budget', 'budget'], ['revenue', 'revenue']]
m = VideoMetadata()
for i,j in mapping:
if getattr(movie, j):
setattr(m, i, getattr(movie, j))
m.inetref = str(movie.id)
if movie.releasedate:
m.year = movie.releasedate.year
if movie.collection:
m.collectionref = str(movie.collection.id)
for country, release in movie.releases.items():
if release.certification:
m.certifications[country] = release.certification
for genre in movie.genres:
m.categories.append(genre.name)
for studio in movie.studios:
m.studios.append(studio.name)
for country in movie.countries:
m.countries.append(country.name)
for cast in movie.cast:
d = {'name':cast.name, 'character':cast.character, 'department':'Actors',
'job':'Actor', 'url':'http://www.themoviedb.org/people/{0}'.format(cast.id)}
if cast.profile: d['thumb'] = cast.profile.geturl()
m.people.append(d)
for crew in movie.crew:
d = {'name':crew.name, 'job':crew.job, 'department':crew.department,
'url':'http://www.themoviedb.org/people/{0}'.format(cast.id)}
if crew.profile: d['thumb'] = crew.profile.geturl()
m.people.append(d)
for backdrop in movie.backdrops:
m.images.append({'type':'fanart', 'url':backdrop.geturl(),
'thumb':backdrop.geturl(backdrop.sizes()[0])})
for poster in movie.posters:
m.images.append({'type':'coverart', 'url':poster.geturl(),
'thumb':poster.geturl(poster.sizes()[0])})
tree.append(m.toXML())
sys.stdout.write(etree.tostring(tree, encoding='UTF-8', pretty_print=True,
xml_declaration=True))
sys.exit()
def buildList(query):
# TEMPORARY FIX:
# replace all dashes from queries to work around search behavior
# as negative to all text that comes afterwards
query = query.replace('-',' ')
results = searchMovie(query)
tree = etree.XML(u'<metadata></metadata>')
mapping = [['runtime', 'runtime'], ['title', 'originaltitle'],
['releasedate', 'releasedate'], ['tagline', 'tagline'],
['description', 'overview'], ['homepage', 'homepage'],
['userrating', 'userrating'], ['popularity', 'popularity']]
count = 0
for res in results:
m = VideoMetadata()
for i,j in mapping:
if getattr(res, j):
setattr(m, i, getattr(res, j))
m.inetref = str(res.id)
if res.releasedate:
m.year = res.releasedate.year
if res.backdrop:
b = res.backdrop
m.images.append({'type':'fanart', 'url':b.geturl(),
'thumb':b.geturl(b.sizes()[0])})
if res.poster:
p = res.poster
m.images.append({'type':'coverart', 'url':p.geturl(),
'thumb':p.geturl(p.sizes()[0])})
tree.append(m.toXML())
count += 1
if count >= 60:
# page limiter, dont want to overload the server
break
sys.stdout.write(etree.tostring(tree, encoding='UTF-8', pretty_print=True,
xml_declaration=True))
sys.exit(0)
def buildCollection(inetref):
collection = Collection(inetref)
tree = etree.XML(u'<metadata></metadata>')
m = VideoMetadata()
m.collectionref = str(collection.id)
m.title = collection.name
if collection.backdrop:
b = collection.backdrop
m.images.append({'type':'fanart', 'url':b.geturl(),
'thumb':b.geturl(b.sizes()[0])})
if collection.poster:
p = collection.poster
m.images.append({'type':'coverart', 'url':p.geturl(),
'thumb':p.geturl(p.sizes()[0])})
tree.append(m.toXML())
sys.stdout.write(etree.tostring(tree, encoding='UTF-8', pretty_print=True,
xml_declaration=True))
sys.exit()
def buildVersion():
version = etree.XML(u'<grabber></grabber>')
etree.SubElement(version, "name").text = __title__
etree.SubElement(version, "author").text = __author__
etree.SubElement(version, "thumbnail").text = 'tmdb.png'
etree.SubElement(version, "command").text = 'tmdb3.py'
etree.SubElement(version, "type").text = 'movie'
etree.SubElement(version, "description").text = \
'Search and metadata downloads for themoviedb.org'
etree.SubElement(version, "version").text = __version__
sys.stdout.write(etree.tostring(version, encoding='UTF-8', pretty_print=True,
xml_declaration=True))
sys.exit(0)
def main():
set_key('c27cb71cff5bd76e1a7a009380562c62')
set_cache(engine='file', filename='~/.mythtv/pytmdb3.cache')
parser = OptionParser()
parser.add_option('-v', "--version", action="store_true", default=False,
dest="version", help="Display version and author")
parser.add_option('-M', "--movielist", action="store_true", default=False,
dest="movielist", help="Get Movies matching search.")
parser.add_option('-D', "--moviedata", action="store_true", default=False,
dest="moviedata", help="Get Movie data.")
parser.add_option('-C', "--collection", action="store_true", default=False,
dest="collectiondata", help="Get Collection data.")
parser.add_option( "-l", "--language", metavar="LANGUAGE", default=u'en',
dest="language", help="Specify language for filtering.")
opts, args = parser.parse_args()
if opts.version:
buildVersion()
if opts.language:
set_locale(language=opts.language, fallthrough=True)
if (len(args) != 1) or (args[0] == ''):
sys.stdout.write('ERROR: tmdb3.py requires exactly one non-empty argument')
sys.exit(1)
if opts.movielist:
buildList(args[0])
if opts.moviedata:
buildSingle(args[0])
if opts.collectiondata:
buildCollection(args[0])
if __name__ == '__main__':
main()