1 # -*- coding: utf-8 -*-
3 __revision__ = '$Id: $'
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Library General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 # You may use and distribute this software under the terms of the
22 # GNU General Public License, version 2 or later
24 from plugins.imp import ImportPlugin as IP
27 from lxml import etree
29 from urllib2 import urlopen, URLError, HTTPError
30 from movie import TempFileCleanup
33 log = logging.getLogger("Griffith")
36 Supports importing videodb.xml exported as single file from Kodi/XBMC.
38 http://kodi.wiki/view/Import-export_library
39 http://kodi.wiki/view/HOW-TO:Backup_the_video_library
41 See lib/plugins/imp/__init__.py for workflow how importer invokes methods from importer plugins
43 class ImportPlugin(IP):
44 description = 'Kodi/XBMC MovieDB importer'
45 author = 'Elan Ruusamäe'
46 email = 'glen@pld-linux.org'
48 file_filters = 'videodb.xml'
57 # used by get_movie_details method
58 # griffith field => kodi field
59 # griffith field is actual SQL column in 'movies' table
62 'o_title': 'originaltitle',
67 'director': 'director',
70 'classification': 'mpaa',
71 # while the trailer field exists, it is not useful for griffith
72 # as it's something like: "plugin://plugin.video.youtube/?action=play_video&videoid=..."
73 # however youtube urls can be probably fixed.
77 # rest of the stuff to insert into notes field
80 _('Play count'): 'playcount',
81 _('Date added'): 'dateadded',
82 _('Last played'): 'lastplayed',
83 _('Collection'): 'set',
84 _('Premiered'): 'premiered',
89 if not IP.initialize(self):
92 self.tempfiles = TempFileCleanup()
96 def set_source(self, name):
97 IP.set_source(self, name)
99 self.fileversion = self.read_fileversion()
100 if self.fileversion == None:
101 gutils.error(_('The format of the file is not supported.'))
106 """clear plugin state before next source file"""
110 self.fileversion = None
114 os.unlink(self.poster_file)
115 self.poster_file = None
118 """close all resources"""
119 self.tempfiles = None
122 def read_fileversion(self):
125 self.xml = etree.parse(self.filename)
126 version = self.xml.xpath('/videodb/version')[0].text
129 log.info('Found file version %s' % version)
132 def count_movies(self):
133 """Returns number of movies in file which is about to be imported"""
135 log.error('No XML object')
141 count = int(self.xml.xpath('count(/videodb/movie)'))
145 log.info('%s movies for import' % count)
148 def get_movie_details(self):
149 """Returns dictionary with movie details"""
151 log.error('XML not opened')
155 self.items = self.xml.xpath('/videodb/movie')
158 # don't bother for empty db (shouldn't happen really)
159 if not self.items or len(self.items) < 1:
163 if self.itemindex >= len(self.items):
166 item = self.items[self.itemindex]
170 for k,v in self.field_map.items():
171 details[k] = item.findtext(v)
173 # if playcount set, means it's seen
174 details['seen'] = int(item.find('playcount').text) > 0
176 # genre can be multiple items, join by comma
177 details['genre'] = ', '.join(n.text for n in item.findall('genre'))
179 # build text for 'cast' field
181 for actor in item.findall('actor'):
182 cast.append('%s as %s' % (actor.findtext('name'), actor.findtext('role')))
185 details['cast'] = "\n".join(cast)
187 # put rest of information into notes field
189 for k,v in self.notes_map.items():
192 notes.append('%s: %s' % (k, v))
194 # credits can have multiple values, handle separately
195 credits = ', '.join(n.text for n in item.findall('credits'))
197 notes.append(_('Credits: %s') % credits)
200 details['notes'] = "\n".join(notes)
203 # take first <thumb aspect="poster"> element
204 posters = item.xpath('thumb[@aspect="poster"]')
206 self.poster_file = self.grab_url(posters[0].get('preview'), prefix = 'poster_', suffix = '.jpg')
207 details['image'] = self.poster_file
209 # increment for next iteration
210 self.itemindex = self.itemindex + 1
214 # grab url, return temp filename with remote file contents
215 # XXX could not figure out how to use griffith own downloader with ui interaction, etc
216 # XXX: grabbing urls while processing import xml blocks the ui
217 def grab_url(self, url, prefix = None, suffix=None):
218 (fd, local_file) = tempfile.mkstemp(suffix=suffix, prefix=prefix)
219 log.debug("Downloading: %s as %s" % (url, local_file))
222 os.write(fd, f.read())
226 log.error("HTTP Error: %s: %s" % (e.code, url))
229 log.error("URL Error: %s: %s" % (e.reason, url))
232 self.tempfiles._tempfiles.append(local_file)
235 # we get here with an exception, cleanup and return None
236 os.unlink(local_file)