]> git.pld-linux.org Git - packages/griffith.git/blobdiff - Kodi.py
use TempFileCleanup class to cleanup poster files after use
[packages/griffith.git] / Kodi.py
diff --git a/Kodi.py b/Kodi.py
index 2242f3e5c3165237e8093d0dc78e76c0278b5796..98f1566f92d3f34a50bd97dc92cdf18d150cd65a 100644 (file)
--- a/Kodi.py
+++ b/Kodi.py
@@ -24,14 +24,12 @@ __revision__ = '$Id: $'
 from plugins.imp import ImportPlugin as IP
 import os
 import gutils
-import string
-from xml.dom import minidom, Node
 from lxml import etree
-import zipfile
-from shutil import rmtree
-from tempfile import mkdtemp
-
+import tempfile
+from urllib2 import urlopen, URLError, HTTPError
+from movie import TempFileCleanup
 import logging
+
 log = logging.getLogger("Griffith")
 
 """
@@ -54,6 +52,7 @@ class ImportPlugin(IP):
     xml          = None
     items        = None
     itemindex    = 0
+    poster_file  = None
 
     # used by get_movie_details method
     # griffith field => kodi field
@@ -62,22 +61,36 @@ class ImportPlugin(IP):
         'title': 'title',
         'o_title': 'originaltitle',
         'year': 'year',
-        'runtime': 'runtime', # may need int cast
+        'runtime': 'runtime',
         'rating': 'rating',
         'plot': 'plot',
         'director': 'director',
         'studio': 'studio',
         'country': 'country',
+        'classification': 'mpaa',
         # while the trailer field exists, it is not useful for griffith
         # as it's something like: "plugin://plugin.video.youtube/?action=play_video&videoid=..."
         # however youtube urls can be probably fixed.
         'trailer': 'trailer',
     }
 
+    # rest of the stuff to insert into notes field
+    notes_map = {
+        _('Id') : 'id',
+        _('Play count'): 'playcount',
+        _('Date added'): 'dateadded',
+        _('Last played'): 'lastplayed',
+        _('Collection'): 'set',
+        _('Premiered'): 'premiered',
+        _('Aired'): 'aired',
+    }
+
     def initialize(self):
         if not IP.initialize(self):
             return False
         self.edit = False
+        self.tempfiles = TempFileCleanup()
+
         return True
 
     def set_source(self, name):
@@ -97,9 +110,13 @@ class ImportPlugin(IP):
             self.fileversion = None
             self.items = None
             self.itemindex = 0
+            if self.poster_file:
+                os.unlink(self.poster_file)
+                self.poster_file = None
 
     def destroy(self):
         """close all resources"""
+        self.tempfiles = None
         IP.destroy(self)
 
     def read_fileversion(self):
@@ -159,7 +176,62 @@ class ImportPlugin(IP):
         # genre can be multiple items, join by comma
         details['genre'] = ', '.join(n.text for n in item.findall('genre'))
 
+        # build text for 'cast' field
+        cast = []
+        for actor in item.findall('actor'):
+            cast.append('%s as %s' % (actor.findtext('name'), actor.findtext('role')))
+
+        if cast:
+            details['cast'] = "\n".join(cast)
+
+        # put rest of information into notes field
+        notes = []
+        for k,v in self.notes_map.items():
+            v = item.findtext(v)
+            if v:
+                notes.append('%s: %s' % (k, v))
+
+        # credits can have multiple values, handle separately
+        credits = ', '.join(n.text for n in item.findall('credits'))
+        if credits:
+            notes.append(_('Credits: %s') % credits)
+
+        if notes:
+            details['notes'] = "\n".join(notes)
+
+        # handle poster
+        # take first <thumb aspect="poster"> element
+        posters = item.xpath('thumb[@aspect="poster"]')
+        if posters:
+            self.poster_file = self.grab_url(posters[0].get('preview'), prefix = 'poster_', suffix = '.jpg')
+            details['image'] = self.poster_file
+
         # increment for next iteration
         self.itemindex = self.itemindex + 1
 
         return details
+
+    # grab url, return temp filename with remote file contents
+    # XXX could not figure out how to use griffith own downloader with ui interaction, etc
+    # XXX: grabbing urls while processing import xml blocks the ui
+    def grab_url(self, url, prefix = None, suffix=None):
+        (fd, local_file) = tempfile.mkstemp(suffix=suffix, prefix=prefix)
+        log.debug("Downloading: %s as %s" % (url, local_file))
+        try:
+            f = urlopen(url)
+            os.write(fd, f.read())
+            os.close(fd)
+
+        except HTTPError, e:
+            log.error("HTTP Error: %s: %s" % (e.code, url))
+            return None
+        except URLError, e:
+            log.error("URL Error: %s: %s" % (e.reason, url))
+            return None
+        else:
+            self.tempfiles._tempfiles.append(local_file)
+            return local_file
+
+        # we get here with an exception, cleanup and return None
+        os.unlink(local_file)
+        return None
This page took 0.087806 seconds and 4 git commands to generate.