Ticket #6245: python-bindings.patch

File python-bindings.patch, 6.7 KB (added by jyavenard@…, 15 years ago)

Mods for new python bindings API

  • bindings/python/MythTV/MythVideo.py

     
    11"""
    2 Provides the MythVideo class with convinience methods to access the MythTV
     2Provides the MythVideo class with convenient methods to access the MythTV
    33MythVideo database.
    44"""
    55
     
    3636                                c2 = self.db.cursor()
    3737                                c2.execute("""DELETE FROM videometadata WHERE intid = %s""", (intid,))
    3838                                c2.close()
     39                                #Some additional cleanup
     40                                #Remove cross-references in cast, country, genres
     41                                self.cleanGenres(intid)
     42                                self.cleanCountry(intid)
     43                                self.cleanCast(intid)
     44                               
    3945                        row = c.fetchone()
    4046                c.close()
    4147
     
    4349                """
    4450                Find the id of the given genre from MythDB.
    4551
    46                 If the genre does not exist, insert it and return its id.
     52                If the category does not exist, insert it and return its id.
     53                This function should have been called getCategoryId
    4754                """
    4855                c = self.db.cursor()
    4956                c.execute("SELECT intid FROM videocategory WHERE lower(category) = %s", (genre_name,))
     
    6774
    6875                If the cast does not exist, insert it and return its id.
    6976                """
    70                 c = self.db.cursor()
    71                 # print "SELECT intid FROM videocast WHERE lower(cast) = '%s'" % (cast_name,)
    72                 c.execute("SELECT intid FROM videocast WHERE lower(cast) = %s", (cast_name,))
    73                 row = c.fetchone()
    74                 c.close()
     77                return self.setFieldId('cast', cast_name)
    7578
    76                 if row is not None:
    77                         # print "getCastId %s %s" % (cast_name, row[0])
    78                         return row[0]
    79 
    80                 # Insert a new cast.
    81                 c = self.db.cursor()
    82                 c.execute("INSERT INTO videocast(cast) VALUES (%s)", (cast_name,))
    83                 #print "INSERT INTO videocast(cast) VALUES ('%s')" % (cast_name,)
    84                 c.close()
    85 
    86                 c = self.db.cursor()
    87                 c.execute("SELECT intid FROM videocast WHERE lower(cast) = %s", (cast_name,))
    88                 row = c.fetchone()
    89                 c.close()
    90 
    91                 return row[0]
    92 
    9379        def setCast(self, cast_name, idvideo):
    9480                """
    9581                Insert the cast_name into videometadatacast if it does already exist.
     
    9783                If the cast does not exist, insert it and return its id.
    9884                """
    9985
    100                 idcast = self.getCastId(cast_name);
     86                return self.setField('cast', cast_name, idvideo)
    10187
    102                 c = self.db.cursor()
    103                 c.execute("SELECT * FROM videometadatacast WHERE idvideo = %s AND idcast = %s", (idvideo,idcast))
    104                 row = c.fetchone()
    105                 c.close()
    106 
    107                 if row is None:
    108                         # Insert a new cast.
    109                         c = self.db.cursor()
    110                         c.execute("INSERT INTO videometadatacast VALUES (%s,%s)", (idvideo,idcast))
    111                         #print "INSERT INTO videometadatacast VALUES (%s,%s)" % (idvideo,idcast)
    112                         c.close()
    113 
    11488        def getMetadataId(self, videopath):
    11589                """
    116                 Finds the MythVideo metadata id for the given video path from the MythDB, if any.
    117 
    118                 Returns None if no metadata was found.
     90                Insert the idvideo file in given cast list if it does already exist.
     91                Cast will be created if it doesn't exist return its id.
    11992                """
    12093                c = self.db.cursor()
    12194                c.execute("""
     
    194167                        c.execute(sql, sql_values)
    195168                        c.close()
    196169
     170        def getCategoryId(self, category_name):
     171                """
     172                Find the id of the given category from MythDB.
     173
     174                If the category does not exist, insert it and return its id.
     175                """
     176                c = self.db.cursor()
     177                c.execute("SELECT intid FROM videocategory WHERE lower(category) = %s", (category_name,))
     178                row = c.fetchone()
     179                c.close()
     180
     181                if row is not None:
     182                        return row[0]
     183
     184                # Insert a new genre.
     185                c = self.db.cursor()
     186                c.execute("INSERT INTO videocategory(category) VALUES (%s)", (category_name.capitalize(),))
     187                newid = c.lastrowid
     188                c.close()
     189
     190                return newid
     191
     192        def getFieldId(self, field, name):
     193                """
     194                Find the id of the given name from MythDB.
     195
     196                If the name does not exist, insert it and return its id.
     197                """
     198                c = self.db.cursor()
     199                # print ""SELECT intid FROM video%s WHERE lower(%s) = %%s" % (field, field), (name,)
     200                c.execute("SELECT intid FROM video%s WHERE lower(%s) = %%s" % (field, field), (name,))
     201                row = c.fetchone()
     202                c.close()
     203
     204                if row is not None:
     205                        return row[0]
     206
     207                # Insert a new cast.
     208                c = self.db.cursor()
     209                c.execute("INSERT INTO video%s(%s) VALUES (%%s)" % (field, field), (name,))
     210                #print "INSERT INTO video%s(%s) VALUES (%%s)" % (field, field), (name,)
     211                c.close()
     212
     213                c = self.db.cursor()
     214                c.execute("SELECT intid FROM video%s WHERE lower(%s) = %%s" % (field, field), (name,))
     215                row = c.fetchone()
     216                c.close()
     217
     218                return row[0]
     219       
     220        def setField(self, field, name, idvideo):
     221                """
     222                Insert the name into videometadata"field" if it does already exist.
     223
     224                If the name does not exist, insert it.
     225                """
     226
     227                id = self.getFieldId(field, name);
     228
     229                c = self.db.cursor()
     230                c.execute("SELECT * FROM videometadata%s WHERE idvideo = %%s AND id%s = %%s" % (field,field), (idvideo, id))
     231                row = c.fetchone()
     232                c.close()
     233
     234                if row is None:
     235                        # Insert a new cast.
     236                        c = self.db.cursor()
     237                        c.execute("INSERT INTO videometadata%s VALUES (%%s,%%s)" % field, (idvideo,id))
     238                        #print "INSERT INTO videometadata%s VALUES (%s,%s)" % field, (idvideo, id)
     239                        c.close()
     240               
     241                return id
     242       
     243
     244        def getGenresId(self, genre_name):
     245                """
     246                Find the id of the given cast from MythDB.
     247
     248                If the genre does not exist, insert it and return its id.
     249                """
     250                return self.getFieldId('genre', genre_name.capitalize())
     251
     252        def setGenres(self, genre_name, idvideo):
     253                """
     254                Insert the idvideo file in given genre list if it does already exist.
     255                Genre will be created if it doesn't exist return its id.
     256                """
     257
     258                return self.setField('genre', genre_name.capitalize(), idvideo)
     259
     260        def getCountryId(self, country_name):
     261                """
     262                Find the id of the given country from MythDB.
     263
     264                If the country does not exist, insert it and return its id.
     265                """
     266                return self.getFieldId('country', country_name)
     267
     268        def setCountry(self, country_name, idvideo):
     269                """
     270                Insert the idvideo file in given country list if it does already exist.
     271                Country will be created if it doesn't exist return its id.
     272                """
     273
     274                return self.setField('country', country_name, idvideo)
     275               
     276        def cleanGenres(self, idvideo):
     277                """
     278                Remove all cross-references of video from genres list.
     279                """
     280                c = self.db.cursor()
     281                c.execute("""DELETE FROM videometadatagenre WHERE idvideo = %s""", (idvideo,))
     282                c.close()
     283
     284        def cleanCountry(self, idvideo):
     285                """
     286                Remove all cross-references of video from country list.
     287                """
     288                c = self.db.cursor()
     289                c.execute("""DELETE FROM videometadatacountry WHERE idvideo = %s""", (idvideo,))
     290                c.close()
     291
     292        def cleanCast(self, idvideo):
     293                """
     294                Remove all cross-references of video from cast list.
     295                """
     296                c = self.db.cursor()
     297                c.execute("""DELETE FROM videometadatacast WHERE idvideo = %s""", (idvideo,))
     298                c.close()
     299               
    197300# vim: ts=4 sw=4: