jellyfin-kodi/resources/lib/kodidb_functions.py

123 lines
4.1 KiB
Python
Raw Normal View History

2016-03-31 15:21:14 +00:00
# -*- coding: utf-8 -*-
##################################################################################################
import logging
2016-03-31 15:21:14 +00:00
import xbmc
import api
import artwork
#################################################################################################
2016-03-31 15:21:14 +00:00
log = logging.getLogger("EMBY."+__name__)
#################################################################################################
2016-03-31 15:21:14 +00:00
2016-10-28 05:02:47 +00:00
class Kodidb_Functions(object):
2016-03-31 15:21:14 +00:00
kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
def __init__(self, cursor):
2016-10-28 05:02:47 +00:00
2016-03-31 15:21:14 +00:00
self.cursor = cursor
self.artwork = artwork.Artwork()
def createTag(self, name):
# This will create and return the tag_id
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton
query = ' '.join((
"SELECT tag_id",
"FROM tag",
"WHERE name = ?",
"COLLATE NOCASE"
))
self.cursor.execute(query, (name,))
try:
tag_id = self.cursor.fetchone()[0]
2016-10-28 05:02:47 +00:00
2016-03-31 15:21:14 +00:00
except TypeError:
self.cursor.execute("select coalesce(max(tag_id),0) from tag")
tag_id = self.cursor.fetchone()[0] + 1
query = "INSERT INTO tag(tag_id, name) values(?, ?)"
self.cursor.execute(query, (tag_id, name))
2016-10-28 05:02:47 +00:00
log.debug("Create tag_id: %s name: %s", tag_id, name)
2016-03-31 15:21:14 +00:00
else:
# Kodi Helix
query = ' '.join((
"SELECT idTag",
"FROM tag",
"WHERE strTag = ?",
"COLLATE NOCASE"
))
self.cursor.execute(query, (name,))
try:
tag_id = self.cursor.fetchone()[0]
except TypeError:
self.cursor.execute("select coalesce(max(idTag),0) from tag")
tag_id = self.cursor.fetchone()[0] + 1
query = "INSERT INTO tag(idTag, strTag) values(?, ?)"
self.cursor.execute(query, (tag_id, name))
2016-10-28 05:02:47 +00:00
log.debug("Create idTag: %s name: %s", tag_id, name)
2016-03-31 15:21:14 +00:00
return tag_id
def updateTag(self, oldtag, newtag, kodiid, mediatype):
2016-10-28 05:02:47 +00:00
# TODO: Move to video nodes eventually
log.debug("Updating: %s with %s for %s: %s", oldtag, newtag, mediatype, kodiid)
2016-03-31 15:21:14 +00:00
if self.kodiversion in (15, 16, 17):
# Kodi Isengard, Jarvis, Krypton
2016-10-28 05:02:47 +00:00
try:
2016-03-31 15:21:14 +00:00
query = ' '.join((
"UPDATE tag_link",
"SET tag_id = ?",
"WHERE media_id = ?",
"AND media_type = ?",
"AND tag_id = ?"
))
self.cursor.execute(query, (newtag, kodiid, mediatype, oldtag,))
2016-10-28 05:02:47 +00:00
except Exception:
2016-03-31 15:21:14 +00:00
# The new tag we are going to apply already exists for this item
# delete current tag instead
query = ' '.join((
"DELETE FROM tag_link",
"WHERE media_id = ?",
"AND media_type = ?",
"AND tag_id = ?"
))
self.cursor.execute(query, (kodiid, mediatype, oldtag,))
else:
# Kodi Helix
try:
query = ' '.join((
"UPDATE taglinks",
"SET idTag = ?",
"WHERE idMedia = ?",
"AND media_type = ?",
"AND idTag = ?"
))
self.cursor.execute(query, (newtag, kodiid, mediatype, oldtag,))
2016-10-28 05:02:47 +00:00
except Exception:
2016-03-31 15:21:14 +00:00
# The new tag we are going to apply already exists for this item
# delete current tag instead
query = ' '.join((
"DELETE FROM taglinks",
"WHERE idMedia = ?",
"AND media_type = ?",
"AND idTag = ?"
))
self.cursor.execute(query, (kodiid, mediatype, oldtag,))