]> git.pld-linux.org Git - packages/griffith.git/blob - Kodi.py
Kodi import plugin
[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 See lib/plugins/imp/__init__.py for workflow how importer invokes methods from importer plugins
39 """
40 class ImportPlugin(IP):
41     description  = 'Kodi/XBMC MovieDB importer'
42     author       = 'Elan Ruusamäe'
43     email        = 'glen@pld-linux.org'
44     version      = '1.0'
45     file_filters = 'videodb.xml'
46     mime_types   = None
47
48     fileversion  = None
49     xml          = None
50     items        = None
51     itemindex    = 0
52
53     # used by get_movie_details method
54     # griffith field => kodi field
55     # griffith field is actual SQL column in 'movies' table
56     field_map = {
57         'title': 'title',
58         'o_title': 'originaltitle',
59         'year': 'year',
60         'runtime': 'runtime', # may need int cast
61         'rating': 'rating',
62         'plot': 'plot',
63         'director': 'director',
64         'studio': 'studio',
65         'country': 'country',
66         # while the trailer field exists, it is not useful for griffith
67         # as it's something like: "plugin://plugin.video.youtube/?action=play_video&videoid=..."
68         # however youtube urls can be probably fixed.
69         'trailer': 'trailer',
70     }
71
72     def initialize(self):
73         if not IP.initialize(self):
74             return False
75         self.edit = False
76         return True
77
78     def set_source(self, name):
79         IP.set_source(self, name)
80         self.filename = name
81         self.fileversion = self.read_fileversion()
82         if self.fileversion == None:
83             gutils.error(_('The format of the file is not supported.'))
84             return False
85         return True
86
87     def clear(self):
88         """clear plugin state before next source file"""
89         IP.clear(self)
90         if self.xml:
91             self.xml = None
92             self.fileversion = None
93             self.items = None
94             self.itemindex = 0
95
96     def destroy(self):
97         """close all resources"""
98         IP.destroy(self)
99
100     def read_fileversion(self):
101         version = None
102         try:
103             self.xml = etree.parse(self.filename)
104             version = self.xml.xpath('/videodb/version')[0].text
105         except Exception, e:
106             log.error(str(e))
107         log.info('Found file version %s' % version)
108         return version
109
110     def count_movies(self):
111         """Returns number of movies in file which is about to be imported"""
112         if not self.xml:
113             log.error('No XML object')
114             return 0
115
116         count = 0
117
118         try:
119             count = int(self.xml.xpath('count(/videodb/movie)'))
120         except Exception, e:
121             log.exception(e)
122
123         log.info('%s movies for import' % count)
124         return count
125
126     def get_movie_details(self):
127         """Returns dictionary with movie details"""
128         if not self.xml:
129             log.error('XML not opened')
130             return None
131
132         if not self.items:
133             self.items = self.xml.xpath('/videodb/movie')
134             self.itemindex = 0
135
136         # don't bother for empty db (shouldn't happen really)
137         if not self.items or len(self.items) < 1:
138             return None
139
140         # no more items
141         if self.itemindex >= len(self.items):
142             return None
143
144         item = self.items[self.itemindex]
145
146         # fill details
147         details = {}
148         for k,v in self.field_map.items():
149             details[k] = item.findtext(v)
150
151         # if playcount set, means it's seen
152         details['seen'] = int(item.find('playcount').text) > 0
153
154         # genre can be multiple items, join by comma
155         details['genre'] = ', '.join(n.text for n in item.findall('genre'))
156
157         # increment for next iteration
158         self.itemindex = self.itemindex + 1
159
160         return details
This page took 0.107206 seconds and 4 git commands to generate.