1# -*- coding: utf-8 -*-
2
3__revision__ = '$Id$'
4
5# Copyright (c) 2009
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
24from plugins.imp import ImportPlugin as IP
25import gutils
26import string
27from xml.dom import minidom, Node
28
29import logging
30log = logging.getLogger("Griffith")
31
32
33class ImportPlugin(IP):
34    description  = 'Personal Video Database (v0.9.9.x)'
35    author       = 'Michael Jahn'
36    email        = 'griffith@griffith.cc'
37    version      = '1.0'
38    file_filters = '*.[xX][mM][lL]'
39    mime_types   = None
40
41    fileversion  = None
42    filedom      = None
43    items        = None
44    itemindex    = 0
45
46    def initialize(self):
47        if not IP.initialize(self):
48            return False
49        self.edit = False
50        return True
51
52    def set_source(self, name):
53        IP.set_source(self, name)
54        self.filename = name
55        self.fileversion = self.read_fileversion()
56        if self.fileversion == None:
57            gutils.error(_('The format of the file is not supported.'))
58            return False
59        return True
60
61    def count_movies(self):
62        """Returns number of movies in file which is about to be imported"""
63        count = 0
64        if self.filedom:
65            try:
66                xmlElement = self.filedom.getElementsByTagName('xml')[0]
67                for element in xmlElement.childNodes:
68                    if element.nodeType == Node.ELEMENT_NODE and element.nodeName == 'viddb':
69                        for viddbElement in element.childNodes:
70                            if viddbElement.nodeType == Node.ELEMENT_NODE and viddbElement.nodeName == 'movies':
71                                count = int(viddbElement.childNodes[0].data.strip())
72                                break
73                        break
74            except:
75                log.exception('')
76        else:
77            log.error('Personal Video Database Import: No filedom object.')
78        log.info('Personal Video Database Import: %s movies for import' % count)
79        return count
80
81    def get_movie_details(self):
82        """Returns dictionary with movie details"""
83        if not self.filedom:
84            self.filedom = minidom.parse(self.filename)
85        if not self.items:
86            xmlElement = self.filedom.getElementsByTagName('xml')[0]
87            viddbElement = self.filedom.getElementsByTagName('viddb')[0]
88            self.items = viddbElement.childNodes
89            self.itemindex = 0
90        if not self.items or len(self.items) < 1:
91            return None
92        if len(self.items) <= self.itemindex:
93            return None
94        item = self.items[self.itemindex]
95        while not (item.nodeType == Node.ELEMENT_NODE and item.nodeName == 'movie') and len(self.items) > self.itemindex + 1:
96            self.itemindex = self.itemindex + 1
97            item = self.items[self.itemindex]
98        if len(self.items) <= self.itemindex:
99            return None
100        if not len(item.childNodes):
101            return None
102
103        details = {}
104        try:
105            for node in item.childNodes:
106                if node.nodeType == Node.ELEMENT_NODE and len(node.childNodes) > 0:
107                    if node.nodeName == 'title':
108                        details['title'] = node.childNodes[0].data.strip()
109                    elif node.nodeName == 'origtitle':
110                        details['o_title'] = node.childNodes[0].data.strip()
111                    elif node.nodeName == 'year':
112                        details['year'] = node.childNodes[0].data.strip()
113                    elif node.nodeName == 'genre':
114                        details['genre'] = node.childNodes[0].data.strip()
115                    elif node.nodeName == 'country':
116                        details['country'] = node.childNodes[0].data.strip()
117                    elif node.nodeName == 'studio':
118                        details['studio'] = node.childNodes[0].data.strip()
119                    elif node.nodeName == 'director':
120                        details['director'] = node.childNodes[0].data.strip()
121                    elif node.nodeName == 'actors':
122                        details['cast'] = string.replace(node.childNodes[0].data.strip(), ', ', '\n')
123                    elif node.nodeName == 'description':
124                        details['plot'] = node.childNodes[0].data.strip()
125                    elif node.nodeName == 'length':
126                        details['runtime'] = node.childNodes[0].data.strip()
127                    elif node.nodeName == 'type':
128                        # setting medium_id to string mediumname; mapping is done in base class
129                        details['medium_id'] = node.childNodes[0].data.strip()
130                    elif node.nodeName == 'count':
131                        details['media_num'] = node.childNodes[0].data.strip()
132                    elif node.nodeName == 'videocodec':
133                        # setting vcodec_id to string codecname; mapping is done in base class
134                        details['vcodec_id'] = node.childNodes[0].data
135                    elif node.nodeName == 'comment':
136                        details['notes'] = node.childNodes[0].data.strip()
137                    elif node.nodeName == 'mpaa':
138                        details['classification'] = node.childNodes[0].data.strip()
139                    elif node.nodeName == 'scenario':
140                        details['screenplay'] = node.childNodes[0].data.strip()
141                    elif node.nodeName == 'tags':
142                        details['tags'] = string.split(node.childNodes[0].data.strip(), ', ')
143                    elif node.nodeName == 'url':
144                        details['o_site'] = node.childNodes[0].data.strip()
145                    elif node.nodeName == 'path':
146                        details['trailer'] = node.childNodes[0].data.strip()
147                    elif node.nodeName == 'barcode':
148                        details['barcode'] = node.childNodes[0].data.strip()
149                    elif node.nodeName == 'poster':
150                        details['poster'] = node.childNodes[0].data.strip()
151                    elif node.nodeName == 'num':
152                        details['number'] = node.childNodes[0].data.strip()
153                    elif node.nodeName == 'rating':
154                        try:
155                            details['rating'] = round(float(node.childNodes[0].data.strip().replace(',', '.')), 0)
156                        except:
157                            pass
158        except EOFError:
159            details = None
160        except Exception as e:
161            log.exception('')
162            details = None
163        self.itemindex = self.itemindex + 1
164        return details
165
166    def clear(self):
167        """clear plugin before next source file"""
168        IP.clear(self)
169        if self.filedom:
170            self.filedom.unlink()
171            self.filedom = None
172            self.fileversion = None
173            self.items = None
174            self.itemindex = 0
175
176    def destroy(self):
177        """close all resources"""
178        IP.destroy(self)
179
180    def read_fileversion(self):
181        version = None
182        self.filedom = minidom.parse(self.filename)
183        try:
184            xmlElement = self.filedom.getElementsByTagName('xml')[0]
185            viddbElement = self.filedom.getElementsByTagName('viddb')[0]
186            version = 0.9
187        except Exception as e:
188            log.error(str(e))
189            self.filedom.unlink()
190            self.filedom = None
191        log.info('Personal Video Database Import: Found file version %s' % version)
192        return version
193