Ejemplo n.º 1
0
def get_instance_contents(instance_url,
                          instance_key,
                          instance_session,
                          instance_name=''):
    instance_contentIds = []
    instance_content_url = get_content_path(instance_url, instance_key)
    instance_contents = instance_session.get(instance_content_url)

    if instance_contents.status_code != 200:
        logger.error('instance{} server error - response {}'.format(
            instance_name, instance_contents.status_code))
        exit_system()
    else:
        try:
            instance_contents = instance_contents.json()
        except:
            logger.error(
                f'Could not decode contents from {instance_content_url}')
            exit_system()

    for content_to_sync in instance_contents:
        instance_contentIds.append(content_to_sync[content_id_key])

    logger.debug('{} contents in instance {}'.format(len(instance_contentIds),
                                                     instance_name))
    return instance_contents, instance_contentIds
Ejemplo n.º 2
0
def sync_servers(instanceA_contents, instanceB_language_id,
                 instanceB_contentIds, instanceB_path, instanceB_profile_id,
                 instanceB_session, instanceB_url, profile_filter_id,
                 instanceB_key):
    search_ids = []

    # if given instance A profile id then we want to filter out content without that id
    if profile_filter_id:
        logging.info(
            f'only filtering content with profile_filter_id {profile_filter_id}'
        )

    # for each content id in instance A, check if it needs to be synced to instance B
    for content in instanceA_contents:
        if content[content_id_key] not in instanceB_contentIds:
            title = content.get('title') or content.get('artistName')

            # if given this, we want to filter from instance by profile id
            if profile_filter_id:
                content_profile_id = content.get('qualityProfileId')
                if profile_filter_id != content_profile_id:
                    logging.debug(
                        f'Skipping content {title} - mismatched profile_id {content_profile_id} with filter_id {profile_filter_id}'
                    )
                    continue

            logging.info(f'syncing content title "{title}"')

            # get the POST payload and sync content to instance B
            payload = get_new_content_payload(
                content=content,
                instance_path=instanceB_path,
                instance_profile_id=instanceB_profile_id,
                instance_url=instanceB_url,
                instance_language_id=instanceB_language_id,
            )
            instanceB_content_url = get_content_path(instanceB_url,
                                                     instanceB_key)
            sync_response = instanceB_session.post(instanceB_content_url,
                                                   data=json.dumps(payload))

            # check response and save content id for searching later on if success
            if sync_response.status_code != 201 and sync_response.status_code != 200:
                logger.error(
                    f'server sync error for {title} - response: {sync_response.text}'
                )
            else:
                try:
                    search_ids.append(int(sync_response.json()['id']))
                except:
                    logger.error(
                        f'Could not decode sync response from {instanceB_content_url}'
                    )
                logging.info(
                    'content title "{0}" synced successfully'.format(title))

    logging.info(f'{len(search_ids)} contents synced successfully')
Ejemplo n.º 3
0
 def __init__(self, theday):
     self.auto_keyword = []
     file_name = str(theday) + '_autoword'
     self.file_list = self.get_file_list(theday)
     for file in self.file_list:
         file_path = get_content_path() + file
         contents = json.loads(self.load_content(file_path),
                               encoding='utf-8')
         self.auto_keyword.extend(self.get_auto_keyword(contents))
     self.save_parser(self.auto_keyword, file_name)
Ejemplo n.º 4
0
def sync_servers(instanceA_contents, instanceB_language_id, instanceB_contentIds,
                 instanceB_path, instanceB_profile_id, instanceA_profile_filter_id,
                 instanceB_session, instanceB_url, instanceB_key, instanceA_quality_match,
                 instanceA_tag_filter_id, instanceA_blacklist):
    global is_radarr, is_sonarr, is_test_run
    search_ids = []

    # if given instance A profile id then we want to filter out content without that id
    if instanceA_profile_filter_id:
        logging.info(f'only filtering content with instanceA_profile_filter_id {instanceA_profile_filter_id}')

    # for each content id in instance A, check if it needs to be synced to instance B
    for content in instanceA_contents:
        if content[content_id_key] not in instanceB_contentIds:
            title = content.get('title') or content.get('artistName')
            instance_path = instanceB_path or dirname(content.get('path'))

            # if skipping missing files, we want to skip any that don't have files
            if is_radarr and skip_missing:
                content_has_file = content.get('hasFile')
                if not content_has_file:
                    logging.debug(f'Skipping content {title} - file missing')
                    continue

            # if given this, we want to filter from instance by profile id
            if instanceA_profile_filter_id:
                quality_profile_id = content.get('qualityProfileId')
                if instanceA_profile_filter_id != quality_profile_id:
                    logging.debug(f'Skipping content {title} - mismatched quality_profile_id {quality_profile_id} with instanceA_profile_filter_id {instanceA_profile_filter_id}')
                    continue

            # if given quality filter we want to filter if quality from instanceA isnt high enough yet
            if is_radarr and instanceA_quality_match:
                content_quality = content.get('movieFile', {}).get('quality', {}).get('quality', {}).get('name', '')
                if content_quality and not re.match(instanceA_quality_match, content_quality):
                    logging.debug(f'Skipping content {title} - mismatched content_quality {content_quality} with instanceA_quality_match {instanceA_quality_match}')
                    continue

            # if given tag filter then filter by tag - (Sonarr/Radarr v3 only)
            if (is_sonarr or is_radarr) and instanceA_tag_filter_id:
                content_tag_ids = content.get('tags')
                if not (set(content_tag_ids) & set(instanceA_tag_filter_id)):
                    logging.debug(f'Skipping content {title} - mismatched content_tag_ids {content_tag_ids} with instanceA_tag_filter_id {instanceA_tag_filter_id}')
                    continue

            # if black list given then dont sync matching slugs/ids
            if instanceA_blacklist:
                title_slug = content.get('titleSlug') or content.get('foreignArtistId')
                if title_slug in instanceA_blacklist:
                    logging.debug(f'Skipping content {title} - blacklist slug: {title_slug}')
                    continue

                content_id = str(content.get('id'))
                if content_id in instanceA_blacklist:
                    logging.debug(f'Skipping content {title} - blacklist ID: {content_id}')
                    continue

            logging.info(f'syncing content title "{title}"')

            # get the POST payload and sync content to instance B
            payload = get_new_content_payload(
                content=content,
                instance_path=instance_path,
                instance_profile_id=instanceB_profile_id,
                instance_url=instanceB_url,
                instance_language_id=instanceB_language_id,
            )
            instanceB_content_url = get_content_path(instanceB_url, instanceB_key)

            if is_test_run:
                logging.info('content title "{0}" synced successfully (test only)'.format(title))
            else:
                sync_response = instanceB_session.post(instanceB_content_url, data=json.dumps(payload))

                # check response and save content id for searching later on if success
                if sync_response.status_code != 201 and sync_response.status_code != 200:
                    logger.error(f'server sync error for {title} - response: {sync_response.text}')
                else:
                    try:
                        search_ids.append(int(sync_response.json()['id']))
                    except:
                        logger.error(f'Could not decode sync response from {instanceB_content_url}')
                    logging.info('content title "{0}" synced successfully'.format(title))

    logging.info(f'{len(search_ids)} contents synced successfully')
Ejemplo n.º 5
0
def sync_servers(instanceA_contents, instanceB_language_id,
                 instanceB_contentIds, instanceB_path, instanceB_profile_id,
                 instanceA_profile_filter_id, instanceB_session, instanceB_url,
                 instanceB_key, instanceA_quality_match,
                 instanceA_tag_filter_id, instanceA_blacklist,
                 instanceB_contents):
    global is_radarr, is_sonarr, is_test_run, sync_monitor
    search_ids = []

    # if given instance A profile id then we want to filter out content without that id
    if instanceA_profile_filter_id:
        logging.info(
            f'only filtering content with instanceA_profile_filter_id {instanceA_profile_filter_id}'
        )

    # for each content id in instance A, check if it needs to be synced to instance B
    for content in instanceA_contents:
        content_not_synced = content[
            content_id_key] not in instanceB_contentIds
        # only skip alrerady synced items if we arent syncing monitoring as well
        if content_not_synced or sync_monitor:
            title = content.get('title') or content.get('artistName')
            instance_path = instanceB_path or dirname(content.get('path'))

            # if skipping missing files, we want to skip any that don't have files
            if is_radarr and skip_missing:
                content_has_file = content.get('hasFile')
                if not content_has_file:
                    logging.debug(f'Skipping content {title} - file missing')
                    continue

            # if given this, we want to filter from instance by profile id
            if instanceA_profile_filter_id:
                quality_profile_id = content.get('qualityProfileId')
                if instanceA_profile_filter_id != quality_profile_id:
                    logging.debug(
                        f'Skipping content {title} - mismatched quality_profile_id {quality_profile_id} with instanceA_profile_filter_id {instanceA_profile_filter_id}'
                    )
                    continue

            # if given quality filter we want to filter if quality from instanceA isnt high enough yet
            if is_radarr and instanceA_quality_match:
                content_quality = content.get('movieFile', {}).get(
                    'quality', {}).get('quality', {}).get('name', '')
                if content_quality and not re.match(instanceA_quality_match,
                                                    content_quality):
                    logging.debug(
                        f'Skipping content {title} - mismatched content_quality {content_quality} with instanceA_quality_match {instanceA_quality_match}'
                    )
                    continue

            # if given tag filter then filter by tag - (Sonarr/Radarr v3 only)
            if (is_sonarr or is_radarr) and instanceA_tag_filter_id:
                content_tag_ids = content.get('tags')
                if not (set(content_tag_ids) & set(instanceA_tag_filter_id)):
                    logging.debug(
                        f'Skipping content {title} - mismatched content_tag_ids {content_tag_ids} with instanceA_tag_filter_id {instanceA_tag_filter_id}'
                    )
                    continue

            # if black list given then dont sync matching slugs/ids
            if instanceA_blacklist:
                title_slug = content.get('titleSlug') or content.get(
                    'foreignArtistId')
                if title_slug in instanceA_blacklist:
                    logging.debug(
                        f'Skipping content {title} - blacklist slug: {title_slug}'
                    )
                    continue

                content_id = str(content.get('id'))
                if content_id in instanceA_blacklist:
                    logging.debug(
                        f'Skipping content {title} - blacklist ID: {content_id}'
                    )
                    continue

            # generate content from instance A to sync into instance B
            formatted_content = get_content_details(
                content=dict(content),
                instance_path=instance_path,
                instance_profile_id=instanceB_profile_id,
                instance_url=instanceB_url,
                instance_language_id=instanceB_language_id,
            )
            instanceB_content_url = get_content_path(instanceB_url,
                                                     instanceB_key)

            if is_test_run:
                logging.info(
                    'content title "{0}" synced successfully (test only)'.
                    format(title))
            elif content_not_synced:
                # sync content if not synced
                logging.info(f'syncing content title "{title}"')
                sync_response = instanceB_session.post(
                    instanceB_content_url, data=json.dumps(formatted_content))
                # check response and save content id for searching later on if success
                if sync_response.status_code != 201 and sync_response.status_code != 200:
                    logger.error(
                        f'server sync error for {title} - response: {sync_response.text}'
                    )
                else:
                    try:
                        search_ids.append(int(sync_response.json()['id']))
                    except:
                        logger.error(
                            f'Could not decode sync response from {instanceB_content_url}'
                        )
                    logging.info(
                        'content title "{0}" synced successfully'.format(
                            title))

            elif sync_monitor:
                # else if is already synced and we want to sync monitoring then sync that now

                # find matching content from instance B to check monitored status
                matching_content_instanceB = list(
                    filter(
                        lambda content_instanceB: content_instanceB[
                            'titleSlug'] == content.get('titleSlug'),
                        instanceB_contents))
                if (len(matching_content_instanceB) == 1):
                    matching_content_instanceB = matching_content_instanceB[0]
                    # if we found a content match from instance B, then check monitored status - if different then sync from A to B
                    if matching_content_instanceB['monitored'] != content[
                            'monitored']:
                        matching_content_instanceB['monitored'] = content[
                            'monitored']
                        instanceB_content_url = get_content_put_path(
                            instanceB_url, instanceB_key,
                            matching_content_instanceB.get('id'))
                        sync_response = instanceB_session.put(
                            instanceB_content_url,
                            data=json.dumps(matching_content_instanceB))
                        # check response and save content id for searching later on if success
                        if sync_response.status_code != 202:
                            logger.error(
                                f'server monitoring sync error for {title} - response: {sync_response.text}'
                            )
                        else:
                            try:
                                search_ids.append(
                                    int(sync_response.json()['id']))
                            except:
                                logger.error(
                                    f'Could not decode sync response from {instanceB_content_url}'
                                )
                            logging.info(
                                'content title "{0}" monitoring synced successfully'
                                .format(title))

    logging.info(f'{len(search_ids)} contents synced successfully')
Ejemplo n.º 6
0
 def get_file_list(self, theday):
     file_list = []
     for file in os.listdir(get_content_path()):
         if str(theday) in str(file):
             file_list.append(file)
     return file_list