예제 #1
0
파일: models.py 프로젝트: antoviaque/plebia
    def apply_tvdb_updates(self):
        '''Retreives last updates from TVDB and update existing series/seasons/episodes 
        accordingly'''

        from thetvdbapi import TheTVDB
        tvdb = TheTVDB(settings.TVDB_API_KEY)

        try:
            update = TVDBCache.objects.get(type='last')
        except TVDBCache.DoesNotExist:
            server_time = tvdb.get_server_time()
            update = TVDBCache(type='last', time=server_time)
            update.save()
            return False

        log.info("Updating series from TVDB (last update: %s)", update.time)

        (timestamp, series_list, episode_list) = tvdb.get_updates_by_timestamp(update.time)

        # Update series (details, seasons & episodes)
        for series_tvdb_id in series_list:
            try:
                series = Series.objects.get(tvdb_id=series_tvdb_id)
                # Check if this has been downloaded in the past
                if series.is_active():
                    log.info('Applying update to series "%s"', series.name)
                    series.update_from_tvdb()
            except Series.DoesNotExist:
                pass

        # Update individual episodes modifications (not new episodes)
        for episode_tvdb_id in episode_list:
            try:
                episode = Episode.objects.get(tvdb_id=episode_tvdb_id)
                episode_tvdb = tvdb.get_episode(episode_tvdb_id)
                log.info('Applying update to episode "%s s%de%d"', episode.season.series.name, episode.season.number, episode.number)
                episode.update_details(episode_tvdb)
            except Episode.DoesNotExist:
                pass

        # Update timestamp
        update.time = timestamp
        update.save()