Speed up kodi database sync

This commit is contained in:
tomkat83 2016-12-04 11:56:37 +01:00
parent 97eb08d3bd
commit e4de57c753
2 changed files with 92 additions and 125 deletions

View file

@ -3,13 +3,10 @@
############################################################################### ###############################################################################
import logging import logging
import xbmc
from ntpath import dirname from ntpath import dirname
import artwork import artwork
import clientinfo from utils import kodiSQL, KODIVERSION
from utils import kodiSQL
############################################################################### ###############################################################################
@ -43,13 +40,8 @@ class GetKodiDB():
class Kodidb_Functions(): class Kodidb_Functions():
kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
def __init__(self, cursor): def __init__(self, cursor):
self.cursor = cursor self.cursor = cursor
self.clientInfo = clientinfo.ClientInfo()
self.artwork = artwork.Artwork() self.artwork = artwork.Artwork()
def pathHack(self): def pathHack(self):
@ -212,8 +204,7 @@ class Kodidb_Functions():
self.cursor.execute(query, (pathid, filename,)) self.cursor.execute(query, (pathid, filename,))
def addCountries(self, kodiid, countries, mediatype): def addCountries(self, kodiid, countries, mediatype):
if KODIVERSION > 14:
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
for country in countries: for country in countries:
query = ' '.join(( query = ' '.join((
@ -284,84 +275,74 @@ class Kodidb_Functions():
) )
self.cursor.execute(query, (idCountry, kodiid)) self.cursor.execute(query, (idCountry, kodiid))
def addPeople(self, kodiid, people, mediatype): def _getactorid(self, name):
"""
castorder = 1 Crucial für sync speed!
for person in people: """
name = person['Name']
person_type = person['Type']
thumb = person['imageurl']
# Kodi Isengard, Jarvis, Krypton
if self.kodiversion in (15, 16, 17):
query = ' '.join(( query = ' '.join((
"SELECT actor_id", "SELECT actor_id",
"FROM actor", "FROM actor",
"WHERE name = ?", "WHERE name = ?",
"COLLATE NOCASE" "COLLATE NOCASE"
)) ))
self.cursor.execute(query, (name,)) self.cursor.execute(query, (name,))
try: try:
actorid = self.cursor.fetchone()[0] actorid = self.cursor.fetchone()[0]
except TypeError: except TypeError:
# Cast entry does not exists # Cast entry does not exists
self.cursor.execute("select coalesce(max(actor_id),0) from actor") self.cursor.execute("select coalesce(max(actor_id),0) from actor")
actorid = self.cursor.fetchone()[0] + 1 actorid = self.cursor.fetchone()[0] + 1
query = "INSERT INTO actor(actor_id, name) values(?, ?)" query = "INSERT INTO actor(actor_id, name) values(?, ?)"
self.cursor.execute(query, (actorid, name)) self.cursor.execute(query, (actorid, name))
return actorid
finally: def _addPerson(self, role, person_type, actorid, kodiid, mediatype,
# Link person to content castorder):
if "Actor" in person_type: if "Actor" == person_type:
role = person.get('Role') query = '''
query = (
'''
INSERT OR REPLACE INTO actor_link( INSERT OR REPLACE INTO actor_link(
actor_id, media_id, media_type, role, cast_order) actor_id, media_id, media_type, role, cast_order)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
''' '''
) self.cursor.execute(query, (actorid, kodiid, mediatype, role,
self.cursor.execute(query, (actorid, kodiid, mediatype, role, castorder)) castorder))
castorder += 1 castorder += 1
elif "Director" == person_type:
elif "Director" in person_type: query = '''
query = (
'''
INSERT OR REPLACE INTO director_link( INSERT OR REPLACE INTO director_link(
actor_id, media_id, media_type) actor_id, media_id, media_type)
VALUES (?, ?, ?) VALUES (?, ?, ?)
''' '''
)
self.cursor.execute(query, (actorid, kodiid, mediatype)) self.cursor.execute(query, (actorid, kodiid, mediatype))
elif person_type == "Writer":
elif person_type in ("Writing", "Writer"): query = '''
query = (
'''
INSERT OR REPLACE INTO writer_link( INSERT OR REPLACE INTO writer_link(
actor_id, media_id, media_type) actor_id, media_id, media_type)
VALUES (?, ?, ?) VALUES (?, ?, ?)
''' '''
)
self.cursor.execute(query, (actorid, kodiid, mediatype)) self.cursor.execute(query, (actorid, kodiid, mediatype))
elif "Artist" == person_type:
elif "Artist" in person_type: query = '''
query = (
'''
INSERT OR REPLACE INTO actor_link( INSERT OR REPLACE INTO actor_link(
actor_id, media_id, media_type) actor_id, media_id, media_type)
VALUES (?, ?, ?) VALUES (?, ?, ?)
''' '''
)
self.cursor.execute(query, (actorid, kodiid, mediatype)) self.cursor.execute(query, (actorid, kodiid, mediatype))
return castorder
def addPeople(self, kodiid, people, mediatype):
castorder = 1
for person in people:
# Kodi Isengard, Jarvis, Krypton
if KODIVERSION > 14:
actorid = self._getactorid(person['Name'])
# Link person to content
castorder = self._addPerson(person.get('Role'),
person['Type'],
actorid,
kodiid,
mediatype,
castorder)
# Kodi Helix # Kodi Helix
else: else:
query = ' '.join(( query = ' '.join((
@ -371,22 +352,19 @@ class Kodidb_Functions():
"WHERE strActor = ?", "WHERE strActor = ?",
"COLLATE NOCASE" "COLLATE NOCASE"
)) ))
self.cursor.execute(query, (name,)) self.cursor.execute(query, (person['Name'],))
try: try:
actorid = self.cursor.fetchone()[0] actorid = self.cursor.fetchone()[0]
except TypeError: except TypeError:
# Cast entry does not exists # Cast entry does not exists
self.cursor.execute("select coalesce(max(idActor),0) from actors") self.cursor.execute("select coalesce(max(idActor),0) from actors")
actorid = self.cursor.fetchone()[0] + 1 actorid = self.cursor.fetchone()[0] + 1
query = "INSERT INTO actors(idActor, strActor) values(?, ?)" query = "INSERT INTO actors(idActor, strActor) values(?, ?)"
self.cursor.execute(query, (actorid, name)) self.cursor.execute(query, (actorid, person['Name']))
finally: finally:
# Link person to content # Link person to content
if "Actor" in person_type: if "Actor" == person['Type']:
role = person.get('Role') role = person.get('Role')
if "movie" in mediatype: if "movie" in mediatype:
@ -416,12 +394,13 @@ class Kodidb_Functions():
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
''' '''
) )
else: return # Item is invalid else:
# Item is invalid
return
self.cursor.execute(query, (actorid, kodiid, role, castorder)) self.cursor.execute(query, (actorid, kodiid, role, castorder))
castorder += 1 castorder += 1
elif "Director" in person_type: elif "Director" == person['Type']:
if "movie" in mediatype: if "movie" in mediatype:
query = ( query = (
''' '''
@ -463,7 +442,7 @@ class Kodidb_Functions():
self.cursor.execute(query, (actorid, kodiid)) self.cursor.execute(query, (actorid, kodiid))
elif person_type in ("Writing", "Writer"): elif person['Type'] == "Writer":
if "movie" in mediatype: if "movie" in mediatype:
query = ( query = (
''' '''
@ -482,29 +461,25 @@ class Kodidb_Functions():
VALUES (?, ?) VALUES (?, ?)
''' '''
) )
else: return # Item is invalid else:
# Item is invalid
return
self.cursor.execute(query, (actorid, kodiid)) self.cursor.execute(query, (actorid, kodiid))
elif "Artist" == person['Type']:
elif "Artist" in person_type:
query = ( query = (
''' '''
INSERT OR REPLACE INTO artistlinkmusicvideo( INSERT OR REPLACE INTO artistlinkmusicvideo(
idArtist, idMVideo) idArtist, idMVideo)
VALUES (?, ?) VALUES (?, ?)
''' '''
) )
self.cursor.execute(query, (actorid, kodiid)) self.cursor.execute(query, (actorid, kodiid))
# Add person image to art table # Add person image to art table
if thumb: if person['imageurl']:
arttype = person_type.lower() self.artwork.addOrUpdateArt(person['imageurl'], actorid,
person['Type'].lower(), "thumb",
if "writing" in arttype: self.cursor)
arttype = "writer"
self.artwork.addOrUpdateArt(thumb, actorid, arttype, "thumb", self.cursor)
def existingArt(self, kodiId, mediaType, refresh=False): def existingArt(self, kodiId, mediaType, refresh=False):
""" """
@ -552,7 +527,7 @@ class Kodidb_Functions():
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
if self.kodiversion in (15, 16, 17): if KODIVERSION > 14:
# Delete current genres for clean slate # Delete current genres for clean slate
query = ' '.join(( query = ' '.join((
@ -665,10 +640,8 @@ class Kodidb_Functions():
self.cursor.execute(query, (idGenre, kodiid)) self.cursor.execute(query, (idGenre, kodiid))
def addStudios(self, kodiid, studios, mediatype): def addStudios(self, kodiid, studios, mediatype):
for studio in studios: for studio in studios:
if KODIVERSION > 14:
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
query = ' '.join(( query = ' '.join((
@ -987,9 +960,8 @@ class Kodidb_Functions():
"DVDPlayer", 1)) "DVDPlayer", 1))
def addTags(self, kodiid, tags, mediatype): def addTags(self, kodiid, tags, mediatype):
# First, delete any existing tags associated to the id # First, delete any existing tags associated to the id
if self.kodiversion in (15, 16, 17): if KODIVERSION > 14:
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
query = ' '.join(( query = ' '.join((
@ -1014,8 +986,7 @@ class Kodidb_Functions():
self.addTag(kodiid, tag, mediatype) self.addTag(kodiid, tag, mediatype)
def addTag(self, kodiid, tag, mediatype): def addTag(self, kodiid, tag, mediatype):
if KODIVERSION > 14:
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
query = ' '.join(( query = ' '.join((
@ -1075,9 +1046,8 @@ class Kodidb_Functions():
self.cursor.execute(query, (tag_id, kodiid, mediatype)) self.cursor.execute(query, (tag_id, kodiid, mediatype))
def createTag(self, name): def createTag(self, name):
# This will create and return the tag_id # This will create and return the tag_id
if self.kodiversion in (15, 16, 17): if KODIVERSION > 14:
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
query = ' '.join(( query = ' '.join((
@ -1121,10 +1091,7 @@ class Kodidb_Functions():
return tag_id return tag_id
def updateTag(self, oldtag, newtag, kodiid, mediatype): def updateTag(self, oldtag, newtag, kodiid, mediatype):
if KODIVERSION > 14:
log.debug("Updating: %s with %s for %s: %s" % (oldtag, newtag, mediatype, kodiid))
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
try: try:
query = ' '.join(( query = ' '.join((
@ -1172,8 +1139,7 @@ class Kodidb_Functions():
self.cursor.execute(query, (kodiid, mediatype, oldtag,)) self.cursor.execute(query, (kodiid, mediatype, oldtag,))
def removeTag(self, kodiid, tagname, mediatype): def removeTag(self, kodiid, tagname, mediatype):
if KODIVERSION > 14:
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton # Kodi Isengard, Jarvis, Krypton
query = ' '.join(( query = ' '.join((
@ -1347,7 +1313,7 @@ class Kodidb_Functions():
# Create the album # Create the album
self.cursor.execute("select coalesce(max(idAlbum),0) from album") self.cursor.execute("select coalesce(max(idAlbum),0) from album")
albumid = self.cursor.fetchone()[0] + 1 albumid = self.cursor.fetchone()[0] + 1
if self.kodiversion in (15, 16, 17): if KODIVERSION > 14:
query = ( query = (
''' '''
INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strReleaseType) INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strReleaseType)

View file

@ -30,6 +30,7 @@ WINDOW = xbmcgui.Window(10000)
ADDON = xbmcaddon.Addon(id='plugin.video.plexkodiconnect') ADDON = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1) KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
KODIVERSION = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
############################################################################### ###############################################################################
# Main methods # Main methods