]> git.pld-linux.org Git - packages/griffith.git/blob - Kodi.py
import cast field
[packages/griffith.git] / Kodi.py
1 # -*- coding: utf-8 -*-
2
3 __revision__ = '$Id: $'
4
5 # Copyright (c) 2015
6
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.
11 #
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.
16 #
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
20
21 # You may use and distribute this software under the terms of the
22 # GNU General Public License, version 2 or later
23
24 from plugins.imp import ImportPlugin as IP
25 import os
26 import gutils
27 import string
28 from xml.dom import minidom, Node
29 from lxml import etree
30 import zipfile
31 from shutil import rmtree
32 from tempfile import mkdtemp
33
34 import logging
35 log = logging.getLogger("Griffith")
36
37 """
38 Supports importing videodb.xml exported as single file from Kodi/XBMC.
39
40 http://kodi.wiki/view/Import-export_library
41 http://kodi.wiki/view/HOW-TO:Backup_the_video_library
42
43 See lib/plugins/imp/__init__.py for workflow how importer invokes methods from importer plugins
44 """
45 class ImportPlugin(IP):
46     description  = 'Kodi/XBMC MovieDB importer'
47     author       = 'Elan Ruusamäe'
48     email        = 'glen@pld-linux.org'
49     version      = '1.0'
50     file_filters = 'videodb.xml'
51     mime_types   = None
52
53     fileversion  = None
54     xml          = None
55     items        = None
56     itemindex    = 0
57
58     # used by get_movie_details method
59     # griffith field => kodi field
60     # griffith field is actual SQL column in 'movies' table
61     field_map = {
62         'title': 'title',
63         'o_title': 'originaltitle',
64         'year': 'year',
65         'runtime': 'runtime',
66         'rating': 'rating',
67         'plot': 'plot',
68         'director': 'director',
69         'studio': 'studio',
70         'country': 'country',
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.
74         'trailer': 'trailer',
75     }
76
77     def initialize(self):
78         if not IP.initialize(self):
79             return False
80         self.edit = False
81         return True
82
83     def set_source(self, name):
84         IP.set_source(self, name)
85         self.filename = name
86         self.fileversion = self.read_fileversion()
87         if self.fileversion == None:
88             gutils.error(_('The format of the file is not supported.'))
89             return False
90         return True
91
92     def clear(self):
93         """clear plugin state before next source file"""
94         IP.clear(self)
95         if self.xml:
96             self.xml = None
97             self.fileversion = None
98             self.items = None
99             self.itemindex = 0
100
101     def destroy(self):
102         """close all resources"""
103         IP.destroy(self)
104
105     def read_fileversion(self):
106         version = None
107         try:
108             self.xml = etree.parse(self.filename)
109             version = self.xml.xpath('/videodb/version')[0].text
110         except Exception, e:
111             log.error(str(e))
112         log.info('Found file version %s' % version)
113         return version
114
115     def count_movies(self):
116         """Returns number of movies in file which is about to be imported"""
117         if not self.xml:
118             log.error('No XML object')
119             return 0
120
121         count = 0
122
123         try:
124             count = int(self.xml.xpath('count(/videodb/movie)'))
125         except Exception, e:
126             log.exception(e)
127
128         log.info('%s movies for import' % count)
129         return count
130
131     def get_movie_details(self):
132         """Returns dictionary with movie details"""
133         if not self.xml:
134             log.error('XML not opened')
135             return None
136
137         if not self.items:
138             self.items = self.xml.xpath('/videodb/movie')
139             self.itemindex = 0
140
141         # don't bother for empty db (shouldn't happen really)
142         if not self.items or len(self.items) < 1:
143             return None
144
145         # no more items
146         if self.itemindex >= len(self.items):
147             return None
148
149         item = self.items[self.itemindex]
150
151         # fill details
152         details = {}
153         for k,v in self.field_map.items():
154             details[k] = item.findtext(v)
155
156         # if playcount set, means it's seen
157         details['seen'] = int(item.find('playcount').text) > 0
158
159         # genre can be multiple items, join by comma
160         details['genre'] = ', '.join(n.text for n in item.findall('genre'))
161
162         # build text for 'cast' field
163         cast = []
164         for actor in item.findall('actor'):
165             cast.append('%s as %s' % (actor.findtext('name'), actor.findtext('role')))
166
167         if cast:
168             details['cast'] = "\n".join(cast)
169
170         # increment for next iteration
171         self.itemindex = self.itemindex + 1
172
173         return details
This page took 0.083664 seconds and 4 git commands to generate.