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-02-10 21:00:32 +11:00
|
|
|
from utils import logging, kodiSQL
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
|
|
|
from sqlite3 import OperationalError
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__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-10 21:00:32 +11:00
|
|
|
class GetEmbyDB():
|
|
|
|
"""
|
|
|
|
Usage: with GetEmbyDB() as emby_db:
|
|
|
|
do stuff with emby_db
|
|
|
|
|
|
|
|
On exiting "with" (no matter what), commits get automatically committed
|
|
|
|
and the db gets closed
|
|
|
|
"""
|
|
|
|
def __enter__(self):
|
|
|
|
self.embyconn = kodiSQL('emby')
|
|
|
|
self.emby_db = Embydb_Functions(self.embyconn.cursor())
|
|
|
|
return self.emby_db
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.embyconn.commit()
|
|
|
|
self.embyconn.close()
|
|
|
|
|
|
|
|
|
|
|
|
@logging
|
2015-12-25 06:51:47 +11:00
|
|
|
class Embydb_Functions():
|
|
|
|
|
|
|
|
def __init__(self, embycursor):
|
|
|
|
|
|
|
|
self.embycursor = embycursor
|
|
|
|
|
|
|
|
def getViews(self):
|
|
|
|
|
|
|
|
views = []
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT view_id",
|
|
|
|
"FROM view"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query)
|
|
|
|
rows = self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
for row in rows:
|
|
|
|
views.append(row[0])
|
|
|
|
return views
|
|
|
|
|
2016-01-28 06:41:28 +11:00
|
|
|
def getAllViewInfo(self):
|
|
|
|
|
|
|
|
embycursor = self.embycursor
|
|
|
|
views = []
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT view_id, view_name, media_type",
|
|
|
|
"FROM view"
|
|
|
|
))
|
|
|
|
embycursor.execute(query)
|
|
|
|
rows = embycursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
views.append({'id': row[0],
|
|
|
|
'name': row[1],
|
|
|
|
'itemtype': row[2]})
|
|
|
|
return views
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getView_byId(self, viewid):
|
|
|
|
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT view_name, media_type, kodi_tagid",
|
|
|
|
"FROM view",
|
|
|
|
"WHERE view_id = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (viewid,))
|
|
|
|
view = self.embycursor.fetchone()
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return view
|
|
|
|
|
|
|
|
def getView_byType(self, mediatype):
|
|
|
|
|
|
|
|
views = []
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
2016-01-28 06:41:28 +11:00
|
|
|
"SELECT view_id, view_name, media_type",
|
2015-12-25 06:51:47 +11:00
|
|
|
"FROM view",
|
|
|
|
"WHERE media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (mediatype,))
|
|
|
|
rows = self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
for row in rows:
|
|
|
|
views.append({
|
|
|
|
|
|
|
|
'id': row[0],
|
2016-01-28 06:41:28 +11:00
|
|
|
'name': row[1],
|
|
|
|
'itemtype': row[2]
|
2015-12-25 06:51:47 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
return views
|
|
|
|
|
|
|
|
def getView_byName(self, tagname):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT view_id",
|
|
|
|
"FROM view",
|
|
|
|
"WHERE view_name = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (tagname,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
view = self.embycursor.fetchone()[0]
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
view = None
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def addView(self, plexid, name, mediatype, tagid):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT INTO view(
|
|
|
|
view_id, view_name, media_type, kodi_tagid)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid, name, mediatype, tagid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def updateView(self, name, tagid, mediafolderid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"UPDATE view",
|
|
|
|
"SET view_name = ?, kodi_tagid = ?",
|
|
|
|
"WHERE view_id = ?"
|
|
|
|
))
|
|
|
|
self.embycursor.execute(query, (name, tagid, mediafolderid))
|
|
|
|
|
2016-02-25 15:28:42 +11:00
|
|
|
def removeView(self, viewid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM view",
|
|
|
|
"WHERE view_id = ?"
|
|
|
|
))
|
|
|
|
self.embycursor.execute(query, (viewid,))
|
|
|
|
|
2016-03-18 02:03:02 +11:00
|
|
|
def getItem_byFileId(self, fileId, kodiType):
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2016-03-18 02:03:02 +11:00
|
|
|
Returns the Plex itemId by using the Kodi fileId. VIDEO ONLY
|
|
|
|
|
|
|
|
kodiType: 'movie', 'episode', ...
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT emby_id",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE kodi_fileid = ? AND media_type = ?"
|
|
|
|
))
|
|
|
|
try:
|
|
|
|
self.embycursor.execute(query, (fileId, kodiType))
|
|
|
|
item = self.embycursor.fetchone()[0]
|
|
|
|
return item
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getMusicItem_byFileId(self, fileId, kodiType):
|
|
|
|
"""
|
|
|
|
Returns the Plex itemId by using the Kodi fileId. MUSIC ONLY
|
|
|
|
|
|
|
|
kodiType: 'song'
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT emby_id",
|
|
|
|
"FROM emby",
|
2016-03-18 02:03:02 +11:00
|
|
|
"WHERE kodi_id = ? AND media_type = ?"
|
2016-03-12 00:42:14 +11:00
|
|
|
))
|
|
|
|
try:
|
2016-03-18 02:03:02 +11:00
|
|
|
self.embycursor.execute(query, (fileId, kodiType))
|
2016-03-12 00:42:14 +11:00
|
|
|
item = self.embycursor.fetchone()[0]
|
|
|
|
return item
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def getItem_byId(self, plexid):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT kodi_id, kodi_fileid, kodi_pathid, parent_id, media_type, emby_type",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE emby_id = ?"
|
|
|
|
))
|
2016-01-12 02:53:41 +11:00
|
|
|
try:
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid,))
|
2016-04-26 21:53:19 +10:00
|
|
|
item = self.embycursor.fetchone()
|
2016-01-12 02:53:41 +11:00
|
|
|
return item
|
|
|
|
except: return None
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def getItem_byWildId(self, plexid):
|
2016-03-16 22:13:47 +11:00
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT kodi_id, media_type",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE emby_id LIKE ?"
|
|
|
|
))
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid+"%",))
|
2016-04-26 21:53:19 +10:00
|
|
|
return self.embycursor.fetchall()
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getItem_byView(self, mediafolderid):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT kodi_id",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE media_folder = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (mediafolderid,))
|
|
|
|
return self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-04-11 20:34:24 +10:00
|
|
|
def getPlexId(self, kodiid, mediatype):
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
|
|
|
Returns the Plex ID usind the Kodiid. Result:
|
|
|
|
(Plex Id, Parent's Plex Id)
|
|
|
|
"""
|
|
|
|
query = ' '.join((
|
|
|
|
"SELECT emby_id, parent_id",
|
|
|
|
"FROM emby",
|
2016-04-11 20:34:24 +10:00
|
|
|
"WHERE kodi_id = ? AND media_type = ?"
|
2016-03-12 00:42:14 +11:00
|
|
|
))
|
|
|
|
try:
|
2016-04-11 20:34:24 +10:00
|
|
|
self.embycursor.execute(query, (kodiid, mediatype))
|
2016-03-12 00:42:14 +11:00
|
|
|
item = self.embycursor.fetchone()
|
|
|
|
return item
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getItem_byKodiId(self, kodiid, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT emby_id, parent_id",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE kodi_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (kodiid, mediatype,))
|
|
|
|
return self.embycursor.fetchone()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def getItem_byParentId(self, parentid, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT emby_id, kodi_id, kodi_fileid",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE parent_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (parentid, mediatype,))
|
|
|
|
return self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def getItemId_byParentId(self, parentid, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT emby_id, kodi_id",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE parent_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (parentid, mediatype,))
|
|
|
|
return self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def getChecksum(self, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT emby_id, checksum",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE emby_type = ?"
|
|
|
|
))
|
2016-04-26 21:53:19 +10:00
|
|
|
self.embycursor.execute(query, (mediatype,))
|
|
|
|
return self.embycursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def getMediaType_byId(self, plexid):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"SELECT emby_type",
|
|
|
|
"FROM emby",
|
|
|
|
"WHERE emby_id = ?"
|
|
|
|
))
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2016-04-26 21:53:19 +10:00
|
|
|
itemtype = self.embycursor.fetchone()[0]
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
itemtype = None
|
|
|
|
|
|
|
|
return itemtype
|
|
|
|
|
|
|
|
def sortby_mediaType(self, itemids, unsorted=True):
|
|
|
|
|
|
|
|
sorted_items = {}
|
|
|
|
|
|
|
|
for itemid in itemids:
|
|
|
|
mediatype = self.getMediaType_byId(itemid)
|
|
|
|
if mediatype:
|
|
|
|
sorted_items.setdefault(mediatype, []).append(itemid)
|
|
|
|
elif unsorted:
|
|
|
|
sorted_items.setdefault('Unsorted', []).append(itemid)
|
|
|
|
|
|
|
|
return sorted_items
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def addReference(self, plexid, kodiid, embytype, mediatype, fileid=None, pathid=None,
|
2015-12-25 06:51:47 +11:00
|
|
|
parentid=None, checksum=None, mediafolderid=None):
|
|
|
|
query = (
|
|
|
|
'''
|
|
|
|
INSERT OR REPLACE INTO emby(
|
|
|
|
emby_id, kodi_id, kodi_fileid, kodi_pathid, emby_type, media_type, parent_id,
|
|
|
|
checksum, media_folder)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
)
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid, kodiid, fileid, pathid, embytype, mediatype,
|
2015-12-25 06:51:47 +11:00
|
|
|
parentid, checksum, mediafolderid))
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def updateReference(self, plexid, checksum):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "UPDATE emby SET checksum = ? WHERE emby_id = ?"
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (checksum, plexid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def updateParentId(self, plexid, parent_kodiid):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "UPDATE emby SET parent_id = ? WHERE emby_id = ?"
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (parent_kodiid, plexid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def removeItems_byParentId(self, parent_kodiid, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM emby",
|
|
|
|
"WHERE parent_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
|
|
|
self.embycursor.execute(query, (parent_kodiid, mediatype,))
|
|
|
|
|
|
|
|
def removeItem_byKodiId(self, kodiid, mediatype):
|
|
|
|
|
|
|
|
query = ' '.join((
|
|
|
|
|
|
|
|
"DELETE FROM emby",
|
|
|
|
"WHERE kodi_id = ?",
|
|
|
|
"AND media_type = ?"
|
|
|
|
))
|
|
|
|
self.embycursor.execute(query, (kodiid, mediatype,))
|
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def removeItem(self, plexid):
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
query = "DELETE FROM emby WHERE emby_id = ?"
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid,))
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def removeWildItem(self, plexid):
|
2016-03-16 22:13:47 +11:00
|
|
|
|
|
|
|
query = "DELETE FROM emby WHERE emby_id LIKE ?"
|
2016-05-31 16:06:42 +10:00
|
|
|
self.embycursor.execute(query, (plexid+"%",))
|
2016-01-12 02:53:41 +11:00
|
|
|
|