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 instanceA_profile_filter_id:
        logging.info(f'only filtering content with instanceA_profile_filter_id {instanceA_profile_filter_id}')
    for content in instanceA_contents:
        content_not_synced = (content[content_id_key] not in instanceB_contentIds)
        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 (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 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 (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 ((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 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
            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:
                logging.info(f'syncing content title "{title}"')
                sync_response = instanceB_session.post(instanceB_content_url, json=formatted_content)
                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:
                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 (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, json=matching_content_instanceB)
                        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')
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 instanceA_profile_filter_id:
        logging.info(f'only filtering content with instanceA_profile_filter_id {instanceA_profile_filter_id}')
    for content in instanceA_contents:
        content_not_synced = (content[content_id_key] not in instanceB_contentIds)
        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 (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 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 (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 ((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 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
            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:
                logging.info(f'syncing content title "{title}"')
                sync_response = instanceB_session.post(instanceB_content_url, json=formatted_content)
                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:
                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 (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, json=matching_content_instanceB)
                        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')