added GetEmbyDB to get to emby_db more securely

This commit is contained in:
tomkat83 2016-02-10 10:04:49 +01:00
parent 9beac817fe
commit 75644de696
3 changed files with 67 additions and 64 deletions

View file

@ -386,11 +386,8 @@ def getThemeMedia():
return return
# Get every user view Id # Get every user view Id
embyconn = utils.kodiSQL('emby') with utils.GetEmbyDB() as emby_db:
embycursor = embyconn.cursor() viewids = emby_db.getViews()
emby_db = embydb.Embydb_Functions(embycursor)
viewids = emby_db.getViews()
embycursor.close()
# Get Ids with Theme Videos # Get Ids with Theme Videos
itemIds = {} itemIds = {}

View file

@ -72,10 +72,6 @@ class ThreadedGetMetadata(threading.Thread):
updateItem['XML'] = plexXML updateItem['XML'] = plexXML
# place item into out queue # place item into out queue
out_queue.put(updateItem) out_queue.put(updateItem)
del plexXML
del updateItem
# If we don't have a valid XML, don't put that into the queue
# but skip this item for now
# Keep track of where we are at # Keep track of where we are at
with lock: with lock:
getMetadataCount += 1 getMetadataCount += 1
@ -139,7 +135,7 @@ class ThreadedProcessMetadata(threading.Thread):
del plexitem del plexitem
del updateItem del updateItem
# signals to queue job is done # signals to queue job is done
self.queue.task_done() queue.task_done()
@utils.ThreadMethodsAdditionalStop('emby_shouldStop') @utils.ThreadMethodsAdditionalStop('emby_shouldStop')
@ -278,17 +274,16 @@ class LibrarySync(threading.Thread):
self.saveLastSync() self.saveLastSync()
# Get all PMS items already saved in Kodi # Get all PMS items already saved in Kodi
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
# Also get checksums of every Plex items already saved in Kodi # Also get checksums of every Plex items already saved in Kodi
allKodiElementsId = {} allKodiElementsId = {}
for itemtype in PlexFunctions.EmbyItemtypes(): with utils.GetEmbyDB() as emby_db:
try: for itemtype in PlexFunctions.EmbyItemtypes():
allKodiElementsId.update(dict(emby_db.getChecksum(itemtype))) try:
except ValueError: allKodiElementsId.update(
pass dict(emby_db.getChecksum(itemtype)))
embyconn.close() except ValueError:
pass
self.allKodiElementsId = allKodiElementsId self.allKodiElementsId = allKodiElementsId
# Run through views and get latest changed elements using time diff # Run through views and get latest changed elements using time diff
@ -310,7 +305,8 @@ class LibrarySync(threading.Thread):
PlexFunctions.GetItemClassFromType(plexType), PlexFunctions.GetItemClassFromType(plexType),
PlexFunctions.GetMethodFromPlexType(plexType), PlexFunctions.GetMethodFromPlexType(plexType),
view['name'], view['name'],
view['id']) view['id'],
dontCheck=True)
# Process self.updatelist # Process self.updatelist
if self.updatelist: if self.updatelist:
if self.updatelist[0]['itemType'] in ['Movies', 'TVShows']: if self.updatelist[0]['itemType'] in ['Movies', 'TVShows']:
@ -573,7 +569,8 @@ class LibrarySync(threading.Thread):
embyconn.close() embyconn.close()
kodiconn.close() kodiconn.close()
def GetUpdatelist(self, xml, itemType, method, viewName, viewId): def GetUpdatelist(self, xml, itemType, method, viewName, viewId,
dontCheck=False):
""" """
Adds items to self.updatelist as well as self.allPlexElementsId dict Adds items to self.updatelist as well as self.allPlexElementsId dict
@ -584,6 +581,8 @@ class LibrarySync(threading.Thread):
see itemtypes.py see itemtypes.py
viewName: Name of the Plex view (e.g. 'My TV shows') viewName: Name of the Plex view (e.g. 'My TV shows')
viewId: Id/Key of Plex library (e.g. '1') viewId: Id/Key of Plex library (e.g. '1')
dontCheck: If True, skips checksum check but assumes
that all items in xml must be processed
Output: self.updatelist, self.allPlexElementsId Output: self.updatelist, self.allPlexElementsId
self.updatelist APPENDED(!!) list itemids (Plex Keys as self.updatelist APPENDED(!!) list itemids (Plex Keys as
@ -599,16 +598,16 @@ class LibrarySync(threading.Thread):
self.allPlexElementsId APPENDED(!!) dict self.allPlexElementsId APPENDED(!!) dict
= {itemid: checksum} = {itemid: checksum}
""" """
if self.compare: if self.compare or not dontCheck:
# Manual sync # Manual sync
for item in xml: for item in xml:
# Skipping items 'title=All episodes' without a 'ratingKey' # Skipping items 'title=All episodes' without a 'ratingKey'
if not item.attrib.get('ratingKey', False): if not item.attrib.get('ratingKey', False):
continue continue
API = PlexAPI.API(item) API = PlexAPI.API(item)
plex_checksum = API.getChecksum()
itemId = API.getRatingKey() itemId = API.getRatingKey()
title, sorttitle = API.getTitle() title, sorttitle = API.getTitle()
plex_checksum = API.getChecksum()
self.allPlexElementsId[itemId] = plex_checksum self.allPlexElementsId[itemId] = plex_checksum
kodi_checksum = self.allKodiElementsId.get(itemId) kodi_checksum = self.allKodiElementsId.get(itemId)
if kodi_checksum != plex_checksum: if kodi_checksum != plex_checksum:
@ -637,7 +636,6 @@ class LibrarySync(threading.Thread):
'viewName': viewName, 'viewName': viewName,
'viewId': viewId, 'viewId': viewId,
'title': title}) 'title': title})
return
def GetAndProcessXMLs(self, itemType): def GetAndProcessXMLs(self, itemType):
""" """
@ -728,27 +726,20 @@ class LibrarySync(threading.Thread):
# Initialize # Initialize
self.allPlexElementsId = {} self.allPlexElementsId = {}
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
itemType = 'Movies' itemType = 'Movies'
views = [x for x in self.views if x['itemtype'] == 'movie'] views = [x for x in self.views if x['itemtype'] == 'movie']
self.logMsg("Processing Plex %s. Libraries: %s" % (itemType, views), 1) self.logMsg("Processing Plex %s. Libraries: %s" % (itemType, views), 1)
self.allKodiElementsId = {}
if self.compare: if self.compare:
# Get movies from Plex server with utils.GetEmbyDB() as emby_db:
emby_db = embydb.Embydb_Functions(embycursor) # Get movies from Plex server
# Pull the list of movies and boxsets in Kodi # Pull the list of movies and boxsets in Kodi
try: try:
self.allKodiElementsId = dict(emby_db.getChecksum('Movie')) self.allKodiElementsId = dict(emby_db.getChecksum('Movie'))
except ValueError: except ValueError:
self.allKodiElementsId = {} self.allKodiElementsId = {}
else:
# Getting all metadata, hence set Kodi elements to {}
self.allKodiElementsId = {}
embyconn.close()
##### PROCESS MOVIES ##### ##### PROCESS MOVIES #####
self.updatelist = [] self.updatelist = []
@ -853,37 +844,32 @@ class LibrarySync(threading.Thread):
# Initialize # Initialize
self.allPlexElementsId = {} self.allPlexElementsId = {}
itemType = 'TVShows' itemType = 'TVShows'
# Open DB connections
embyconn = utils.kodiSQL('emby')
embycursor = embyconn.cursor()
emby_db = embydb.Embydb_Functions(embycursor)
views = [x for x in self.views if x['itemtype'] == 'show'] views = [x for x in self.views if x['itemtype'] == 'show']
self.logMsg("Media folders for %s: %s" % (itemType, views), 1) self.logMsg("Media folders for %s: %s" % (itemType, views), 1)
self.allKodiElementsId = {} self.allKodiElementsId = {}
if self.compare: if self.compare:
# Get movies from Plex server with utils.GetEmbyDB() as emby_db:
# Pull the list of TV shows already in Kodi # Get movies from Plex server
try: # Pull the list of TV shows already in Kodi
all_koditvshows = dict(emby_db.getChecksum('Series')) try:
self.allKodiElementsId.update(all_koditvshows) all_koditvshows = dict(emby_db.getChecksum('Series'))
except ValueError: self.allKodiElementsId.update(all_koditvshows)
pass except ValueError:
# Same for seasons pass
try: # Same for seasons
all_kodiseasons = dict(emby_db.getChecksum('Season')) try:
self.allKodiElementsId.update(all_kodiseasons) all_kodiseasons = dict(emby_db.getChecksum('Season'))
except ValueError: self.allKodiElementsId.update(all_kodiseasons)
pass except ValueError:
# Same for the episodes (sub-element of shows/series) pass
try: # Same for the episodes (sub-element of shows/series)
all_kodiepisodes = dict(emby_db.getChecksum('Episode')) try:
self.allKodiElementsId.update(all_kodiepisodes) all_kodiepisodes = dict(emby_db.getChecksum('Episode'))
except ValueError: self.allKodiElementsId.update(all_kodiepisodes)
pass except ValueError:
# Close DB connections pass
embyconn.close()
##### PROCESS TV Shows ##### ##### PROCESS TV Shows #####
self.updatelist = [] self.updatelist = []

View file

@ -19,12 +19,32 @@ import xbmcaddon
import xbmcgui import xbmcgui
import xbmcvfs import xbmcvfs
import embydb_functions as embydb
################################################################################################# #################################################################################################
addonName = xbmcaddon.Addon().getAddonInfo('name') addonName = xbmcaddon.Addon().getAddonInfo('name')
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.Embydb_Functions(self.embyconn.cursor())
return self.emby_db
def __exit__(self, type, value, traceback):
self.embyconn.commit()
self.embyconn.close()
def LogTime(func): def LogTime(func):
""" """
Decorator for functions and methods to log the time it took to run the code Decorator for functions and methods to log the time it took to run the code