Exemplo n.º 1
0
    def _update_show(self, trakt_id, show_item, get_meta=True):
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        cursor.execute('SELECT * FROM shows WHERE trakt_id=?', (trakt_id, ))
        old_entry = cursor.fetchone()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)

        if get_meta:
            try:
                kodi_meta = tvdb.TVDBAPI().seriesIDToListItem(show_item)
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = tmdb.TMDBAPI().showToListItem(show_item)
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = imdb.IMDBScraper().showToListItem(show_item)
                if kodi_meta is None or kodi_meta == '{}':
                    return
                update_time = str(
                    datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
            except:
                return None
        else:
            if old_entry is None:
                kodi_meta = {}
                update_time = self.base_date
            else:
                update_time = old_entry['last_updated']
                kodi_meta = old_entry['kodi_meta']
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        try:
            cursor.execute('PRAGMA foreign_keys=OFF')
            cursor.execute(
                "REPLACE INTO shows ("
                "trakt_id, kodi_meta, last_updated, air_date)"
                "VALUES "
                "(?, ?, ?, ?)", (int(trakt_id), str(kodi_meta), update_time,
                                 kodi_meta['info']['premiered']))
            cursor.execute('PRAGMA foreign_keys=ON')
            cursor.connection.commit()
            cursor.close()

            return {
                'trakt_id': trakt_id,
                'kodi_meta': kodi_meta,
                'last_updated': update_time
            }
        except:
            cursor.close()

            import traceback
            traceback.print_exc()
            pass
        finally:
            tools.try_release_lock(tools.traktSyncDB_lock)
Exemplo n.º 2
0
    def _update_season(self, show_meta, season_meta, get_meta=True):

        if get_meta:
            try:
                kodi_meta = tvdb.TVDBAPI().seasonIDToListItem(
                    season_meta, show_meta)
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = tmdb.TMDBAPI().showSeasonToListItem(
                        season_meta, show_meta)
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = imdb.IMDBScraper().showSeasonToListItem(
                        season_meta, show_meta)
            except:
                return None
        else:
            kodi_meta = {}

        season = season_meta['number']
        show_id = show_meta['ids']['trakt']
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        try:
            cursor.execute(
                "REPLACE INTO seasons ("
                "show_id, season, kodi_meta, air_date)"
                "VALUES "
                "(?, ?, ?, ?)", (int(show_id), str(season), str(kodi_meta),
                                 kodi_meta['info']['aired']))
            cursor.connection.commit()
            cursor.close()
            return {
                'show_id': show_meta['ids']['trakt'],
                'season': season,
                'kodi_meta': kodi_meta
            }

        except:
            cursor.close()
            import traceback
            traceback.print_exc()
            pass
        finally:
            tools.try_release_lock(tools.traktSyncDB_lock)
Exemplo n.º 3
0
    def _update_episode(self, show_id, episode_object, get_meta=True, watched=None, collected=None):
        episode_id = episode_object['ids']['trakt']
        season = episode_object['season']
        old_entry = None
        number = episode_object['number']

        show_meta = self.get_single_show(show_id, get_meta=get_meta)
        if show_meta is None:
            return

        season_meta = self.get_single_season(show_id, season, get_meta=get_meta)
        if season_meta is None:
            return

        try:
            tools.traktSyncDB_lock.acquire()
            cursor = self._get_cursor()
            cursor.execute("SELECT * FROM episodes WHERE trakt_id=?", (episode_id,))
            old_entry = cursor.fetchone()
            cursor.close()
            tools.try_release_lock(tools.traktSyncDB_lock)
        except:
            pass

        if show_meta == '{}' and get_meta:
            return

        show_meta = {'showInfo': show_meta, 'seasonInfo': season_meta}

        if (get_meta and old_entry is None) or (get_meta and old_entry['kodi_meta'] == '{}'):
            try:
                kodi_meta = tmdb.TMDBAPI().episodeIDToListItem(episode_object, copy.deepcopy(show_meta))
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = tvdb.TVDBAPI().episodeIDToListItem(episode_object, copy.deepcopy(show_meta))
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = imdb.IMDBScraper().episodeIDToListItem(episode_object, copy.deepcopy(show_meta))
                if kodi_meta is None or kodi_meta == '{}':
                    return
            except:
                return None
            kodi_meta.pop('showInfo')
            update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        else:
            if old_entry is None:
                update_time = self.base_date
                kodi_meta = {}
            else:
                update_time = old_entry['last_updated']
                kodi_meta = old_entry['kodi_meta']

        if old_entry is None:
            old_entry = {'collected': 0, 'watched': 0, 'air_date': ''}

        if collected is None:
            collected = old_entry['collected']

        if watched is None:
            watched = old_entry['watched']
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        try:
            cursor.execute(
                "REPLACE INTO episodes ("
                "show_id, season, trakt_id, kodi_meta, last_updated, watched, collected, number, air_date)"
                "VALUES "
                "(?, ?, ?, ?, ?, ?, ?, ?, ?)",
                (show_id, season, episode_id, str(kodi_meta), update_time, watched, collected, number,
                 kodi_meta['info']['premiered']))

            cursor.connection.commit()
            cursor.close()

            return {'show_id': show_id, 'season': season, 'episode_id': episode_id, 'kodi_meta': kodi_meta,
                    'update_time': update_time, 'watched': watched, 'collected': collected, 'number': number}
        except:
            cursor.close()

            import traceback
            traceback.print_exc()
            pass
        finally:
            tools.try_release_lock(tools.traktSyncDB_lock)
Exemplo n.º 4
0
    def _update_movie(self, trakt_object, get_meta=True):
        movie_id = trakt_object['ids']['trakt']
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        cursor.execute('SELECT * FROM movies WHERE trakt_id=?',
                       (int(movie_id), ))
        old_entry = cursor.fetchone()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)

        if get_meta and (old_entry is None or old_entry['kodi_meta'] == '{}'):
            try:
                kodi_meta = tmdb.TMDBAPI().movieToListItem(trakt_object)
                if kodi_meta is None or kodi_meta == '{}':
                    kodi_meta = imdb.IMDBScraper().movieToListItem(
                        trakt_object)
            except:
                return None
        else:
            if old_entry is None:
                kodi_meta = {}
            else:
                kodi_meta = old_entry['kodi_meta']

        if kodi_meta is None:
            return

        if old_entry is None:
            air_date = kodi_meta['info'].get('aired',
                                             kodi_meta['info']['premiered'])
            old_entry = {'collected': 0, 'watched': 0, 'air_date': air_date}

        collected = old_entry['collected']
        watched = old_entry['watched']
        air_date = old_entry['air_date']

        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        try:
            cursor.execute(
                "REPLACE INTO movies ("
                "trakt_id, kodi_meta, collected, watched, last_updated, air_date)"
                "VALUES "
                "(?, ?, ?, ?, ?, ?)", (movie_id, str(kodi_meta), collected,
                                       watched, update_time, air_date))
            cursor.connection.commit()
            cursor.close()

            return {
                'trakt_id': movie_id,
                'kodi_meta': kodi_meta,
                'update_time': update_time,
                'watched': watched,
                'collected': collected
            }
        except:
            cursor.close()
            import traceback
            traceback.print_exc()
            pass
        finally:
            tools.try_release_lock(tools.traktSyncDB_lock)