Esempio n. 1
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"')
Esempio n. 2
0
def _setRemoteMovieTag(imdbId, postdata):
    global recentlyFailedMovies
    global updatedMovies
    
    try:
        if(imdbId not in recentlyFailedMovies):
            mmdb.setRemoteMovieTag(imdbId,postdata) 
            updatedMovies += [imdbId] ## adding to updated list
            return True
        else:
            debug('Movie was on failed list, and did not update')           
    except urllib2.URLError, e:
        if(e.code == 404):
            recentlyFailedMovies += [imdbId] #Adding movie to failed movies list,  these will not be updated next sync
            debug('Adding movie to failed list.')
            return False
Esempio n. 3
0
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 ---')
Esempio n. 4
0
# 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')
    
    # Var to check if we're in autoexec.py
    found = False
    autostart = addon.getSetting('autostart') == 'true'
    autoexecfile = file(AUTOEXEC_PATH, 'r')
    filecontents = autoexecfile.readlines()
    autoexecfile.close()
    
    # Check if we're in it
    for line in filecontents:
        if line.find(addon.getAddonInfo('id')) > 0:
            debug('Found ourselves in autoexec')
            found = True
    
    # If the autoexec.py file is found and we're not in it,
Esempio n. 5
0
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 ---')
Esempio n. 6
0
    # Constants
    mmdb = MMDB(addon.getSetting('username'),addon.getSetting('password'))
    xbmcApp = XBMCApp(xbmc.translatePath('special://database/%s' % addon.getSetting('database')))

    # Globals
    mmdb_library = []
    recentlyFailedMedia = []
    updatedMovies = []

    # Print addon information
    print "[ADDON] '%s: version %s' initialized!" % (addon.getAddonInfo('name'), addon.getAddonInfo('version'))
    if(addon.getSetting('shownotifications') == 'true'):
        xbmc.executebuiltin('Notification(%s,%s,%s,%s)' % (addon.getAddonInfo('name'),'is running!',3000,addon.getAddonInfo("icon")))
    
    # Main logic
    debug('initial import of mmdb library')
    getRemoteLibrary() #initial fetch
            
    syncWithMmdbRunsCounter= 0
    
    while not xbmc.abortRequested:
        debug('Syncing local library with mmdb')
        syncWithMMDB()       
        sleeper(300000) #5minutes
        syncWithMmdbRunsCounter += 1
        if syncWithMmdbRunsCounter % 12 == 0: #60minutes
            del recentlyFailedMedia[:]   # Will clear the failedmovies list, since we now got a newer remote medialibrary
            getRemoteLibrary()
            debug('Scheduled import of mmdb library')

except RuntimeWarning as e: