def movieUpdatedNotifications(): global updatedMovies if(addon.getSetting('shownotifications') == 'true'): moviesUpdatedCounter= updatedMovies.__len__() del updatedMovies[:] if(moviesUpdatedCounter > 0): xbmc.executebuiltin('Notification(%s,%s,%s,%s)' % (addon.getAddonInfo('name'),"%d movies updated on MMDB" % (moviesUpdatedCounter),7000,addon.getAddonInfo("icon")))
def __openRequest(self,request): if addon.getSetting('debug') == 'true': opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1)) else: opener = urllib2.build_opener() response = opener.open(request) headers = response.info() return response
def setLocalMovieAsWatched(self,idFile): if(addon.getSetting('testmode') == 'false'): connection = sqlite.connect(self.moviedb) cursor = connection.cursor() cursor.execute("update files SET playCount=1 where idFile=?",(idFile,)) connection.commit() totalChanges = connection.total_changes connection.close() if totalChanges == 0: raise RuntimeError('Expected 1 updated row, got 0')
def setRemoteMovieTag(self,imdbId,postdata): if(addon.getSetting('testmode') == 'false'): request = self.__makeRequest(self.apiurl+'/userMedia?mediaType=movie&idType=imdb&id=%s' % imdbId) request.add_data(json.dumps(postdata)) request.get_method = lambda: 'PUT' f = self.__openRequest(request) if(f != None): json.load(f) else: debug('MMDB Testmode cancelled API request "setRemoteMovieTag"')
def syncWithMMDB(): #define global access global mmdb_library #sync remote media with local db if(mmdb_library == None): debug("mmdb_library = None, is api down/changed?") return anyRemoteChanges = False anyLocalChanges = False for remoteMedia in mmdb_library: if(remoteMedia['imdbId']) != None: localMedia = xbmcApp.getLocalMovie(remoteMedia['imdbId']) if (localMedia != None): debug('Media exists both locally and remotely - ('+remoteMedia['name']+')') if not remoteMedia['acquired']: debug('Setting remote media status to acquired') _setRemoteMovieTag(remoteMedia['imdbId'],{'acquired':True}) anyRemoteChanges = True if(remoteMedia['experienced'] != localMedia['watched']): debug('watched status is not synchronized') if(addon.getSetting('dontsyncwatched') == 'false'): if(remoteMedia['experienced']): debug('setting local media to watched') xbmcApp.setLocalMovieAsWatched(localMedia['idFile']) anyLocalChanges = True else: debug ('setting remote media to watched') _setRemoteMovieTag(localMedia['imdbId'],{'experienced':localMedia['watched'] == 1}) anyRemoteChanges = True else: debug('Cancelled synchronize of watched status due to settings!') else: debug('Media ('+remoteMedia['name']+') exists only remotely') if(remoteMedia['acquired'] == True): if(addon.getSetting('dontdeleteacquired') == 'false'): debug('Acquired flag was removed from mmdb') _setRemoteMovieTag(remoteMedia['imdbId'],{'acquired':False}) anyRemoteChanges = True else: debug('Acquired flag was not removed from mmdb due to settings!') else: #MISSING IMDBID in REMOTE MOVIE debug('('+remoteMedia['name']+') was missing imdbID, please add it at TMDB.org') #sync local media with remote db for localMedia in xbmcApp.getLocalMovieLibrary(): if(remoteMovieExists(localMedia['imdbId'])): continue debug('Media exists only locally - ('+localMedia['name']+')') if(_setRemoteMovieTag(localMedia['imdbId'],{'acquired': True, 'experienced':localMedia['watched'] == 1})): anyRemoteChanges = True #if it _setRemoteMovieTag fails doesnt set: anyRemoteChanges movieUpdatedNotifications() if(anyRemoteChanges): debug('--- MADE REMOTE UPDATE(S) ---') mmdb_library = mmdb.getRemoteMovieLibrary() #sync local copy with changes on remote elif(anyLocalChanges): debug('--- MADE LOCAL CHANGE(S) ---') else: debug('--- NO CHANGES DETECTED ---')
if(_setRemoteMovieTag(localMedia['imdbId'],{'acquired': True, 'experienced':localMedia['watched'] == 1})): anyRemoteChanges = True #if it _setRemoteMovieTag fails doesnt set: anyRemoteChanges movieUpdatedNotifications() if(anyRemoteChanges): debug('--- MADE REMOTE UPDATE(S) ---') mmdb_library = mmdb.getRemoteMovieLibrary() #sync local copy with changes on remote elif(anyLocalChanges): debug('--- MADE LOCAL CHANGE(S) ---') else: debug('--- NO CHANGES DETECTED ---') # Constants mmdb = MMDB(addon.getSetting('username'),addon.getSetting('password')) xbmcApp = XBMCApp(xbmc.translatePath('special://database/%s' % addon.getSetting('database'))) # Globals mmdb_library = [] recentlyFailedMovies = [] updatedMovies = [] # autoexecute addon on startup for older xbmc versions, remove this when xbmc.service goes live # Auto exec info AUTOEXEC_PATH = xbmc.translatePath( 'special://home/userdata/autoexec.py' ) AUTOEXEC_FOLDER_PATH = xbmc.translatePath( 'special://home/userdata/' ) AUTOEXEC_SCRIPT = '\nimport time;time.sleep(5);xbmc.executebuiltin("XBMC.RunScript(special://home/addons/%s/default.py)")\n' % addon.getAddonInfo('id') # See if the autoexec.py file exists if (os.path.exists(AUTOEXEC_PATH)): debug('Found autoexec')
def syncWithMMDB(): #define global access global mmdb_library #sync remote media with local db if mmdb_library is None: debug("mmdb_library = None, is api down/changed?") return anyRemoteChanges = False anyLocalChanges = False for remoteData in mmdb_library: remoteMedia = remoteData['media'] remoteTags = remoteData['tags'] if remoteData['type'] == 'movie': if remoteMedia['imdbId'] is not None: localMedia = xbmcApp.getLocalMovie(remoteMedia['imdbId']) if localMedia is not None: debug('Media exists both locally and remotely - ('+remoteMedia['name']+')') if ACQUIRED not in remoteTags: debug('Setting remote media status to acquired ('+remoteMedia['name']+'['+localMedia['imdbId']+','+remoteMedia['id']+']).') mmdb.addRemoteMediaTag(remoteMedia['id'],ACQUIRED) anyRemoteChanges = True if (WATCHED in remoteTags) != localMedia[WATCHED]: debug('watched status is not synchronized') if addon.getSetting('dontsyncwatched') == 'false': if WATCHED in remoteTags: debug('setting local media to watched') xbmcApp.setLocalFileAsWatched(localMedia['idFile']) anyLocalChanges = True else: debug ('setting remote media to watched ('+remoteMedia['name']+'['+localMedia['imdbId']+','+remoteMedia['id']+']).') mmdb.addRemoteMediaTag(remoteMedia['id'],WATCHED) anyRemoteChanges = True else: debug('Cancelled synchronize of watched status due to settings!') else: debug('Media ('+remoteMedia['name']+') exists only remotely') if ACQUIRED in remoteTags: if(addon.getSetting('dontdeleteacquired') == 'false'): debug('Acquired flag was removed from mmdb ('+remoteMedia['name']+'['+remoteMedia['imdbId']+']).') mmdb.removeRemoteMediaTag(remoteMedia['id'],ACQUIRED) anyRemoteChanges = True else: debug('Acquired flag was not removed from mmdb due to settings!') elif remoteData['type'] == 'episode': if remoteMedia['ttdbId'] is not None: localMedia = xbmcApp.getLocalEpisode(remoteMedia['ttdbId'],remoteMedia['season'],remoteMedia['episodeNumber']) if localMedia is not None: debug('Media exists both locally and remotely - ('+remoteMedia['name']+')') if ACQUIRED not in remoteTags: debug('Setting remote media status to acquired ('+remoteMedia['name']+'['+localMedia['ttdbId']+','+remoteMedia['id']+']).') mmdb.addRemoteMediaTag(remoteMedia['id'],ACQUIRED) anyRemoteChanges = True if (WATCHED in remoteTags) != localMedia[WATCHED]: debug('watched status is not synchronized') if addon.getSetting('dontsyncwatched') == 'false': if WATCHED in remoteTags: debug('setting local media to watched') xbmcApp.setLocalFileAsWatched(localMedia['idFile']) anyLocalChanges = True else: debug ('setting remote media to watched ('+remoteMedia['name']+'['+localMedia['ttdbId']+','+remoteMedia['id']+']).') mmdb.addRemoteMediaTag(remoteMedia['id'],WATCHED) anyRemoteChanges = True else: debug('Cancelled synchronize of watched status due to settings!') else: debug('Media ('+remoteMedia['name']+') exists only remotely') if ACQUIRED in remoteTags: if(addon.getSetting('dontdeleteacquired') == 'false'): debug('Acquired flag was removed from mmdb ('+remoteMedia['name']+'['+remoteMedia['id']+']).') mmdb.removeRemoteMediaTag(remoteMedia['id'],ACQUIRED) anyRemoteChanges = True else: debug('Acquired flag was not removed from mmdb due to settings!') else: raise RuntimeError('type matched nothing?') #sync local media with remote db localLibrary = [] localLibrary.extend(xbmcApp.getLocalEpisodeLibrary()) localLibrary.extend(xbmcApp.getLocalMovieLibrary()) for localMedia in localLibrary: if 'seriesName' in localMedia: #is series/episode debug('Episode exists only locally - ('+localMedia['seriesName']+' - ['+localMedia['season']+'x'+localMedia['episode']+'] '+' - '+localMedia['name']+')') # TODO should cache the mmdb id <-> imdb id connection found in this search to prevent uneccesary load on # mymediadb.org result = mmdb.search(localMedia['ttdbId']) if 'series' in result and len(result['series']) >= 1: #Might be several results, however picking the first should suffice. seriesId = result['series'][0]['id'] episodes = mmdb.getRemoteSeriesEpisodes(seriesId) for episode in episodes: if str(episode['season']) == str(localMedia['season']) and str(episode['episodeNumber']) == str(localMedia['episode']): mmdbId = episode['id'] mmdb.addRemoteMediaTag(mmdbId,ACQUIRED) if localMedia[WATCHED] == 1: mmdb.addRemoteMediaTag(mmdbId,WATCHED) break else: #is movie if remoteMovieExists(localMedia['imdbId']): continue debug('Movie exists only locally - ('+localMedia['name']+')') # TODO should cache the mmdb id <-> imdb id connection found in this search to prevent uneccesary load on # mymediadb.org result = mmdb.search(localMedia['imdbId']) if 'movie' in result and len(result['movie']) >= 1: #Might be several results, however picking the first should suffice. mmdbId = result['movie'][0]['id'] mmdb.addRemoteMediaTag(mmdbId,ACQUIRED) if localMedia[WATCHED] == 1: mmdb.addRemoteMediaTag(mmdbId,WATCHED) anyRemoteChanges = True movieUpdatedNotifications() if anyRemoteChanges: debug('--- MADE REMOTE UPDATE(S) ---') getRemoteLibrary() elif anyLocalChanges: debug('--- MADE LOCAL CHANGE(S) ---') else: debug('--- NO CHANGES DETECTED ---')