Ticket #4440: amc2Myth.patch

File amc2Myth.patch, 5.6 KB (added by Chris Browet <cbro@…>, 12 years ago)
  • mythvideo/mythvideo/scripts/MythTV.py

     
    319319                                log.info("%s not exist, removing metadata..." % filename)
    320320                                c2 = self.db.cursor()
    321321                                c2.execute("""DELETE FROM videometadata WHERE intid = %s""", (intid,))
     322                                c2.execute("""DELETE FROM videometadatagenre WHERE idvideo = %s""", (intid,))
    322323                                c2.close()
    323324                        row = c.fetchone()
    324325                c.close()
    325326
    326         def getGenreId(self, genre_name):
     327        def getCategoryId(self, genre_name):
    327328                """
    328329                Find the id of the given genre from MythDB.
    329330               
     
    345346               
    346347                return newid
    347348
     349        def getGenreId(self, genre_name):
     350                """
     351                Find the id of the given genre from MythDB.
     352               
     353                If the genre does not exist, insert it and return its id.
     354                """
     355                c = self.db.cursor()
     356                c.execute("SELECT intid FROM videogenre WHERE lower(genre) = %s", (genre_name.lower(),))
     357                row = c.fetchone()
     358                c.close()
     359               
     360                if row is not None:
     361                        return row[0]
     362               
     363                # Insert a new genre.
     364                c = self.db.cursor()
     365                c.execute("INSERT INTO videogenre(genre) VALUES (%s)", (genre_name.capitalize(),))
     366                newid = c.lastrowid
     367                c.close()
     368               
     369                return newid
     370
     371        def setGenreId(self, genreId, id):
     372                c = self.db.cursor()
     373                c.execute("INSERT INTO videometadatagenre(idvideo,idgenre) VALUES (%s,%s)", (id,genreId,))
     374                c.close()
     375
    348376        def getMetadataId(self, videopath):
    349377                """
    350378                Finds the MythVideo metadata id for the given video path from the MythDB, if any.
     
    426454                        sql_values.append(id)
    427455                        c.execute(sql, sql_values)
    428456                        c.close()
     457                        return id
    429458
    430459class Recorder:
    431460        def __str__(self):
  • mythvideo/mythvideo/scripts/amc2Myth.py

     
     1#!/usr/bin/python
     2"""
     3This Python script convert an Ant Media Catalog (antp.be) to metadata for the MytVideo plugin,
     4including covers.
     5
     6Your AMC database must be saved in XML format for this to work.
     7
     8If you specify, in AMC, in the Media Label field, a filename starting with FILE:,
     9the script will consider the movie as online and will add/update
     10the MythVideo metadata for the given filename.
     11
     12Otherwise, a dummy ".dvd" file will be created (containing the xml informations from AMC).
     13For them to appear in MythVideo, you have to define the .dvd extensions in the configuration.
     14You are then free to use whatever script to instruct the mounting of your DVD.
     15
     16Please adjust the fldrXXX variable according your setup.
     17
     18Written by Chris Browet (2008)
     19"""
     20
     21import sys
     22import xml.dom.ext
     23import xml.dom.minidom
     24from xml.dom.minidom import Node
     25from Numeric import *
     26import os
     27import shutil
     28
     29fldrDVD = "/var/video/db/DVD/"
     30fldrANT = "/var/video/AMC/"
     31fileANT = "cbro.xml"
     32
     33def nukedir(dir):
     34    if dir[-1] == os.sep: dir = dir[:-1]
     35    files = os.listdir(dir)
     36    for file in files:
     37        if file == '.' or file == '..': continue
     38        path = dir + os.sep + file
     39        if os.path.isdir(path):
     40            nukedir(path)
     41        else:
     42            os.unlink(path)
     43    #os.rmdir(dir)
     44
     45def create_dvd(movie):
     46   global mythvideo
     47
     48   location = movie.getAttribute("MediaLabel")
     49   dvdFn = ""
     50
     51   if location.upper().startswith("FILE:"):
     52      dvdFn = location[4:]
     53   else:
     54      doc = xml.dom.minidom.Document()
     55      root = doc.createElement("AntMovieCatalog")
     56      root.appendChild(movie.cloneNode(True))
     57      doc.appendChild(root)
     58
     59      num = movie.getAttribute("Number")
     60      dvdFn = fldrDVD + num + ".dvd"
     61
     62      file_object = open(dvdFn, "w")
     63      xml.dom.ext.PrettyPrint(doc, file_object)
     64      file_object.close()
     65   
     66   mythId = mythvideo.getMetadataId(dvdFn)
     67
     68   data = {}
     69   data["title"] = movie.getAttribute("FormattedTitle")
     70   data["director"] = movie.getAttribute("Director")
     71   data["plot"] = movie.getAttribute("Description")
     72   data["inetref"] = movie.getAttribute("Number")
     73   data["year"] = movie.getAttribute("Year")
     74   data["userrating"] = movie.getAttribute("Rating")
     75   data["length"] = movie.getAttribute("Length")
     76   data["showlevel"] = 1
     77   data["filename"] = dvdFn
     78   data["childid"] = -1
     79   data["playcommand"] = ""
     80   #data["category"] = mythvideo.getGenreId(movie.getAttribute("Category"))
     81   data["rating"] = "NR"   
     82
     83
     84   cover = movie.getAttribute("Picture")
     85   if cover != "":
     86      data["coverfile"] = fldrDVD + cover
     87      shutil.copyfile(fldrANT + cover, fldrDVD + cover)
     88   
     89   mythId = mythvideo.setMetadata(data, mythId)
     90   
     91   tokens = movie.getAttribute("Category").split(",")
     92   for genre in tokens:
     93      if genre != "":
     94         mythvideo.setGenreId(mythvideo.getGenreId(genre.strip()), mythId)
     95
     96     
     97try:
     98      # If found, we can insert data directly to MythDB
     99      from MythTV import MythDB, MythVideo
     100      mythdb = MythDB()
     101      mythvideo = MythVideo()
     102except:
     103      print "MythTV module cannot be initialized, MythDB importing disabled."
     104      mythdb = None
     105      mythvideo = None
     106      sys.exit(1)
     107
     108doc = xml.dom.minidom.parse(fldrANT + fileANT)
     109 
     110nukedir(fldrDVD)
     111mythvideo.pruneMetadata()
     112
     113for node in doc.getElementsByTagName("Movie"):
     114   create_dvd(node)
     115
     116   #Otitle = node.getAttribute("OriginalTitle")
     117   #Ftitle = node.getAttribute("FormattedTitle")
     118   #    print Ftitle
     119
     120