diff --git a/resources/lib/PlexFunctions.py b/resources/lib/PlexFunctions.py index 9202078a..b075b3df 100644 --- a/resources/lib/PlexFunctions.py +++ b/resources/lib/PlexFunctions.py @@ -194,8 +194,7 @@ def GetPlexSectionResults(viewId, headerOptions={}): return result -def GetAllPlexLeaves(viewId, lastViewedAt=None, updatedAt=None, - headerOptions={}): +def GetAllPlexLeaves(viewId, lastViewedAt=None, updatedAt=None): """ Returns a list (raw XML API dump) of all Plex subitems for the key. (e.g. /library/sections/2/allLeaves pointing to all TV shows) @@ -206,7 +205,6 @@ def GetAllPlexLeaves(viewId, lastViewedAt=None, updatedAt=None, since that point of time until now. updatedAt Unix timestamp; only retrieves PMS items updated by the PMS since that point of time until now. - headerOptions to override any download headers If lastViewedAt and updatedAt=None, ALL PMS items are returned. @@ -216,14 +214,16 @@ def GetAllPlexLeaves(viewId, lastViewedAt=None, updatedAt=None, e.g. when server and client are in different time zones. """ args = [] - url = "{server}/library/sections/%s/allLeaves?" % viewId + url = "{server}/library/sections/%s/allLeaves" % viewId + if lastViewedAt: args.append('lastViewedAt>=%s' % lastViewedAt) if updatedAt: args.append('updatedAt>=%s' % updatedAt) - args = '&'.join(args) - xml = downloadutils.DownloadUtils().downloadUrl( - url+args, headerOptions=headerOptions) + if args: + url += '?' + '&'.join(args) + + xml = downloadutils.DownloadUtils().downloadUrl(url) try: xml.attrib diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index e830d0c4..2dba8af6 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -32,7 +32,7 @@ requests.packages.urllib3.disable_warnings(InsecureRequestWarning) class DownloadUtils(): # Borg - multiple instances, shared state - _shared_state = {} + # _shared_state = {} clientInfo = clientinfo.ClientInfo() # Requests session @@ -41,7 +41,8 @@ class DownloadUtils(): def __init__(self): - self.__dict__ = self._shared_state + # self.__dict__ = self._shared_state + pass def setUsername(self, username): # Reserved for userclient only diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 37150ad3..b65b1c79 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -224,6 +224,26 @@ class Items(object): icon="special://home/addons/plugin.video.plexkodiconnect/icon.png", sound=False) + def updateUserdata(self, xml): + """ + Updates the Kodi watched state of the item from PMS. Also retrieves + Plex resume points for movies in progress. + """ + self.logMsg("Entering updateUserdata", 1) + for mediaitem in xml: + API = PlexAPI.API(mediaitem) + itemid = API.getRatingKey() + # Get key and db entry on the Kodi db side + fileid = self.emby_db.getItem_byId(itemid)[1] + # Grab the user's viewcount, resume points etc. from PMS' answer + userdata = API.getUserData() + # Write to Kodi DB + self.kodi_db.addPlaystate(fileid, + userdata['Resume'], + userdata['Runtime'], + userdata['PlayCount'], + userdata['LastPlayedDate']) + class Movies(Items): @@ -255,26 +275,6 @@ class Movies(Items): count += 1 self.add_updateBoxset(boxset) - def updateUserdata(self, itemList): - """ - Updates the Kodi watched state of the item from PMS. Also retrieves - Plex resume points for movies in progress. - """ - API = PlexAPI.API(itemList) - for itemNumber in range(len(itemList)): - API.setChildNumber(itemNumber) - itemid = API.getRatingKey() - # Get key and db entry on the Kodi db side - fileid = self.emby_db.getItem_byId(itemid)[1] - # Grab the user's viewcount, resume points etc. from PMS' answer - userdata = API.getUserData() - # Write to Kodi DB - self.kodi_db.addPlaystate(fileid, - userdata['Resume'], - userdata['Runtime'], - userdata['PlayCount'], - userdata['LastPlayedDate']) - def add_update(self, item, viewtag=None, viewid=None): self.logMsg("Entering add_update", 1) # Process single movie @@ -886,26 +886,6 @@ class TVShows(Items): if not pdialog and self.contentmsg: self.contentPop(title) - def updateUserdata(self, itemList): - """ - Updates the Kodi watched state of the item from PMS. Also retrieves - Plex resume points for movies in progress. - """ - API = PlexAPI.API(itemList) - for itemNumber in range(len(itemList)): - API.setChildNumber(itemNumber) - itemid = API.getRatingKey() - # Get key and db entry on the Kodi db side - fileid = self.emby_db.getItem_byId(itemid)[1] - # Grab the user's viewcount, resume points etc. from PMS' answer - userdata = API.getUserData() - # Write to Kodi DB - self.kodi_db.addPlaystate(fileid, - userdata['Resume'], - userdata['Runtime'], - userdata['PlayCount'], - userdata['LastPlayedDate']) - def add_update(self, item, viewtag=None, viewid=None): # Process single tvshow kodicursor = self.kodicursor diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 86210897..09d65caf 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -314,7 +314,8 @@ class LibrarySync(threading.Thread): if self.updatelist: if self.updatelist[0]['itemType'] in ['Movies', 'TVShows']: updateKodiVideoLib = True - self.GetAndProcessXMLs(plexType) + self.GetAndProcessXMLs( + PlexFunctions.GetItemClassFromType(plexType)) self.updatelist = [] # Let Kodi grab the artwork now if updateKodiVideoLib: @@ -791,17 +792,13 @@ class LibrarySync(threading.Thread): also updates resume times. This is done by downloading one XML for ALL elements with viewId """ - # Download XML, not JSON, because PMS JSON seems to be damaged - headerOptions = {'Accept': 'application/xml'} - plexItems = PlexFunctions.GetAllPlexLeaves( - viewId, - lastViewedAt=lastViewedAt, - updatedAt=updatedAt, - headerOptions=headerOptions) - if plexItems: + xml = PlexFunctions.GetAllPlexLeaves(viewId, + lastViewedAt=lastViewedAt, + updatedAt=updatedAt) + if xml: itemMth = getattr(itemtypes, itemType) with itemMth() as method: - method.updateUserdata(plexItems) + method.updateUserdata(xml) def musicvideos(self, embycursor, kodicursor, pdialog): # Get musicvideos from emby @@ -1147,19 +1144,19 @@ class LibrarySync(threading.Thread): else: # Run full lib scan approx every 10min if count % 600 == 0: - self.logMsg('Running maintainViews() scan', 1) utils.window('emby_dbScan', value="true") + self.logMsg('Running maintainViews() scan', 1) self.fullSync(manualrun=True) - utils.window('emby_dbScan', value="false") count = 0 - # Update views / PMS libraries approx. every 2 minutes + utils.window('emby_dbScan', value="false") + # Update views / PMS libraries approx. every 5 elif count % 120 == 0: self.logMsg('Running maintainViews() scan', 1) utils.window('emby_dbScan', value="true") self.maintainViews() self.startSync() - # Run fast sync approx every 10s - elif count % 10 == 0: + # Run fast sync otherwise (ever second or so) + else: self.startSync() xbmc.sleep(1000) diff --git a/resources/lib/playlist.py b/resources/lib/playlist.py index bafe71ac..48a7d69a 100644 --- a/resources/lib/playlist.py +++ b/resources/lib/playlist.py @@ -50,7 +50,7 @@ class Playlist(): mediatype = embydb_item[4] except TypeError: # Item is not found in our database, add item manually - self.logMsg("Item was not found in the database, manually adding item.", 1) + self.logMsg("Item was not found in the database, manually adding item.", 1) item = self.emby.getItem(itemid) self.addtoPlaylist_xbmc(playlist, item) else: @@ -67,7 +67,7 @@ class Playlist(): # Seek to the starting position seektime = startat / 10000000.0 player.seekTime(seektime) - self.logMsg("Seeking to: %s" % seektime, 1) + self.logMsg("Seeking to: %s" % seektime, 1) self.verifyPlaylist() embycursor.close()