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
32 log = logging.getLogger("Griffith")
35 Supports importing videodb.xml exported as single file from Kodi/XBMC.
37 http://kodi.wiki/view/Import-export_library
38 http://kodi.wiki/view/HOW-TO:Backup_the_video_library
40 See lib/plugins/imp/__init__.py for workflow how importer invokes methods from importer plugins
42 class ImportPlugin(IP):
43 description = 'Kodi/XBMC MovieDB importer'
44 author = 'Elan Ruusamäe'
45 email = 'glen@pld-linux.org'
47 file_filters = 'videodb.xml'
56 # used by get_movie_details method
57 # griffith field => kodi field
58 # griffith field is actual SQL column in 'movies' table
61 'o_title': 'originaltitle',
66 'director': 'director',
69 'classification': 'mpaa',
70 # while the trailer field exists, it is not useful for griffith
71 # as it's something like: "plugin://plugin.video.youtube/?action=play_video&videoid=..."
72 # however youtube urls can be probably fixed.
76 # rest of the stuff to insert into notes field
79 _('Play count'): 'playcount',
80 _('Date added'): 'dateadded',
81 _('Last played'): 'lastplayed',
82 _('Collection'): 'set',
83 _('Premiered'): 'premiered',
88 if not IP.initialize(self):
93 def set_source(self, name):
94 IP.set_source(self, name)
96 self.fileversion = self.read_fileversion()
97 if self.fileversion == None:
98 gutils.error(_('The format of the file is not supported.'))
103 """clear plugin state before next source file"""
107 self.fileversion = None
111 os.unlink(self.poster_file)
112 self.poster_file = None
115 """close all resources"""
118 def read_fileversion(self):
121 self.xml = etree.parse(self.filename)
122 version = self.xml.xpath('/videodb/version')[0].text
125 log.info('Found file version %s' % version)
128 def count_movies(self):
129 """Returns number of movies in file which is about to be imported"""
131 log.error('No XML object')
137 count = int(self.xml.xpath('count(/videodb/movie)'))
141 log.info('%s movies for import' % count)
144 def get_movie_details(self):
145 """Returns dictionary with movie details"""
147 log.error('XML not opened')
151 self.items = self.xml.xpath('/videodb/movie')
154 # don't bother for empty db (shouldn't happen really)
155 if not self.items or len(self.items) < 1:
159 if self.itemindex >= len(self.items):
162 item = self.items[self.itemindex]
166 for k,v in self.field_map.items():
167 details[k] = item.findtext(v)
169 # if playcount set, means it's seen
170 details['seen'] = int(item.find('playcount').text) > 0
172 # genre can be multiple items, join by comma
173 details['genre'] = ', '.join(n.text for n in item.findall('genre'))
175 # build text for 'cast' field
177 for actor in item.findall('actor'):
178 cast.append('%s as %s' % (actor.findtext('name'), actor.findtext('role')))
181 details['cast'] = "\n".join(cast)
183 # put rest of information into notes field
185 for k,v in self.notes_map.items():
188 notes.append('%s: %s' % (k, v))
190 # credits can have multiple values, handle separately
191 credits = ', '.join(n.text for n in item.findall('credits'))
193 notes.append(_('Credits: %s') % credits)
196 details['notes'] = "\n".join(notes)
199 # take first <thumb aspect="poster"> element
200 posters = item.xpath('thumb[@aspect="poster"]')
202 self.poster_file = self.grab_url(posters[0].get('preview'), prefix = 'poster_', suffix = '.jpg')
203 details['image'] = self.poster_file
205 # increment for next iteration
206 self.itemindex = self.itemindex + 1
210 # grab url, return temp filename with remote file contents
211 # XXX could not figure out how to use griffith own downloader with ui interaction, etc
212 # XXX: grabbing urls while processing import xml blocks the ui
213 def grab_url(self, url, prefix = None, suffix=None):
214 log.debug("Downloading: %s" % url)
215 (fd, local_file) = tempfile.mkstemp(suffix=suffix, prefix=prefix)
218 os.write(fd, f.read())
222 log.error("HTTP Error: %s: %s" % (e.code, url))
225 log.error("URL Error: %s: %s" % (e.reason, url))
230 # we get here with an exception, cleanup and return None
231 os.unlink(local_file)