Exemple #1
0
def mark_expired(session):
    """Marks series and episodes that have expired since we cached them"""
    # Only get the expired list every hour
    last_check = persist.get('last_check')

    if not last_check:
        persist['last_check'] = datetime.utcnow()
        return
    if datetime.utcnow() - last_check <= timedelta(hours=2):
        # It has been less than 2 hour, don't check again
        return

    new_last_check = datetime.utcnow()

    try:
        # Calculate seconds since epoch minus a minute for buffer
        last_check_epoch = int((last_check - datetime(1970, 1, 1)).total_seconds()) - 60
        log.debug("Getting updates from thetvdb (%s)", last_check_epoch)
        updates = TVDBRequest().get('updated/query', fromTime=last_check_epoch)
    except requests.RequestException as e:
        log.error('Could not get update information from tvdb: %s', e)
        return

    expired_series = [series['id'] for series in updates] if updates else []

    # Update our cache to mark the items that have expired
    for chunk in chunked(expired_series):
        series_updated = session.query(TVDBSeries).filter(TVDBSeries.id.in_(chunk)).update({'expired': True}, 'fetch')
        episodes_updated = session.query(TVDBEpisode).filter(TVDBEpisode.series_id.in_(chunk)).update({'expired': True},
                                                                                                      'fetch')
        log.debug('%s series and %s episodes marked as expired', series_updated, episodes_updated)

    persist['last_check'] = new_last_check
Exemple #2
0
def mark_expired(session):
    """Marks series and episodes that have expired since we cached them"""
    # Only get the expired list every hour
    last_check = persist.get('last_check')

    if not last_check:
        persist['last_check'] = datetime.utcnow()
        return
    if datetime.utcnow() - last_check <= timedelta(hours=2):
        # It has been less than 2 hour, don't check again
        return

    new_last_check = datetime.utcnow()

    try:
        # Calculate seconds since epoch minus a minute for buffer
        last_check_epoch = int((last_check - datetime(1970, 1, 1)).total_seconds()) - 60
        logger.debug('Getting updates from thetvdb ({})', last_check_epoch)
        updates = TVDBRequest().get('updated/query', fromTime=last_check_epoch)
    except requests.RequestException as e:
        logger.error('Could not get update information from tvdb: {}', e)
        return

    expired_series = [series['id'] for series in updates] if updates else []

    # Update our cache to mark the items that have expired
    for chunk in chunked(expired_series):
        series_updated = (
            session.query(TVDBSeries)
            .filter(TVDBSeries.id.in_(chunk))
            .update({'expired': True}, 'fetch')
        )
        episodes_updated = (
            session.query(TVDBEpisode)
            .filter(TVDBEpisode.series_id.in_(chunk))
            .update({'expired': True}, 'fetch')
        )
        logger.debug(
            '{} series and {} episodes marked as expired', series_updated, episodes_updated
        )

    persist['last_check'] = new_last_check