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 04:00:16 +10:00
|
|
|
from utils import kodiSQL
|
2016-08-30 02:44:27 +10:00
|
|
|
import logging
|
|
|
|
|
2016-08-30 04:00:16 +10:00
|
|
|
###############################################################################
|
2016-08-30 02:44:27 +10:00
|
|
|
|
2016-08-30 04:00:16 +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
|
|
|
|
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
class Get_Plex_DB():
|
2016-02-10 21:00:32 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
Usage: with Get_Plex_DB() as plex_db:
|
|
|
|
plex_db.do_something()
|
2016-02-10 21:00:32 +11:00
|
|
|
|
|
|
|
On exiting "with" (no matter what), commits get automatically committed
|
|
|
|
and the db gets closed
|
|
|
|
"""
|
|
|
|
def __enter__(self):
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexconn = kodiSQL('plex')
|
2017-01-08 06:11:48 +11:00
|
|
|
return Plex_DB_Functions(self.plexconn.cursor())
|
2016-02-10 21:00:32 +11:00
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexconn.commit()
|
|
|
|
self.plexconn.close()
|
2016-02-10 21:00:32 +11:00
|
|
|
|
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
class Plex_DB_Functions():
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
def __init__(self, plexcursor):
|
|
|
|
self.plexcursor = plexcursor
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def getViews(self):
|
2017-01-08 06:11:48 +11:00
|
|
|
"""
|
|
|
|
Returns a list of view_id
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
views = []
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT view_id
|
|
|
|
FROM view
|
|
|
|
'''
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexcursor.execute(query)
|
|
|
|
rows = self.plexcursor.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):
|
2017-01-08 06:11:48 +11:00
|
|
|
"""
|
|
|
|
Returns a list of dicts:
|
|
|
|
{'id': view_id, 'name': view_name, 'itemtype': kodi_type}
|
|
|
|
"""
|
2017-01-05 06:57:16 +11:00
|
|
|
plexcursor = self.plexcursor
|
2016-01-28 06:41:28 +11:00
|
|
|
views = []
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT view_id, view_name, kodi_type
|
|
|
|
FROM view
|
|
|
|
'''
|
2017-01-05 06:57:16 +11:00
|
|
|
plexcursor.execute(query)
|
|
|
|
rows = plexcursor.fetchall()
|
2016-01-28 06:41:28 +11:00
|
|
|
for row in rows:
|
|
|
|
views.append({'id': row[0],
|
|
|
|
'name': row[1],
|
|
|
|
'itemtype': row[2]})
|
|
|
|
return views
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getView_byId(self, view_id):
|
|
|
|
"""
|
|
|
|
Returns tuple (view_name, kodi_type, kodi_tagid) for view_id
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT view_name, kodi_type, kodi_tagid
|
|
|
|
FROM view
|
|
|
|
WHERE view_id = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (view_id,))
|
2017-01-05 06:57:16 +11:00
|
|
|
view = self.plexcursor.fetchone()
|
2015-12-25 06:51:47 +11:00
|
|
|
return view
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getView_byType(self, kodi_type):
|
|
|
|
"""
|
|
|
|
Returns a list of dicts for kodi_type:
|
|
|
|
{'id': view_id, 'name': view_name, 'itemtype': kodi_type}
|
|
|
|
"""
|
2015-12-25 06:51:47 +11:00
|
|
|
views = []
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT view_id, view_name, kodi_type
|
|
|
|
FROM view
|
|
|
|
WHERE kodi_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (kodi_type,))
|
2017-01-05 06:57:16 +11:00
|
|
|
rows = self.plexcursor.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
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getView_byName(self, view_name):
|
|
|
|
"""
|
|
|
|
Returns the view_id for view_name (or None)
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT view_id
|
|
|
|
FROM view
|
|
|
|
WHERE view_name = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (view_name,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
view = self.plexcursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
view = None
|
|
|
|
return view
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def addView(self, view_id, view_name, kodi_type, kodi_tagid):
|
|
|
|
"""
|
|
|
|
Appends an entry to the view table
|
|
|
|
"""
|
|
|
|
query = '''
|
2015-12-25 06:51:47 +11:00
|
|
|
INSERT INTO view(
|
2017-01-08 06:11:48 +11:00
|
|
|
view_id, view_name, kodi_type, kodi_tagid)
|
2015-12-25 06:51:47 +11:00
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
2017-01-08 06:11:48 +11:00
|
|
|
self.plexcursor.execute(query,
|
|
|
|
(view_id, view_name, kodi_type, kodi_tagid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def updateView(self, view_name, kodi_tagid, view_id):
|
|
|
|
"""
|
|
|
|
Updates the view_id with view_name and kodi_tagid
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
UPDATE view
|
|
|
|
SET view_name = ?, kodi_tagid = ?
|
|
|
|
WHERE view_id = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (view_name, kodi_tagid, view_id))
|
|
|
|
|
|
|
|
def removeView(self, view_id):
|
|
|
|
query = '''
|
|
|
|
DELETE FROM view
|
|
|
|
WHERE view_id = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (view_id,))
|
|
|
|
|
|
|
|
def getItem_byFileId(self, kodi_fileid, kodi_type):
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
Returns plex_id for kodi_fileid and kodi_type
|
2016-03-18 02:03:02 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
None if not found
|
2016-03-18 02:03:02 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT plex_id
|
|
|
|
FROM plex
|
|
|
|
WHERE kodi_fileid = ? AND kodi_type = ?
|
|
|
|
'''
|
2016-03-18 02:03:02 +11:00
|
|
|
try:
|
2017-01-08 06:11:48 +11:00
|
|
|
self.plexcursor.execute(query, (kodi_fileid, kodi_type))
|
2017-01-05 06:57:16 +11:00
|
|
|
item = self.plexcursor.fetchone()[0]
|
2016-03-18 02:03:02 +11:00
|
|
|
return item
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getMusicItem_byFileId(self, kodi_id, kodi_type):
|
2016-03-18 02:03:02 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
Returns the plex_id for kodi_id and kodi_type
|
2016-03-18 02:03:02 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
None if not found
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT plex_id
|
|
|
|
FROM plex
|
|
|
|
WHERE kodi_id = ? AND kodi_type = ?
|
|
|
|
'''
|
2016-03-12 00:42:14 +11:00
|
|
|
try:
|
2017-01-08 06:11:48 +11:00
|
|
|
self.plexcursor.execute(query, (kodi_id, kodi_type))
|
2017-01-05 06:57:16 +11:00
|
|
|
item = self.plexcursor.fetchone()[0]
|
2016-03-12 00:42:14 +11:00
|
|
|
return item
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItem_byId(self, plex_id):
|
|
|
|
"""
|
|
|
|
For plex_id, returns the tuple
|
|
|
|
(kodi_id, kodi_fileid, kodi_pathid, parent_id, kodi_type, plex_type)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
None if not found
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT kodi_id, kodi_fileid, kodi_pathid,
|
|
|
|
parent_id, kodi_type, plex_type
|
|
|
|
FROM plex
|
|
|
|
WHERE plex_id = ?
|
|
|
|
'''
|
2016-01-12 02:53:41 +11:00
|
|
|
try:
|
2017-01-08 06:11:48 +11:00
|
|
|
self.plexcursor.execute(query, (plex_id,))
|
2017-01-05 06:57:16 +11:00
|
|
|
item = self.plexcursor.fetchone()
|
2016-01-12 02:53:41 +11:00
|
|
|
return item
|
2017-01-08 06:11:48 +11:00
|
|
|
except:
|
|
|
|
return None
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItem_byWildId(self, plex_id):
|
|
|
|
"""
|
|
|
|
Returns a list of tuples (kodi_id, kodi_type) for plex_id (% appended)
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT kodi_id, kodi_type
|
|
|
|
FROM plex
|
|
|
|
WHERE plex_id LIKE ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (plex_id+"%",))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchall()
|
2016-03-16 22:13:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItem_byView(self, view_id):
|
|
|
|
"""
|
|
|
|
Returns kodi_id for view_id
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT kodi_id
|
|
|
|
FROM plex
|
|
|
|
WHERE view_id = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (view_id,))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItem_byKodiId(self, kodi_id, kodi_type):
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
Returns the tuple (plex_id, parent_id) for kodi_id and kodi_type
|
2016-03-12 00:42:14 +11:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT plex_id, parent_id
|
|
|
|
FROM plex
|
|
|
|
WHERE kodi_id = ?
|
|
|
|
AND kodi_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (kodi_id, kodi_type,))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchone()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItem_byParentId(self, parent_id, kodi_type):
|
|
|
|
"""
|
|
|
|
Returns the tuple (plex_id, kodi_id, kodi_fileid) for parent_id,
|
|
|
|
kodi_type
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT plex_id, kodi_id, kodi_fileid
|
|
|
|
FROM plex
|
|
|
|
WHERE parent_id = ?
|
|
|
|
AND kodi_type = ?"
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (parent_id, kodi_type,))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getItemId_byParentId(self, parent_id, kodi_type):
|
|
|
|
"""
|
|
|
|
Returns the tuple (plex_id, kodi_id) for parent_id, kodi_type
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT plex_id, kodi_id
|
|
|
|
FROM plex
|
|
|
|
WHERE parent_id = ?
|
|
|
|
AND kodi_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (parent_id, kodi_type,))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getChecksum(self, plex_type):
|
|
|
|
"""
|
|
|
|
Returns a list of tuples (plex_id, checksum) for plex_type
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT plex_id, checksum
|
|
|
|
FROM plex
|
|
|
|
WHERE plex_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (plex_type,))
|
2017-01-05 06:57:16 +11:00
|
|
|
return self.plexcursor.fetchall()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def getMediaType_byId(self, plex_id):
|
|
|
|
"""
|
|
|
|
Returns plex_type for plex_id
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
Or None if not found
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
SELECT plex_type
|
|
|
|
FROM plex
|
|
|
|
WHERE plex_id = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (plex_id,))
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
itemtype = self.plexcursor.fetchone()[0]
|
2015-12-25 06:51:47 +11:00
|
|
|
except TypeError:
|
|
|
|
itemtype = None
|
|
|
|
return itemtype
|
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def addReference(self, plex_id, plex_type, kodi_id, kodi_type,
|
|
|
|
kodi_fileid=None, kodi_pathid=None, parent_id=None,
|
|
|
|
checksum=None, view_id=None):
|
|
|
|
"""
|
|
|
|
Appends or replaces an entry into the plex table
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO plex(
|
|
|
|
plex_id, kodi_id, kodi_fileid, kodi_pathid, plex_type,
|
|
|
|
kodi_type, parent_id, checksum, view_id)
|
2015-12-25 06:51:47 +11:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
'''
|
2017-01-08 06:11:48 +11:00
|
|
|
self.plexcursor.execute(query, (plex_id, kodi_id, kodi_fileid,
|
|
|
|
kodi_pathid, plex_type, kodi_type,
|
|
|
|
parent_id, checksum, view_id))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def updateReference(self, plex_id, checksum):
|
|
|
|
"""
|
|
|
|
Updates checksum for plex_id
|
|
|
|
"""
|
|
|
|
query = "UPDATE plex SET checksum = ? WHERE plex_id = ?"
|
|
|
|
self.plexcursor.execute(query, (checksum, plex_id))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
def updateParentId(self, plexid, parent_kodiid):
|
2017-01-08 06:11:48 +11:00
|
|
|
"""
|
|
|
|
Updates parent_id for plex_id
|
|
|
|
"""
|
|
|
|
query = "UPDATE plex SET parent_id = ? WHERE plex_id = ?"
|
2017-01-05 06:57:16 +11:00
|
|
|
self.plexcursor.execute(query, (parent_kodiid, plexid))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def removeItems_byParentId(self, parent_id, kodi_type):
|
|
|
|
"""
|
|
|
|
Removes all entries with parent_id and kodi_type
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
DELETE FROM plex
|
|
|
|
WHERE parent_id = ?
|
|
|
|
AND kodi_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (parent_id, kodi_type,))
|
|
|
|
|
|
|
|
def removeItem_byKodiId(self, kodi_id, kodi_type):
|
|
|
|
"""
|
|
|
|
Removes the one entry with kodi_id and kodi_type
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
DELETE FROM plex
|
|
|
|
WHERE kodi_id = ?
|
|
|
|
AND kodi_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (kodi_id, kodi_type,))
|
|
|
|
|
|
|
|
def removeItem(self, plex_id):
|
|
|
|
"""
|
|
|
|
Removes the one entry with plex_id
|
|
|
|
"""
|
|
|
|
query = "DELETE FROM plex WHERE plex_id = ?"
|
|
|
|
self.plexcursor.execute(query, (plex_id,))
|
2016-09-11 03:49:03 +10:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def removeWildItem(self, plex_id):
|
|
|
|
"""
|
|
|
|
Removes all entries with plex_id with % added
|
2016-09-11 03:49:03 +10:00
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
query = "DELETE FROM plex WHERE plex_id LIKE ?"
|
|
|
|
self.plexcursor.execute(query, (plex_id+"%",))
|
2016-09-11 03:49:03 +10:00
|
|
|
|
2017-01-08 06:11:48 +11:00
|
|
|
def itemsByType(self, plex_type):
|
|
|
|
"""
|
|
|
|
Returns a list of dicts for plex_type:
|
2016-09-11 03:49:03 +10:00
|
|
|
{
|
2017-01-08 06:11:48 +11:00
|
|
|
'plexId': plex_id
|
|
|
|
'kodiId': kodi_id
|
|
|
|
'kodi_type': kodi_type
|
|
|
|
'plex_type': plex_type
|
2016-09-11 03:49:03 +10:00
|
|
|
}
|
|
|
|
"""
|
2017-01-08 06:11:48 +11:00
|
|
|
query = '''
|
|
|
|
SELECT plex_id, kodi_id, kodi_type
|
|
|
|
FROM plex
|
|
|
|
WHERE plex_type = ?
|
|
|
|
'''
|
|
|
|
self.plexcursor.execute(query, (plex_type, ))
|
2016-09-11 03:49:03 +10:00
|
|
|
result = []
|
2017-01-05 06:57:16 +11:00
|
|
|
for row in self.plexcursor.fetchall():
|
2016-09-11 03:49:03 +10:00
|
|
|
result.append({
|
|
|
|
'plexId': row[0],
|
|
|
|
'kodiId': row[1],
|
|
|
|
'kodi_type': row[2],
|
2017-01-08 06:11:48 +11:00
|
|
|
'plex_type': plex_type
|
2016-09-11 03:49:03 +10:00
|
|
|
})
|
|
|
|
return result
|