Esempio n. 1
0
def get_release(base_url, imdb_id=None, download_id=None, release_id=None):
    results = {}
    params = {}

    # determine cmd and params to send to CouchPotato to get our results
    section = 'movies'
    cmd = 'media.list'
    if release_id or imdb_id:
        section = 'media'
        cmd = 'media.get'
        params['id'] = release_id or imdb_id

    if not (release_id or imdb_id or download_id):
        logger.debug('No information available to filter CP results')
        return results

    url = '{0}{1}'.format(base_url, cmd)
    logger.debug('Opening URL: {0} with PARAMS: {1}'.format(url, params))

    try:
        r = requests.get(url, params=params, verify=False, timeout=(30, 60))
    except requests.ConnectionError:
        logger.error('Unable to open URL {0}'.format(url))
        return results

    try:
        result = r.json()
    except ValueError:
        # ValueError catches simplejson's JSONDecodeError and json's ValueError
        logger.error('CouchPotato returned the following non-json data')
        for line in r.iter_lines():
            logger.error('{0}'.format(line))
        return results

    if not result['success']:
        if 'error' in result:
            logger.error('{0}'.format(result['error']))
        else:
            logger.error('no media found for id {0}'.format(params['id']))
        return results

    # Gather release info and return it back, no need to narrow results
    if release_id:
        try:
            cur_id = result[section]['_id']
            results[cur_id] = result[section]
            return results
        except Exception:
            pass

    # Gather release info and proceed with trying to narrow results to one release choice

    movies = result[section]
    if not isinstance(movies, list):
        movies = [movies]
    for movie in movies:
        if movie['status'] not in ['active', 'done']:
            continue
        releases = movie['releases']
        if not releases:
            continue
        for release in releases:
            try:
                if release['status'] not in ['snatched', 'downloaded', 'done']:
                    continue
                if download_id:
                    if download_id.lower() != release['download_info']['id'].lower():
                        continue

                cur_id = release['_id']
                results[cur_id] = release
                results[cur_id]['title'] = movie['title']
            except Exception:
                continue

    # Narrow results by removing old releases by comparing their last_edit field
    if len(results) > 1:
        for id1, x1 in results.items():
            for id2, x2 in results.items():
                try:
                    if x2['last_edit'] > x1['last_edit']:
                        results.pop(id1)
                except Exception:
                    continue

    # Search downloads on clients for a match to try and narrow our results down to 1
    if len(results) > 1:
        for cur_id, x in results.items():
            try:
                if not find_download(str(x['download_info']['downloader']).lower(), x['download_info']['id']):
                    results.pop(cur_id)
            except Exception:
                continue

    return results
Esempio n. 2
0
def get_release(base_url, imdb_id=None, download_id=None, release_id=None):
    results = {}
    params = {}

    # determine cmd and params to send to CouchPotato to get our results
    section = 'movies'
    cmd = 'media.list'
    if release_id or imdb_id:
        section = 'media'
        cmd = 'media.get'
        params['id'] = release_id or imdb_id

    if not (release_id or imdb_id or download_id):
        logger.debug('No information available to filter CP results')
        return results

    url = '{0}{1}'.format(base_url, cmd)
    logger.debug('Opening URL: {0} with PARAMS: {1}'.format(url, params))

    try:
        r = requests.get(url, params=params, verify=False, timeout=(30, 60))
    except requests.ConnectionError:
        logger.error('Unable to open URL {0}'.format(url))
        return results

    try:
        result = r.json()
    except ValueError:
        # ValueError catches simplejson's JSONDecodeError and json's ValueError
        logger.error('CouchPotato returned the following non-json data')
        for line in r.iter_lines():
            logger.error('{0}'.format(line))
        return results

    if not result['success']:
        if 'error' in result:
            logger.error('{0}'.format(result['error']))
        else:
            logger.error('no media found for id {0}'.format(params['id']))
        return results

    # Gather release info and return it back, no need to narrow results
    if release_id:
        try:
            cur_id = result[section]['_id']
            results[cur_id] = result[section]
            return results
        except Exception:
            pass

    # Gather release info and proceed with trying to narrow results to one release choice

    movies = result[section]
    if not isinstance(movies, list):
        movies = [movies]
    for movie in movies:
        if movie['status'] not in ['active', 'done']:
            continue
        releases = movie['releases']
        if not releases:
            continue
        for release in releases:
            try:
                if release['status'] not in ['snatched', 'downloaded', 'done']:
                    continue
                if download_id:
                    if download_id.lower(
                    ) != release['download_info']['id'].lower():
                        continue

                cur_id = release['_id']
                results[cur_id] = release
                results[cur_id]['title'] = movie['title']
            except Exception:
                continue

    # Narrow results by removing old releases by comparing their last_edit field
    if len(results) > 1:
        for id1, x1 in results.items():
            for x2 in results.values():
                try:
                    if x2['last_edit'] > x1['last_edit']:
                        results.pop(id1)
                except Exception:
                    continue

    # Search downloads on clients for a match to try and narrow our results down to 1
    if len(results) > 1:
        for cur_id, x in results.items():
            try:
                if not find_download(
                        str(x['download_info']['downloader']).lower(),
                        x['download_info']['id']):
                    results.pop(cur_id)
            except Exception:
                continue

    return results