2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
2016-04-22 22:46:08 +10:00
|
|
|
from ntpath import dirname
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import artwork
|
2017-01-25 02:04:42 +11:00
|
|
|
from utils import kodiSQL
|
|
|
|
import variables as v
|
2016-08-31 00:43:56 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
|
2016-02-12 00:03:04 +11:00
|
|
|
class GetKodiDB():
|
|
|
|
"""
|
2017-01-29 23:06:09 +11:00
|
|
|
Usage: with GetKodiDB(db_type) as kodi_db:
|
2016-02-12 00:03:04 +11:00
|
|
|
do stuff with kodi_db
|
|
|
|
|
|
|
|
Parameters:
|
2017-01-29 23:06:09 +11:00
|
|
|
db_type: DB to open: 'video', 'music', 'plex', 'texture'
|
2016-02-12 00:03:04 +11:00
|
|
|
|
|
|
|
On exiting "with" (no matter what), commits get automatically committed
|
|
|
|
and the db gets closed
|
|
|
|
"""
|
2017-01-29 23:06:09 +11:00
|
|
|
def __init__(self, db_type):
|
|
|
|
self.db_type = db_type
|
2016-02-12 00:03:04 +11:00
|
|
|
|
|
|
|
def __enter__(self):
|
2017-01-29 23:06:09 +11:00
|
|
|
self.kodiconn = kodiSQL(self.db_type)
|
2017-01-09 01:03:41 +11:00
|
|
|
kodi_db = Kodidb_Functions(self.kodiconn.cursor())
|
|
|
|
return kodi_db
|
2016-02-12 00:03:04 +11:00
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.kodiconn.commit()
|
|
|
|
self.kodiconn.close()
|
|
|
|
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
class Kodidb_Functions():
|
|
|
|
def __init__(self, cursor):
|
|
|
|
self.cursor = cursor
|
2016-09-11 23:49:50 +10:00
|
|
|
self.artwork = artwork.Artwork()
|
2016-08-31 00:43:56 +10:00
|
|
|
|
2016-04-07 21:49:05 +10:00
|
|
|
def pathHack(self):
|
|
|
|
"""
|
|
|
|
Use with Kodi video DB
|
|
|
|
|
|
|
|
Sets strContent to e.g. 'movies' and strScraper to metadata.local
|
|
|
|
|
|
|
|
For some reason, Kodi ignores this if done via itemtypes while e.g.
|
|
|
|
adding or updating items. (addPath method does NOT work)
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"UPDATE path",
|
|
|
|
"SET strContent = ?, strScraper = ?",
|
|
|
|
"WHERE strPath LIKE ?"
|
|
|
|
))
|
2016-04-20 16:46:51 +10:00
|
|
|
self.cursor.execute(
|
|
|
|
query, ('movies',
|
|
|
|
'metadata.local',
|
2017-01-03 00:07:24 +11:00
|
|
|
'plugin://plugin.video.plexkodiconnect/movies%%'))
|
2016-04-07 21:49:05 +10:00
|
|
|
|
2016-04-22 22:46:08 +10:00
|
|
|
def getParentPathId(self, path):
|
|
|
|
"""
|
|
|
|
Video DB: Adds all subdirectories to SQL path while setting a "trail"
|
|
|
|
of parentPathId
|
|
|
|
"""
|
|
|
|
if "\\" in path:
|
|
|
|
# Local path
|
|
|
|
parentpath = "%s\\" % dirname(dirname(path))
|
|
|
|
else:
|
|
|
|
# Network path
|
|
|
|
parentpath = "%s/" % dirname(dirname(path))
|
|
|
|
pathid = self.getPath(parentpath)
|
|
|
|
if pathid is None:
|
|
|
|
self.cursor.execute("select coalesce(max(idPath),0) from path")
|
|
|
|
pathid = self.cursor.fetchone()[0] + 1
|
|
|
|
query = ' '.join((
|
|
|
|
"INSERT INTO path(idPath, strPath)",
|
|
|
|
"VALUES (?, ?)"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (pathid, parentpath))
|
|
|
|
parentPathid = self.getParentPathId(parentpath)
|
|
|
|
query = ' '.join((
|
|
|
|
"UPDATE path",
|
|
|
|
"SET idParentPath = ?",
|
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (parentPathid, pathid))
|
|
|
|
return pathid
|
|
|
|
|
2016-03-22 23:25:30 +11:00
|
|
|
def addPath(self, path, strHash=None):
|
2016-03-18 01:06:04 +11:00
|
|
|
# SQL won't return existing paths otherwise
|
|
|
|
if path is None:
|
|
|
|
path = ""
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idPath",
|
|
|
|
"FROM path",
|
|
|
|
"WHERE strPath = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (path,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
pathid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idPath),0) from path")
|
|
|
|
pathid = self.cursor.fetchone()[0] + 1
|
2016-03-22 23:25:30 +11:00
|
|
|
if strHash is None:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO path(
|
|
|
|
idPath, strPath)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (pathid, path))
|
2016-03-22 23:25:30 +11:00
|
|
|
else:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO path(
|
|
|
|
idPath, strPath, strHash)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (pathid, path, strHash))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return pathid
|
|
|
|
|
|
|
|
def getPath(self, path):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idPath",
|
|
|
|
"FROM path",
|
|
|
|
"WHERE strPath = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (path,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
pathid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
pathid = None
|
|
|
|
|
|
|
|
return pathid
|
|
|
|
|
|
|
|
def addFile(self, filename, pathid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idFile",
|
|
|
|
"FROM files",
|
|
|
|
"WHERE strFilename = ?",
|
|
|
|
"AND idPath = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (filename, pathid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
fileid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idFile),0) from files")
|
|
|
|
fileid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO files(
|
|
|
|
idFile, strFilename)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid, filename))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return fileid
|
|
|
|
|
|
|
|
def getFile(self, fileid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT strFilename",
|
|
|
|
"FROM files",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
filename = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
filename = ""
|
|
|
|
|
|
|
|
return filename
|
|
|
|
|
|
|
|
def removeFile(self, path, filename):
|
|
|
|
|
|
|
|
pathid = self.getPath(path)
|
|
|
|
|
|
|
|
if pathid is not None:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM files",
|
|
|
|
"WHERE idPath = ?",
|
|
|
|
"AND strFilename = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (pathid, filename,))
|
|
|
|
|
|
|
|
def addCountries(self, kodiid, countries, mediatype):
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
for country in countries:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT country_id",
|
|
|
|
"FROM country",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (country,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
country_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Country entry does not exists
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(country_id),0) from country")
|
|
|
|
country_id = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO country(country_id, name) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (country_id, country))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add country to media, processing: %s" % country)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally: # Assign country to content
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO country_link(
|
|
|
|
country_id, media_id, media_type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (country_id, kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
for country in countries:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idCountry",
|
|
|
|
"FROM country",
|
|
|
|
"WHERE strCountry = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (country,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
idCountry = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Country entry does not exists
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idCountry),0) from country")
|
|
|
|
idCountry = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO country(idCountry, strCountry) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (idCountry, country))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add country to media, processing: %s" % country)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
# Only movies have a country field
|
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO countrylinkmovie(
|
|
|
|
idCountry, idMovie)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (idCountry, kodiid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-12-21 02:13:19 +11:00
|
|
|
def _getactorid(self, name):
|
|
|
|
"""
|
|
|
|
Crucial für sync speed!
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT actor_id",
|
|
|
|
"FROM actor",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"LIMIT 1"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (name,))
|
|
|
|
try:
|
|
|
|
actorid = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# Cast entry does not exists
|
|
|
|
self.cursor.execute("select coalesce(max(actor_id),0) from actor")
|
|
|
|
actorid = self.cursor.fetchone()[0] + 1
|
|
|
|
query = "INSERT INTO actor(actor_id, name) VALUES (?, ?)"
|
|
|
|
self.cursor.execute(query, (actorid, name))
|
|
|
|
return actorid
|
|
|
|
|
|
|
|
def _addPerson(self, role, person_type, actorid, kodiid, mediatype,
|
|
|
|
castorder):
|
|
|
|
if "Actor" == person_type:
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO actor_link(
|
|
|
|
actor_id, media_id, media_type, role, cast_order)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (actorid, kodiid, mediatype, role,
|
|
|
|
castorder))
|
|
|
|
castorder += 1
|
|
|
|
elif "Director" == person_type:
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO director_link(
|
|
|
|
actor_id, media_id, media_type)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (actorid, kodiid, mediatype))
|
|
|
|
elif person_type == "Writer":
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO writer_link(
|
|
|
|
actor_id, media_id, media_type)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (actorid, kodiid, mediatype))
|
|
|
|
elif "Artist" == person_type:
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO actor_link(
|
|
|
|
actor_id, media_id, media_type)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (actorid, kodiid, mediatype))
|
|
|
|
return castorder
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def addPeople(self, kodiid, people, mediatype):
|
|
|
|
castorder = 1
|
|
|
|
for person in people:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-12-21 02:13:19 +11:00
|
|
|
actorid = self._getactorid(person['Name'])
|
|
|
|
# Link person to content
|
|
|
|
castorder = self._addPerson(person.get('Role'),
|
|
|
|
person['Type'],
|
|
|
|
actorid,
|
|
|
|
kodiid,
|
|
|
|
mediatype,
|
|
|
|
castorder)
|
2015-12-25 06:51:47 +11:00
|
|
|
# Kodi Helix
|
|
|
|
else:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idActor",
|
|
|
|
"FROM actors",
|
|
|
|
"WHERE strActor = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-12-21 02:13:19 +11:00
|
|
|
self.cursor.execute(query, (person['Name'],))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
actorid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
# Cast entry does not exists
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idActor),0) from actors")
|
|
|
|
actorid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO actors(idActor, strActor) values(?, ?)"
|
2016-12-21 02:13:19 +11:00
|
|
|
self.cursor.execute(query, (actorid, person['Name']))
|
2015-12-25 06:51:47 +11:00
|
|
|
finally:
|
|
|
|
# Link person to content
|
2016-12-21 02:13:19 +11:00
|
|
|
if "Actor" == person['Type']:
|
2015-12-26 14:41:28 +11:00
|
|
|
role = person.get('Role')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO actorlinkmovie(
|
|
|
|
idActor, idMovie, strRole, iOrder)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "tvshow" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO actorlinktvshow(
|
|
|
|
idActor, idShow, strRole, iOrder)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "episode" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO actorlinkepisode(
|
|
|
|
idActor, idEpisode, strRole, iOrder)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-12-21 02:13:19 +11:00
|
|
|
else:
|
|
|
|
# Item is invalid
|
|
|
|
return
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (actorid, kodiid, role, castorder))
|
2015-12-25 06:51:47 +11:00
|
|
|
castorder += 1
|
|
|
|
|
2016-12-21 02:13:19 +11:00
|
|
|
elif "Director" == person['Type']:
|
2015-12-25 06:51:47 +11:00
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO directorlinkmovie(
|
|
|
|
idDirector, idMovie)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "tvshow" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO directorlinktvshow(
|
|
|
|
idDirector, idShow)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "musicvideo" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO directorlinkmusicvideo(
|
|
|
|
idDirector, idMVideo)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
|
|
|
|
elif "episode" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO directorlinkepisode(
|
|
|
|
idDirector, idEpisode)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
else: return # Item is invalid
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (actorid, kodiid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-12-21 02:13:19 +11:00
|
|
|
elif person['Type'] == "Writer":
|
2015-12-25 06:51:47 +11:00
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO writerlinkmovie(
|
|
|
|
idWriter, idMovie)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "episode" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO writerlinkepisode(
|
|
|
|
idWriter, idEpisode)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-12-21 02:13:19 +11:00
|
|
|
else:
|
|
|
|
# Item is invalid
|
|
|
|
return
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (actorid, kodiid))
|
2016-12-21 02:13:19 +11:00
|
|
|
elif "Artist" == person['Type']:
|
2016-02-19 13:01:11 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO artistlinkmusicvideo(
|
|
|
|
idArtist, idMVideo)
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (actorid, kodiid))
|
2016-02-19 13:01:11 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
# Add person image to art table
|
2016-12-21 02:13:19 +11:00
|
|
|
if person['imageurl']:
|
|
|
|
self.artwork.addOrUpdateArt(person['imageurl'], actorid,
|
|
|
|
person['Type'].lower(), "thumb",
|
|
|
|
self.cursor)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
def existingArt(self, kodiId, mediaType, refresh=False):
|
|
|
|
"""
|
|
|
|
For kodiId, returns an artwork dict with already existing art from
|
|
|
|
the Kodi db
|
|
|
|
"""
|
|
|
|
# Only get EITHER poster OR thumb (should have same URL)
|
|
|
|
kodiToPKC = {
|
|
|
|
'banner': 'Banner',
|
|
|
|
'clearart': 'Art',
|
|
|
|
'clearlogo': 'Logo',
|
|
|
|
'discart': 'Disc',
|
|
|
|
'landscape': 'Thumb',
|
|
|
|
'thumb': 'Primary'
|
|
|
|
}
|
|
|
|
# BoxRear yet unused
|
|
|
|
result = {'BoxRear': ''}
|
|
|
|
for art in kodiToPKC:
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (kodiId, mediaType, art,))
|
|
|
|
try:
|
|
|
|
url = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
url = ""
|
|
|
|
result[kodiToPKC[art]] = url
|
|
|
|
# There may be several fanart URLs saved
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT url",
|
|
|
|
"FROM art",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND type LIKE ?"
|
|
|
|
))
|
|
|
|
data = self.cursor.execute(query, (kodiId, mediaType, "fanart%",))
|
|
|
|
result['Backdrop'] = [d[0] for d in data]
|
|
|
|
return result
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def addGenres(self, kodiid, genres, mediatype):
|
|
|
|
|
|
|
|
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2015-12-25 06:51:47 +11:00
|
|
|
# Delete current genres for clean slate
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM genre_link",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Add genres
|
|
|
|
for genre in genres:
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT genre_id",
|
|
|
|
"FROM genre",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
genre_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Create genre in database
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(genre_id),0) from genre")
|
|
|
|
genre_id = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO genre(genre_id, name) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre_id, genre))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add Genres to media, processing: %s" % genre)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
finally:
|
|
|
|
# Assign genre to item
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO genre_link(
|
|
|
|
genre_id, media_id, media_type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre_id, kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
# Delete current genres for clean slate
|
|
|
|
if "movie" in mediatype:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("DELETE FROM genrelinkmovie WHERE idMovie = ?", (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
elif "tvshow" in mediatype:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("DELETE FROM genrelinktvshow WHERE idShow = ?", (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
elif "musicvideo" in mediatype:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("DELETE FROM genrelinkmusicvideo WHERE idMVideo = ?", (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Add genres
|
|
|
|
for genre in genres:
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idGenre",
|
|
|
|
"FROM genre",
|
|
|
|
"WHERE strGenre = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
idGenre = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Create genre in database
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idGenre),0) from genre")
|
|
|
|
idGenre = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO genre(idGenre, strGenre) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (idGenre, genre))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add Genres to media, processing: %s" % genre)
|
2016-04-26 21:53:19 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
finally:
|
|
|
|
# Assign genre to item
|
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE into genrelinkmovie(
|
|
|
|
idGenre, idMovie)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "tvshow" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE into genrelinktvshow(
|
|
|
|
idGenre, idShow)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
elif "musicvideo" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE into genrelinkmusicvideo(
|
|
|
|
idGenre, idMVideo)
|
|
|
|
|
|
|
|
VALUES (?, ?)
|
|
|
|
'''
|
|
|
|
)
|
|
|
|
else: return # Item is invalid
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (idGenre, kodiid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def addStudios(self, kodiid, studios, mediatype):
|
|
|
|
for studio in studios:
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT studio_id",
|
|
|
|
"FROM studio",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studio,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
studioid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Studio does not exists.
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(studio_id),0) from studio")
|
|
|
|
studioid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO studio(studio_id, name) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studioid, studio))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add Studios to media, processing: %s" % studio)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally: # Assign studio to item
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO studio_link(
|
|
|
|
studio_id, media_id, media_type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
''')
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studioid, kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idstudio",
|
|
|
|
"FROM studio",
|
|
|
|
"WHERE strstudio = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studio,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
studioid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Studio does not exists.
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idstudio),0) from studio")
|
|
|
|
studioid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO studio(idstudio, strstudio) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studioid, studio))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Add Studios to media, processing: %s" % studio)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally: # Assign studio to item
|
|
|
|
if "movie" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO studiolinkmovie(idstudio, idMovie)
|
|
|
|
VALUES (?, ?)
|
|
|
|
''')
|
|
|
|
elif "musicvideo" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO studiolinkmusicvideo(idstudio, idMVideo)
|
|
|
|
VALUES (?, ?)
|
|
|
|
''')
|
|
|
|
elif "tvshow" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO studiolinktvshow(idstudio, idShow)
|
|
|
|
VALUES (?, ?)
|
|
|
|
''')
|
|
|
|
elif "episode" in mediatype:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO studiolinkepisode(idstudio, idEpisode)
|
|
|
|
VALUES (?, ?)
|
|
|
|
''')
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (studioid, kodiid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def addStreams(self, fileid, streamdetails, runtime):
|
|
|
|
|
|
|
|
# First remove any existing entries
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("DELETE FROM streamdetails WHERE idFile = ?", (fileid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
if streamdetails:
|
|
|
|
# Video details
|
|
|
|
for videotrack in streamdetails['video']:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO streamdetails(
|
|
|
|
idFile, iStreamType, strVideoCodec, fVideoAspect,
|
|
|
|
iVideoWidth, iVideoHeight, iVideoDuration ,strStereoMode)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid, 0, videotrack['codec'],
|
2016-01-12 08:20:34 +11:00
|
|
|
videotrack['aspect'], videotrack['width'], videotrack['height'],
|
2015-12-25 06:51:47 +11:00
|
|
|
runtime ,videotrack['video3DFormat']))
|
|
|
|
|
|
|
|
# Audio details
|
|
|
|
for audiotrack in streamdetails['audio']:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO streamdetails(
|
|
|
|
idFile, iStreamType, strAudioCodec, iAudioChannels, strAudioLanguage)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid, 1, audiotrack['codec'],
|
2016-01-12 08:20:34 +11:00
|
|
|
audiotrack['channels'], audiotrack['language']))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Subtitles details
|
|
|
|
for subtitletrack in streamdetails['subtitle']:
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO streamdetails(
|
|
|
|
idFile, iStreamType, strSubtitleLanguage)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid, 2, subtitletrack))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
def getResumes(self):
|
|
|
|
"""
|
|
|
|
VIDEOS
|
|
|
|
|
|
|
|
Returns all Kodi idFile that have a resume point set (not unwatched
|
|
|
|
ones or items that have already been completely watched)
|
|
|
|
"""
|
|
|
|
cursor = self.cursor
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT idFile",
|
|
|
|
"FROM bookmark"
|
|
|
|
))
|
|
|
|
try:
|
|
|
|
rows = cursor.execute(query)
|
|
|
|
except:
|
|
|
|
return []
|
|
|
|
ids = []
|
|
|
|
for row in rows:
|
|
|
|
ids.append(row[0])
|
|
|
|
return ids
|
|
|
|
|
|
|
|
def getUnplayedMusicItems(self):
|
|
|
|
"""
|
|
|
|
MUSIC
|
|
|
|
|
|
|
|
Returns all Kodi Item idFile that have not yet been completely played
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
2016-03-17 23:34:11 +11:00
|
|
|
"SELECT idSong",
|
2016-03-12 00:42:14 +11:00
|
|
|
"FROM song",
|
2016-03-28 19:51:38 +11:00
|
|
|
"WHERE iTimesPlayed = ?"
|
2016-03-12 00:42:14 +11:00
|
|
|
))
|
|
|
|
try:
|
2016-03-28 19:51:38 +11:00
|
|
|
rows = self.cursor.execute(query, (0, ))
|
2016-03-12 00:42:14 +11:00
|
|
|
except:
|
|
|
|
return []
|
|
|
|
ids = []
|
|
|
|
for row in rows:
|
|
|
|
ids.append(row[0])
|
|
|
|
return ids
|
|
|
|
|
2016-06-26 00:02:40 +10:00
|
|
|
def getIdFromFilename(self, filename, path):
|
2016-03-23 02:17:06 +11:00
|
|
|
"""
|
2016-06-26 00:02:40 +10:00
|
|
|
Returns the tuple (itemId, type) where
|
|
|
|
itemId: Kodi DB unique Id for either movie or episode
|
|
|
|
type: either 'movie' or 'episode'
|
|
|
|
|
|
|
|
Returns None if not found OR if too many entries were found
|
2016-03-23 02:17:06 +11:00
|
|
|
"""
|
2016-06-26 00:02:40 +10:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT idFile, idPath",
|
|
|
|
"FROM files",
|
|
|
|
"WHERE strFilename = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (filename,))
|
|
|
|
files = self.cursor.fetchall()
|
|
|
|
if len(files) == 0:
|
2016-08-31 00:43:56 +10:00
|
|
|
log.info('Did not find any file, abort')
|
2016-04-18 20:09:01 +10:00
|
|
|
return
|
2016-06-26 00:02:40 +10:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT strPath",
|
|
|
|
"FROM path",
|
|
|
|
"WHERE idPath = ?"
|
|
|
|
))
|
|
|
|
# result will contain a list of all idFile with matching filename and
|
|
|
|
# matching path
|
|
|
|
result = []
|
|
|
|
for file in files:
|
|
|
|
# Use idPath to get path as a string
|
|
|
|
self.cursor.execute(query, (file[1],))
|
2016-03-23 02:17:06 +11:00
|
|
|
try:
|
2016-06-26 00:02:40 +10:00
|
|
|
strPath = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
# idPath not found; skip
|
|
|
|
continue
|
|
|
|
# For whatever reason, double might have become triple
|
|
|
|
strPath = strPath.replace('///', '//')
|
|
|
|
strPath = strPath.replace('\\\\\\', '\\\\')
|
|
|
|
if strPath == path:
|
|
|
|
result.append(file[0])
|
|
|
|
if len(result) == 0:
|
2016-08-31 00:43:56 +10:00
|
|
|
log.info('Did not find matching paths, abort')
|
2016-06-26 00:02:40 +10:00
|
|
|
return
|
|
|
|
# Kodi seems to make ONE temporary entry; we only want the earlier,
|
|
|
|
# permanent one
|
|
|
|
if len(result) > 2:
|
2016-08-31 00:43:56 +10:00
|
|
|
log.warn('We found too many items with matching filenames and '
|
|
|
|
' paths, aborting')
|
2016-06-26 00:02:40 +10:00
|
|
|
return
|
|
|
|
idFile = result[0]
|
2016-03-23 02:17:06 +11:00
|
|
|
|
2016-06-26 00:02:40 +10:00
|
|
|
# Try movies first
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT idMovie",
|
|
|
|
"FROM movie",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (idFile,))
|
|
|
|
try:
|
|
|
|
itemId = self.cursor.fetchone()[0]
|
2017-01-25 02:04:42 +11:00
|
|
|
typus = v.KODI_TYPE_MOVIE
|
2016-06-26 00:02:40 +10:00
|
|
|
except TypeError:
|
|
|
|
# Try tv shows next
|
2016-03-23 02:17:06 +11:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT idEpisode",
|
|
|
|
"FROM episode",
|
2016-06-26 00:02:40 +10:00
|
|
|
"WHERE idFile = ?"
|
2016-03-23 02:17:06 +11:00
|
|
|
))
|
2016-06-26 00:02:40 +10:00
|
|
|
self.cursor.execute(query, (idFile,))
|
2016-03-23 02:17:06 +11:00
|
|
|
try:
|
2016-06-26 00:02:40 +10:00
|
|
|
itemId = self.cursor.fetchone()[0]
|
2017-01-25 02:04:42 +11:00
|
|
|
typus = v.KODI_TYPE_EPISODE
|
2016-06-26 00:02:40 +10:00
|
|
|
except TypeError:
|
2016-08-31 00:43:56 +10:00
|
|
|
log.warn('Unexpectantly did not find a match!')
|
2016-04-18 20:09:01 +10:00
|
|
|
return
|
2016-06-26 00:02:40 +10:00
|
|
|
return itemId, typus
|
2016-03-23 02:17:06 +11:00
|
|
|
|
2016-03-12 00:42:14 +11:00
|
|
|
def getUnplayedItems(self):
|
|
|
|
"""
|
|
|
|
VIDEOS
|
|
|
|
|
|
|
|
Returns all Kodi Item idFile that have not yet been completely played
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT idFile",
|
|
|
|
"FROM files",
|
|
|
|
"WHERE playCount IS NULL OR playCount = ''"
|
|
|
|
))
|
|
|
|
try:
|
|
|
|
rows = self.cursor.execute(query)
|
|
|
|
except:
|
|
|
|
return []
|
|
|
|
ids = []
|
|
|
|
for row in rows:
|
|
|
|
ids.append(row[0])
|
|
|
|
return ids
|
|
|
|
|
2016-03-25 04:52:02 +11:00
|
|
|
def getVideoRuntime(self, kodiid, mediatype):
|
2017-01-25 02:04:42 +11:00
|
|
|
if mediatype == v.KODI_TYPE_MOVIE:
|
2016-03-25 04:52:02 +11:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT c11",
|
|
|
|
"FROM movie",
|
|
|
|
"WHERE idMovie = ?",
|
|
|
|
))
|
2017-01-25 02:04:42 +11:00
|
|
|
elif mediatype == v.KODI_TYPE_EPISODE:
|
2016-03-25 04:52:02 +11:00
|
|
|
query = ' '.join((
|
|
|
|
"SELECT c09",
|
|
|
|
"FROM episode",
|
|
|
|
"WHERE idEpisode = ?",
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (kodiid,))
|
|
|
|
try:
|
|
|
|
runtime = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
return None
|
|
|
|
return int(runtime)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def addPlaystate(self, fileid, resume_seconds, total_seconds, playcount, dateplayed):
|
|
|
|
# Delete existing resume point
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM bookmark",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (fileid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Set watched count
|
2016-04-26 21:53:19 +10:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE files",
|
|
|
|
"SET playCount = ?, lastPlayed = ?",
|
|
|
|
"WHERE idFile = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (playcount, dateplayed, fileid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Set the resume bookmark
|
|
|
|
if resume_seconds:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idBookmark),0) from bookmark")
|
|
|
|
bookmarkId = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO bookmark(
|
|
|
|
idBookmark, idFile, timeInSeconds, totalTimeInSeconds, player, type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (bookmarkId, fileid, resume_seconds, total_seconds,
|
2015-12-25 06:51:47 +11:00
|
|
|
"DVDPlayer", 1))
|
|
|
|
|
|
|
|
def addTags(self, kodiid, tags, mediatype):
|
|
|
|
# First, delete any existing tags associated to the id
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM tag_link",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM taglinks",
|
|
|
|
"WHERE idMedia = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
# Add tags
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Adding Tags: %s" % tags)
|
2015-12-25 06:51:47 +11:00
|
|
|
for tag in tags:
|
|
|
|
self.addTag(kodiid, tag, mediatype)
|
|
|
|
|
|
|
|
def addTag(self, kodiid, tag, mediatype):
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT tag_id",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Create the tag, because it does not exist
|
|
|
|
tag_id = self.createTag(tag)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Adding tag: %s" % tag)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
# Assign tag to item
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO tag_link(
|
|
|
|
tag_id, media_id, media_type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag_id, kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idTag",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE strTag = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
# Create the tag
|
|
|
|
tag_id = self.createTag(tag)
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Adding tag: %s" % tag)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
finally:
|
|
|
|
# Assign tag to item
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO taglinks(
|
|
|
|
idTag, idMedia, media_type)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag_id, kodiid, mediatype))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def createTag(self, name):
|
|
|
|
# This will create and return the tag_id
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT tag_id",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (name,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(tag_id),0) from tag")
|
|
|
|
tag_id = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO tag(tag_id, name) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag_id, name))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Create tag_id: %s name: %s" % (tag_id, name))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idTag",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE strTag = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (name,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idTag),0) from tag")
|
|
|
|
tag_id = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO tag(idTag, strTag) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tag_id, name))
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Create idTag: %s name: %s" % (tag_id, name))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return tag_id
|
|
|
|
|
|
|
|
def updateTag(self, oldtag, newtag, kodiid, mediatype):
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2016-12-21 02:13:19 +11:00
|
|
|
try:
|
2016-01-09 20:03:39 +11:00
|
|
|
query = ' '.join((
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-01-09 20:03:39 +11:00
|
|
|
"UPDATE tag_link",
|
|
|
|
"SET tag_id = ?",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND tag_id = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (newtag, kodiid, mediatype, oldtag,))
|
2016-01-09 20:03:39 +11:00
|
|
|
except Exception as e:
|
|
|
|
# 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 = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype, oldtag,))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
2016-01-09 20:03:39 +11:00
|
|
|
try:
|
|
|
|
query = ' '.join((
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-01-09 20:03:39 +11:00
|
|
|
"UPDATE taglinks",
|
|
|
|
"SET idTag = ?",
|
|
|
|
"WHERE idMedia = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND idTag = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (newtag, kodiid, mediatype, oldtag,))
|
2016-01-09 20:03:39 +11:00
|
|
|
except Exception as e:
|
|
|
|
# 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 = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype, oldtag,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def removeTag(self, kodiid, tagname, mediatype):
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-01-24 02:45:31 +11:00
|
|
|
# Kodi Isengard, Jarvis, Krypton
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT tag_id",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE name = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tagname,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM tag_link",
|
|
|
|
"WHERE media_id = ?",
|
|
|
|
"AND media_type = ?",
|
|
|
|
"AND tag_id = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype, tag_id,))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
# Kodi Helix
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idTag",
|
|
|
|
"FROM tag",
|
|
|
|
"WHERE strTag = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (tagname,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
tag_id = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM taglinks",
|
|
|
|
"WHERE idMedia = ?",
|
2015-12-25 08:16:47 +11:00
|
|
|
"AND media_type = ?",
|
2015-12-25 06:51:47 +11:00
|
|
|
"AND idTag = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid, mediatype, tag_id,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
def addSets(self, movieid, collections, kodicursor):
|
2016-05-17 01:56:48 +10:00
|
|
|
for setname in collections:
|
|
|
|
setid = self.createBoxset(setname)
|
|
|
|
self.assignBoxset(setid, movieid)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def createBoxset(self, boxsetname):
|
|
|
|
|
2016-08-30 02:44:27 +10:00
|
|
|
log.debug("Adding boxset: %s" % boxsetname)
|
2015-12-25 06:51:47 +11:00
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idSet",
|
|
|
|
"FROM sets",
|
|
|
|
"WHERE strSet = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (boxsetname,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
setid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idSet),0) from sets")
|
|
|
|
setid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT INTO sets(idSet, strSet) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (setid, boxsetname))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return setid
|
|
|
|
|
|
|
|
def assignBoxset(self, setid, movieid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE movie",
|
|
|
|
"SET idSet = ?",
|
|
|
|
"WHERE idMovie = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (setid, movieid,))
|
|
|
|
|
|
|
|
def removefromBoxset(self, movieid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE movie",
|
|
|
|
"SET idSet = null",
|
|
|
|
"WHERE idMovie = ?"
|
|
|
|
))
|
|
|
|
self.cursor.execute(query, (movieid,))
|
|
|
|
|
|
|
|
def addSeason(self, showid, seasonnumber):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idSeason",
|
|
|
|
"FROM seasons",
|
|
|
|
"WHERE idShow = ?",
|
|
|
|
"AND season = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (showid, seasonnumber,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
seasonid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idSeason),0) from seasons")
|
|
|
|
seasonid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = "INSERT INTO seasons(idSeason, idShow, season) values(?, ?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (seasonid, showid, seasonnumber))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return seasonid
|
|
|
|
|
|
|
|
def addArtist(self, name, musicbrainz):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idArtist, strArtist",
|
|
|
|
"FROM artist",
|
|
|
|
"WHERE strMusicBrainzArtistID = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (musicbrainz,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
result = self.cursor.fetchone()
|
2015-12-25 06:51:47 +11:00
|
|
|
artistid = result[0]
|
|
|
|
artistname = result[1]
|
|
|
|
|
|
|
|
except TypeError:
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idArtist",
|
|
|
|
"FROM artist",
|
|
|
|
"WHERE strArtist = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (name,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
artistid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idArtist),0) from artist")
|
|
|
|
artistid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO artist(idArtist, strArtist, strMusicBrainzArtistID)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (artistid, name, musicbrainz))
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
if artistname != name:
|
|
|
|
query = "UPDATE artist SET strArtist = ? WHERE idArtist = ?"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (name, artistid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return artistid
|
|
|
|
|
|
|
|
def addAlbum(self, name, musicbrainz):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idAlbum",
|
|
|
|
"FROM album",
|
|
|
|
"WHERE strMusicBrainzAlbumID = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (musicbrainz,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
albumid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
2016-01-02 16:24:28 +11:00
|
|
|
# Create the album
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idAlbum),0) from album")
|
|
|
|
albumid = self.cursor.fetchone()[0] + 1
|
2017-01-25 02:04:42 +11:00
|
|
|
if v.KODIVERSION > 14:
|
2016-03-16 22:13:47 +11:00
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID, strReleaseType)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-03-16 22:13:47 +11:00
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (albumid, name, musicbrainz, "album"))
|
2016-03-16 22:13:47 +11:00
|
|
|
else: # Helix
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO album(idAlbum, strAlbum, strMusicBrainzAlbumID)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (albumid, name, musicbrainz))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
return albumid
|
|
|
|
|
|
|
|
def addMusicGenres(self, kodiid, genres, mediatype):
|
|
|
|
|
|
|
|
if mediatype == "album":
|
|
|
|
|
|
|
|
# Delete current genres for clean slate
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM album_genre",
|
|
|
|
"WHERE idAlbum = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
for genre in genres:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idGenre",
|
|
|
|
"FROM genre",
|
|
|
|
"WHERE strGenre = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
genreid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
# Create the genre
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idGenre),0) from genre")
|
|
|
|
genreid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = "INSERT INTO genre(idGenre, strGenre) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genreid, genre))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT OR REPLACE INTO album_genre(idGenre, idAlbum) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genreid, kodiid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
elif mediatype == "song":
|
|
|
|
|
|
|
|
# Delete current genres for clean slate
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM song_genre",
|
|
|
|
"WHERE idSong = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (kodiid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
for genre in genres:
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT idGenre",
|
|
|
|
"FROM genre",
|
|
|
|
"WHERE strGenre = ?",
|
|
|
|
"COLLATE NOCASE"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genre,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
genreid = self.cursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
# Create the genre
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute("select coalesce(max(idGenre),0) from genre")
|
|
|
|
genreid = self.cursor.fetchone()[0] + 1
|
2015-12-25 06:51:47 +11:00
|
|
|
query = "INSERT INTO genre(idGenre, strGenre) values(?, ?)"
|
2016-04-26 21:53:19 +10:00
|
|
|
self.cursor.execute(query, (genreid, genre))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "INSERT OR REPLACE INTO song_genre(idGenre, idSong) values(?, ?)"
|
2016-05-17 01:56:48 +10:00
|
|
|
self.cursor.execute(query, (genreid, kodiid))
|
2016-12-29 21:22:02 +11:00
|
|
|
|
2017-01-10 06:33:52 +11:00
|
|
|
# Krypton only stuff ##############################
|
|
|
|
|
2017-02-03 02:21:01 +11:00
|
|
|
def update_userrating(self, kodi_id, kodi_type, userrating):
|
|
|
|
"""
|
|
|
|
Updates userrating for >=Krypton
|
|
|
|
"""
|
|
|
|
if kodi_type == v.KODI_TYPE_MOVIE:
|
|
|
|
ID = 'idMovie'
|
|
|
|
elif kodi_type == v.KODI_TYPE_EPISODE:
|
|
|
|
ID = 'idEpisode'
|
|
|
|
elif kodi_type == v.KODI_TYPE_SONG:
|
|
|
|
ID = 'idSong'
|
2017-05-12 21:25:46 +10:00
|
|
|
query = '''UPDATE %s SET userrating = ? WHERE ? = ?''' % kodi_type
|
|
|
|
self.cursor.execute(query, (userrating, ID, kodi_id))
|
2017-02-03 02:21:01 +11:00
|
|
|
|
2017-01-10 06:33:52 +11:00
|
|
|
def create_entry_uniqueid(self):
|
|
|
|
self.cursor.execute(
|
|
|
|
"select coalesce(max(uniqueid_id),0) from uniqueid")
|
|
|
|
return self.cursor.fetchone()[0] + 1
|
|
|
|
|
|
|
|
def add_uniqueid(self, *args):
|
|
|
|
"""
|
|
|
|
Feed with:
|
2017-02-14 06:50:10 +11:00
|
|
|
uniqueid_id: int
|
|
|
|
media_id: int
|
|
|
|
media_type: string
|
|
|
|
value: string
|
|
|
|
type: e.g. 'imdb' or 'tvdb'
|
2017-01-10 06:33:52 +11:00
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
INSERT INTO uniqueid(
|
|
|
|
uniqueid_id, media_id, media_type, value, type)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (args))
|
|
|
|
|
2017-02-14 06:26:30 +11:00
|
|
|
def get_uniqueid(self, kodi_id, kodi_type):
|
|
|
|
query = '''
|
|
|
|
SELECT uniqueid_id FROM uniqueid
|
|
|
|
WHERE media_id = ? AND media_type = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (kodi_id, kodi_type))
|
2017-01-11 07:14:30 +11:00
|
|
|
try:
|
|
|
|
uniqueid = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
uniqueid = None
|
|
|
|
return uniqueid
|
|
|
|
|
|
|
|
def update_uniqueid(self, *args):
|
|
|
|
"""
|
|
|
|
Pass in media_id, media_type, value, type, uniqueid_id
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
UPDATE uniqueid
|
|
|
|
SET media_id = ?, media_type = ?, value = ?, type = ?
|
|
|
|
WHERE uniqueid_id = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (args))
|
|
|
|
|
2017-02-03 02:53:50 +11:00
|
|
|
def remove_uniqueid(self, kodi_id, kodi_type):
|
|
|
|
query = '''
|
|
|
|
DELETE FROM uniqueid
|
|
|
|
WHERE media_id = ? AND media_type = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (kodi_id, kodi_type))
|
|
|
|
|
2017-01-10 06:33:52 +11:00
|
|
|
def create_entry_rating(self):
|
|
|
|
self.cursor.execute("select coalesce(max(rating_id),0) from rating")
|
|
|
|
return self.cursor.fetchone()[0] + 1
|
|
|
|
|
2017-02-14 06:23:02 +11:00
|
|
|
def get_ratingid(self, kodi_id, kodi_type):
|
|
|
|
query = '''
|
|
|
|
SELECT rating_id FROM rating
|
|
|
|
WHERE media_id = ? AND media_type = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (kodi_id, kodi_type))
|
2017-01-10 06:33:52 +11:00
|
|
|
try:
|
|
|
|
ratingid = self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
ratingid = None
|
|
|
|
return ratingid
|
|
|
|
|
|
|
|
def update_ratings(self, *args):
|
|
|
|
"""
|
|
|
|
Feed with media_id, media_type, rating_type, rating, votes, rating_id
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
UPDATE rating
|
|
|
|
SET media_id = ?,
|
|
|
|
media_type = ?,
|
|
|
|
rating_type = ?,
|
|
|
|
rating = ?,
|
|
|
|
votes = ?
|
|
|
|
WHERE rating_id = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (args))
|
|
|
|
|
|
|
|
def add_ratings(self, *args):
|
|
|
|
"""
|
|
|
|
feed with:
|
|
|
|
rating_id, media_id, media_type, rating_type, rating, votes
|
|
|
|
|
|
|
|
rating_type = 'default'
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
INSERT INTO rating(
|
|
|
|
rating_id, media_id, media_type, rating_type, rating, votes)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (args))
|
|
|
|
|
2017-02-03 02:53:50 +11:00
|
|
|
def remove_ratings(self, kodi_id, kodi_type):
|
|
|
|
query = '''
|
|
|
|
DELETE FROM rating
|
|
|
|
WHERE media_id = ? AND media_type = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (kodi_id, kodi_type))
|
|
|
|
|
2016-12-29 21:22:02 +11:00
|
|
|
|
|
|
|
def get_kodiid_from_filename(file):
|
|
|
|
"""
|
|
|
|
Returns the tuple (kodiid, type) if we have a video in the database with
|
|
|
|
said filename, or (None, None)
|
|
|
|
"""
|
|
|
|
kodiid = None
|
|
|
|
typus = None
|
|
|
|
try:
|
|
|
|
filename = file.rsplit('/', 1)[1]
|
|
|
|
path = file.rsplit('/', 1)[0] + '/'
|
|
|
|
except IndexError:
|
|
|
|
filename = file.rsplit('\\', 1)[1]
|
|
|
|
path = file.rsplit('\\', 1)[0] + '\\'
|
|
|
|
log.debug('Trying to figure out playing item from filename: %s '
|
|
|
|
'and path: %s' % (filename, path))
|
|
|
|
with GetKodiDB('video') as kodi_db:
|
|
|
|
try:
|
|
|
|
kodiid, typus = kodi_db.getIdFromFilename(filename, path)
|
|
|
|
except TypeError:
|
|
|
|
log.info('No kodi video element found with filename %s' % filename)
|
|
|
|
return (kodiid, typus)
|