예제 #1
0
def xem_refresh(series_obj, force=False):
    """
    Refresh data from xem for a tv show

    :param indexer_id: int
    """
    if not series_obj or series_obj.series_id < 1:
        return

    indexer_id = series_obj.indexer
    series_id = series_obj.series_id

    MAX_REFRESH_AGE_SECS = 86400  # 1 day

    main_db_con = db.DBConnection()
    rows = main_db_con.select(
        "SELECT last_refreshed FROM xem_refresh WHERE indexer = ? and indexer_id = ?",
        [indexer_id, series_id])
    if rows:
        lastRefresh = int(rows[0]['last_refreshed'])
        refresh = int(time.mktime(datetime.datetime.today().timetuple())
                      ) > lastRefresh + MAX_REFRESH_AGE_SECS
    else:
        refresh = True

    if refresh or force:
        logger.log(
            u'Looking up XEM scene mapping for show ID {0} on {1}'.format(
                series_id, series_obj.indexer_name), logger.DEBUG)

        # mark refreshed
        main_db_con.upsert(
            "xem_refresh", {
                'last_refreshed':
                int(time.mktime(datetime.datetime.today().timetuple()))
            }, {
                'indexer': indexer_id,
                'indexer_id': series_id
            })

        try:
            if not indexerApi(indexer_id).config.get('xem_origin'):
                logger.log(
                    u'{0} is an unsupported indexer in XEM'.format(
                        indexerApi(indexer_id).name), logger.DEBUG)
                return
            # XEM MAP URL
            url = "http://thexem.de/map/havemap?origin={0}".format(
                indexerApi(indexer_id).config['xem_origin'])
            parsed_json = safe_session.get_json(url)
            if not parsed_json or 'result' not in parsed_json or 'success' not in parsed_json[
                    'result'] or 'data' not in parsed_json or str(
                        indexer_id) not in parsed_json['data']:
                logger.log(
                    u'No XEM data for show ID {0} on {1}'.format(
                        series_id, series_obj.indexer_name), logger.DEBUG)
                return

            # XEM API URL
            url = "http://thexem.de/map/all?id={0}&origin={1}&destination=scene".format(
                indexer_id,
                indexerApi(indexer_id).config['xem_origin'])
            parsed_json = safe_session.get_json(url)
            if not parsed_json or 'result' not in parsed_json or 'success' not in parsed_json[
                    'result']:
                logger.log(
                    u'No XEM data for show ID {0} on {1}'.format(
                        indexer_id, series_obj.indexer_name), logger.DEBUG)
                return

            cl = []
            for entry in parsed_json['data']:
                if 'scene' in entry:
                    cl.append([
                        "UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? WHERE indexer = ? AND showid = ? AND season = ? AND episode = ?",
                        [
                            entry['scene']['season'],
                            entry['scene']['episode'],
                            entry['scene']['absolute'], indexer_id, series_id,
                            entry[indexerApi(indexer_id).config['xem_origin']]
                            ['season'], entry[indexerApi(
                                indexer_id).config['xem_origin']]['episode']
                        ]
                    ])
                    cl.append([
                        "UPDATE tv_episodes SET absolute_number = ? WHERE indexer = ? AND showid = ? AND season = ? AND episode = ? AND absolute_number = 0",
                        [
                            entry[indexerApi(
                                indexer_id).config['xem_origin']]['absolute'],
                            indexer_id, series_id, entry[indexerApi(
                                indexer_id).config['xem_origin']]['season'],
                            entry[indexerApi(
                                indexer_id).config['xem_origin']]['episode']
                        ]
                    ])
                if 'scene_2' in entry:  # for doubles
                    cl.append([
                        "UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? WHERE indexer = ? AND showid = ? AND season = ? AND episode = ?",
                        [
                            entry['scene_2']['season'],
                            entry['scene_2']['episode'],
                            entry['scene_2']['absolute'], indexer_id,
                            series_id, entry[indexerApi(
                                indexer_id).config['xem_origin']]['season'],
                            entry[indexerApi(
                                indexer_id).config['xem_origin']]['episode']
                        ]
                    ])

            if cl:
                main_db_con = db.DBConnection()
                main_db_con.mass_action(cl)

        except Exception as e:
            logger.log(
                u"Exception while refreshing XEM data for show ID {0} on {1}: {2}"
                .format(series_id, series_obj.indexer_name,
                        ex(e)), logger.WARNING)
            logger.log(traceback.format_exc(), logger.DEBUG)
예제 #2
0
def xem_refresh(series_obj, force=False):
    """
    Refresh data from xem for a tv show

    :param indexer_id: int
    """
    if not series_obj or series_obj.series_id < 1:
        return

    indexer_id = series_obj.indexer
    series_id = series_obj.series_id

    MAX_REFRESH_AGE_SECS = 86400  # 1 day

    main_db_con = db.DBConnection()
    rows = main_db_con.select('SELECT last_refreshed FROM xem_refresh WHERE indexer = ? and indexer_id = ?',
                              [indexer_id, series_id])
    if rows:
        lastRefresh = int(rows[0]['last_refreshed'])
        refresh = int(time.mktime(datetime.datetime.today().timetuple())) > lastRefresh + MAX_REFRESH_AGE_SECS
    else:
        refresh = True

    if refresh or force:
        logger.log(
            u'Looking up XEM scene mapping for show ID {0} on {1}'.format(series_id, series_obj.indexer_name), logger.DEBUG)

        # mark refreshed
        main_db_con.upsert(
            'xem_refresh',
            {'last_refreshed': int(time.mktime(datetime.datetime.today().timetuple()))},
            {'indexer': indexer_id, 'indexer_id': series_id}
        )

        try:
            if not indexerApi(indexer_id).config.get('xem_origin'):
                logger.log(u'{0} is an unsupported indexer in XEM'.format(indexerApi(indexer_id).name), logger.DEBUG)
                return
            # XEM MAP URL
            url = 'http://thexem.de/map/havemap?origin={0}'.format(indexerApi(indexer_id).config['xem_origin'])
            parsed_json = safe_session.get_json(url)
            if not parsed_json or 'result' not in parsed_json or 'success' not in parsed_json['result'] or 'data' not in parsed_json or str(series_id) not in parsed_json['data']:
                logger.log(u'No XEM data for show ID {0} on {1}'.format(series_id, series_obj.indexer_name), logger.DEBUG)
                return

            # XEM API URL
            url = 'http://thexem.de/map/all?id={0}&origin={1}&destination=scene'.format(series_id, indexerApi(indexer_id).config['xem_origin'])
            parsed_json = safe_session.get_json(url)
            if not parsed_json or 'result' not in parsed_json or 'success' not in parsed_json['result']:
                logger.log(u'No XEM data for show ID {0} on {1}'.format(indexer_id, series_obj.indexer_name), logger.DEBUG)
                return

            cl = []
            for entry in parsed_json['data']:
                if 'scene' in entry:
                    cl.append([
                        'UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? '
                        'WHERE indexer = ? AND showid = ? AND season = ? AND episode = ?',
                        [entry['scene']['season'], entry['scene']['episode'],
                         entry['scene']['absolute'], indexer_id, series_id,
                         entry[indexerApi(indexer_id).config['xem_origin']]['season'],
                         entry[indexerApi(indexer_id).config['xem_origin']]['episode']]
                    ])
                    # Update the absolute_number from xem, but do not set it when it has already been set by tvdb.
                    # We want to prevent doubles and tvdb is leading in that case.
                    cl.append([
                        'UPDATE tv_episodes SET absolute_number = ? '
                        'WHERE indexer = ? AND showid = ? AND season = ? AND episode = ? AND absolute_number = 0 '
                        'AND {absolute_number} NOT IN '
                        '(SELECT absolute_number '
                        'FROM tv_episodes '
                        'WHERE absolute_number = ? AND indexer = ? AND showid = ?)'.format(
                            absolute_number=entry[indexerApi(indexer_id).config['xem_origin']]['absolute']
                        ),
                        [entry[indexerApi(indexer_id).config['xem_origin']]['absolute'], indexer_id, series_id,
                         entry[indexerApi(indexer_id).config['xem_origin']]['season'],
                         entry[indexerApi(indexer_id).config['xem_origin']]['episode'],
                         entry[indexerApi(indexer_id).config['xem_origin']]['absolute'],
                         indexer_id, series_id]
                    ])
                if 'scene_2' in entry:  # for doubles
                    cl.append([
                        'UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? '
                        'WHERE indexer = ? AND showid = ? AND season = ? AND episode = ?',
                        [entry['scene_2']['season'], entry['scene_2']['episode'],
                         entry['scene_2']['absolute'], indexer_id, series_id,
                         entry[indexerApi(indexer_id).config['xem_origin']]['season'],
                         entry[indexerApi(indexer_id).config['xem_origin']]['episode']]
                    ])

            if cl:
                main_db_con = db.DBConnection()
                main_db_con.mass_action(cl)

        except Exception as e:
            logger.log(u'Exception while refreshing XEM data for show ID {0} on {1}: {2}'.format
                       (series_id, series_obj.indexer_name, ex(e)), logger.WARNING)
            logger.log(traceback.format_exc(), logger.DEBUG)