automated commit 20/01/2020 18:36:31

pull/21/head
Leonardo Merza 4 years ago
parent a0869e961c
commit c9d80383d5

@ -9,6 +9,7 @@ import sys
import time
from syncarr.config import *
from syncarr.paths import *
def get_new_content_payload(content, instance_path, instance_profile_id, instanceB_url):
@ -55,27 +56,14 @@ def get_new_content_payload(content, instance_path, instance_profile_id, instanc
return payload
def get_content_path(instance_url, key):
url = '{0}/{1}?apikey={2}'.format(instance_url, api_content_path, key)
logger.debug('get_content_path: {}'.format(url))
return url
def get_search_path(instance_url, key):
url = '{0}/{1}?apikey={2}'.format(instance_url, api_search_path, key)
logger.debug('get_search_path: {}'.format(url))
return url
def get_profile_path(instance_url, key):
url = '{0}/{1}?apikey={2}'.format(instance_url, api_profile_path, key)
logger.debug('get_profile_path: {}'.format(url))
return url
def get_profile_from_id(instance_session, instance_url, instance_key, instance_profile, instance_name=''):
instance_profile_url = get_profile_path(instance_url, instance_key)
instance_profiles = instance_session.get(instance_profile_url)
instance_profiles = instance_profiles.json()
try:
instance_profiles = instance_profiles.json()
except:
logger.error(f'Could not decode profile id from {instance_profile_url}')
sys.exit(0)
profile = next((item for item in instance_profiles if item["name"].lower() == instance_profile.lower()), False)
if not profile:
@ -96,16 +84,9 @@ def search_synced(search_ids, instance_search_url, instance_session):
payload = { 'name': 'contentsSearch', 'contentIds': search_ids }
instance_session.post(instance_search_url, data=json.dumps(payload))
def sync_servers(
instanceA_contents,
instanceB_contentIds,
instanceB_path,
instanceB_profile_id,
instanceB_session,
instanceB_url,
profile_filter_id,
instanceB_key,
):
def sync_servers(instanceA_contents, instanceB_contentIds, instanceB_path, instanceB_profile_id,
instanceB_session, instanceB_url, profile_filter_id, instanceB_key):
search_ids = []
@ -133,7 +114,10 @@ def sync_servers(
if sync_response.status_code != 201 and sync_response.status_code != 200:
logger.error('server sync error for {} - response {}'.format(title, sync_response.status_code))
else:
search_ids.append(int(sync_response.json()['id']))
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('{0} contents synced successfully'.format(len(search_ids)))
@ -151,7 +135,11 @@ def get_instance_contents(instance_url, instance_key, instance_session, instance
logger.error('instance{} server error - response {}'.format(instance_name, instance_contents.status_code))
sys.exit(0)
else:
instance_contents = instance_contents.json()
try:
instance_contents = instance_contents.json()
except:
logger.error(f'Could not decode contents from {instance_content_url}')
sys.exit(0)
for content_to_sync in instance_contents:
instance_contentIds.append(content_to_sync[content_id_key])
@ -160,15 +148,62 @@ def get_instance_contents(instance_url, instance_key, instance_session, instance
return instance_contents, instance_contentIds
def check_status(instance_session, instance_url, instance_key, instance_name='', checkV3=False):
global api_version
logger.debug(f'check api_version "{api_version}"')
instance_status_url = get_status_path(instance_url, instance_key, checkV3)
error_message = f'Could not connect to instance{instance_name}: {instance_status_url}'
status_response = None
try:
status_response = instance_session.get(instance_status_url)
# only test again if not lidarr and we haven't tested v3 already
if status_response.status_code != 200 and not checkV3 and not is_lidarr:
logger.debug(f'check api_version again')
status_response = check_status(instance_session, instance_url, instance_key, instance_name, checkV3=True)
elif status_response.status_code != 200:
logger.error(error_message)
sys.exit(0)
except:
if not checkV3 and not is_lidarr:
logger.debug(f'check api_version again exception')
status_response = check_status(instance_session, instance_url, instance_key, instance_name, checkV3=True)
if status_response is None:
logger.error(error_message)
sys.exit(0)
else:
try:
status_response = status_response.json()
except:
logger.error(f"Could not retrieve status for {instance_status_url}")
sys.exit(0)
if(status_response.get('error')):
logger.error(f"{instance_status_url} error {status_response.get('error')}")
sys.exit(0)
logger.debug(f"{instance_status_url} version {status_response.get('version')}")
return status_response
def sync_content():
global instanceA_profile_id, instanceA_profile, instanceB_profile_id, instanceB_profile, instanceA_profile_filter, instanceA_profile_filter_id, instanceB_profile_filter, instanceB_profile_filter_id
global instanceA_profile_id, instanceA_profile, instanceB_profile_id, instanceB_profile, instanceA_profile_filter, instanceA_profile_filter_id, instanceB_profile_filter, instanceB_profile_filter_id, tested_api_version
# get sessions
instanceA_session = requests.Session()
instanceA_session.trust_env = False
instanceB_session = requests.Session()
instanceB_session.trust_env = False
if not tested_api_version:
check_status(instanceA_session, instanceA_url, instanceA_key, instance_name='A')
check_status(instanceB_session, instanceB_url, instanceB_key, instance_name='B')
tested_api_version = True
# if given a profile instead of a profile id then try to find the profile id
if not instanceA_profile_id and instanceA_profile:
instanceA_profile_id = get_profile_from_id(instanceA_session, instanceA_url, instanceA_key, instanceA_profile, 'A')

@ -1,54 +1,10 @@
#!/usr/bin/env python
import os
import logging
import requests
import json
import configparser
import sys
import time
DEV = os.environ.get('DEV', True)
VER = '1.3.0'
def ConfigSectionMap(section):
'''get all config options from config file'''
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
def get_config_value(env_key, config_key, config_section):
if is_in_docker:
return os.environ.get(env_key)
try:
_config = ConfigSectionMap(config_section)
return _config.get(config_key)
except configparser.NoSectionError:
return ''
########################################################################################################################
# load config file
BASE_CONFIG = 'config.conf'
if DEV:
settingsFilename = os.path.join(os.getcwd(), 'dev-{}'.format(BASE_CONFIG))
else:
settingsFilename = os.path.join(os.getcwd(), BASE_CONFIG)
from syncarr.get_config import *
config = configparser.ConfigParser()
config.read(settingsFilename)
is_in_docker = os.environ.get('IS_IN_DOCKER')
instance_sync_interval_seconds = os.environ.get('SYNC_INTERVAL_SECONDS')
if instance_sync_interval_seconds:
instance_sync_interval_seconds = int(instance_sync_interval_seconds)
########################################################################################################################
# setup logger
@ -79,69 +35,6 @@ logger.info('log level {}'.format(log_level))
if DEV:
logger.info('-----------------DEVV-----------------')
########################################################################################################################
########################################################################################################################
# get config settings from ENV or config files for Radarr
radarrA_url = get_config_value('RADARR_A_URL', 'url', 'radarrA')
radarrA_key = get_config_value('RADARR_A_KEY', 'key', 'radarrA')
radarrA_profile = get_config_value('RADARR_A_PROFILE', 'profile', 'radarrA')
radarrA_profile_id = get_config_value('RADARR_A_PROFILE_ID', 'profile_id', 'radarrA')
radarrA_profile_filter = get_config_value('RADARR_A_PROFILE_FILTER', 'profile_filter', 'radarrA')
radarrA_profile_filter_id = get_config_value('RADARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'radarrA')
radarrA_path = get_config_value('RADARR_A_PATH', 'path', 'radarrA')
radarrA_is_version_3 = get_config_value('RADARR_A_VERSION_3', 'version3', 'radarrA')
radarrB_url = get_config_value('RADARR_B_URL', 'url', 'radarrB')
radarrB_key = get_config_value('RADARR_B_KEY', 'key', 'radarrB')
radarrB_profile = get_config_value('RADARR_B_PROFILE', 'profile', 'radarrB')
radarrB_profile_id = get_config_value('RADARR_B_PROFILE_ID', 'profile_id', 'radarrB')
radarrB_profile_filter = get_config_value('RADARR_B_PROFILE_FILTER', 'profile_filter', 'radarrB')
radarrB_profile_filter_id = get_config_value('RADARR_B_PROFILE_FILTER_ID', 'profile_filter_id', 'radarrB')
radarrB_path = get_config_value('RADARR_B_PATH', 'path', 'radarrB')
radarrB_is_version_3 = get_config_value('RADARR_B_VERSION_3', 'version3', 'radarrB')
# get config settings from ENV or config files for Sonarr
sonarrA_url = get_config_value('SONARR_A_URL', 'url', 'sonarrA')
sonarrA_key = get_config_value('SONARR_A_KEY', 'key', 'sonarrA')
sonarrA_profile = get_config_value('SONARR_A_PROFILE', 'profile', 'sonarrA')
sonarrA_profile_id = get_config_value('SONARR_A_PROFILE_ID', 'profile_id', 'sonarrA')
sonarrA_profile_filter = get_config_value('SONARR_A_PROFILE_FILTER', 'profile_filter', 'sonarrA')
sonarrA_profile_filter_id = get_config_value('SONARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'sonarrA')
sonarrA_path = get_config_value('SONARR_A_PATH', 'path', 'sonarrA')
sonarrB_url = get_config_value('SONARR_B_URL', 'url', 'sonarrB')
sonarrB_key = get_config_value('SONARR_B_KEY', 'key', 'sonarrB')
sonarrB_profile = get_config_value('SONARR_B_PROFILE', 'profile', 'sonarrB')
sonarrB_profile_id = get_config_value('SONARR_B_PROFILE_ID', 'profile_id', 'sonarrB')
sonarrB_profile_filter = get_config_value('SONARR_A_PROFILE_FILTER', 'profile_filter', 'sonarrB')
sonarrB__profile_filter_id = get_config_value('SONARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'sonarrB')
sonarrB_path = get_config_value('SONARR_B_PATH', 'path', 'sonarrB')
# get config settings from ENV or config files for Lidarr
lidarrA_url = get_config_value('LIDARR_A_URL', 'url', 'lidarrA')
lidarrA_key = get_config_value('LIDARR_A_KEY', 'key', 'lidarrA')
lidarrA_profile = get_config_value('LIDARR_A_PROFILE', 'profile', 'lidarrA')
lidarrA_profile_id = get_config_value('LIDARR_A_PROFILE_ID', 'profile_id', 'lidarrA')
lidarrA_profile_filter = get_config_value('LIDARR_A_PROFILE_FILTER', 'profile_filter', 'lidarrA')
lidarrA_profile_filter_id = get_config_value('LIDARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'lidarrA')
lidarrA_path = get_config_value('LIDARR_A_PATH', 'path', 'lidarrA')
lidarrB_url = get_config_value('LIDARR_B_URL', 'url', 'lidarrB')
lidarrB_key = get_config_value('LIDARR_B_KEY', 'key', 'lidarrB')
lidarrB_profile = get_config_value('LIDARR_B_PROFILE', 'profile', 'lidarrB')
lidarrB_profile_id = get_config_value('LIDARR_B_PROFILE_ID', 'profile_id', 'lidarrB')
lidarrB_profile_filter = get_config_value('LIDARR_A_PROFILE_FILTER', 'profile_filter', 'lidarrB')
lidarrB_profile_filter_id = get_config_value('LIDARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'lidarrB')
lidarrB_path = get_config_value('LIDARR_B_PATH', 'path', 'lidarrB')
# get general conf options
sync_bidirectionally = get_config_value('SYNCARR_BIDIRECTIONAL_SYNC', 'bidirectional', 'general') or 0
if sync_bidirectionally:
sync_bidirectionally = int(sync_bidirectionally) or 0
########################################################################################################################
# make sure we have radarr, lidarr, OR sonarr
if (
(sonarrA_url and radarrA_url) or
@ -173,9 +66,14 @@ instanceB_profile_id = ''
instanceB_profile_filter = ''
instanceB_path = ''
api_version = 'v1/' # we are going to detect what API version we are on
tested_api_version = False # only get api version once
api_content_path = '' # url path to add content
api_search_path = '' # url path to search for content on RSS feeds
api_profile_path = '' # url path to get quality profiles
api_status_path = '' # url path to check on server status
is_radarr = False
is_sonarr = False
@ -183,10 +81,6 @@ is_lidarr = False
content_id_key = '' # the unique id for a content item
instanceA_is_v3 = False
instanceB_is_v3 = False
if radarrA_url and radarrB_url:
instanceA_url = radarrA_url
instanceA_key = radarrA_key
@ -204,15 +98,14 @@ if radarrA_url and radarrB_url:
instanceB_profile_filter_id = radarrB_profile_filter_id
instanceB_path = radarrB_path
api_content_path = 'api/movie'
api_search_path = 'api/command'
api_profile_path = 'api/profile'
api_version = '' # radarr v2 doesnt have version in api url
api_content_path = 'movie'
api_search_path = 'command'
api_profile_path = 'profile'
api_status_path = 'system/status'
content_id_key = 'tmdbId'
is_radarr = True
instanceA_is_v3 = True if radarrA_is_version_3 else False
instanceB_is_v3 = True if radarrB_is_version_3 else False
elif lidarrA_url and lidarrB_url:
instanceA_url = lidarrA_url
@ -231,15 +124,14 @@ elif lidarrA_url and lidarrB_url:
instanceB_profile_filter_id = lidarrB_profile_filter_id
instanceB_path = lidarrB_path
api_content_path = 'api/v1/artist'
api_search_path = 'api/v1/command'
api_profile_path = 'api/v1/qualityprofile'
api_version = 'v1/'
api_content_path = 'artist'
api_search_path = 'command'
api_profile_path = 'qualityprofile'
api_status_path = 'system/status'
content_id_key = 'foreignArtistId'
is_lidarr = True
instanceA_is_v3 = True
instanceB_is_v3 = True
elif sonarrA_url and sonarrB_url:
instanceA_url = sonarrA_url
@ -258,18 +150,18 @@ elif sonarrA_url and sonarrB_url:
instanceB_profile_filter_id = sonarrB_profile_filter_id
instanceB_path = sonarrB_path
api_content_path = 'api/v3/series'
api_search_path = 'api/command'
api_profile_path = 'api/v3/qualityprofile'
api_version = ''
api_content_path = 'series'
api_search_path = 'command'
api_profile_path = 'profile'
api_status_path = 'system/status'
content_id_key = 'tvdbId'
is_sonarr = True
instanceA_is_v3 = True
instanceB_is_v3 = True
########################################################################################################################
logger.debug({
'instanceA_url': instanceA_url,
'instanceA_key': instanceA_key,
@ -279,8 +171,6 @@ logger.debug({
'instanceB_path': instanceB_path,
'api_content_path': api_content_path,
'api_search_path': api_search_path,
'instanceA_is_v3': instanceA_is_v3,
'instanceB_is_v3': instanceB_is_v3,
'is_sonarr': is_sonarr,
'is_lidarr': is_lidarr,
})

@ -0,0 +1,106 @@
import os
import configparser
DEV = os.environ.get('DEV', False)
VER = '1.3.0'
def ConfigSectionMap(section):
'''get all config options from config file'''
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
def get_config_value(env_key, config_key, config_section):
if is_in_docker:
return os.environ.get(env_key)
try:
_config = ConfigSectionMap(config_section)
return _config.get(config_key)
except configparser.NoSectionError:
return ''
########################################################################################################################
# load config file
BASE_CONFIG = 'config.conf'
if DEV:
settingsFilename = os.path.join(os.getcwd(), 'dev-{}'.format(BASE_CONFIG))
else:
settingsFilename = os.path.join(os.getcwd(), BASE_CONFIG)
config = configparser.ConfigParser()
config.read(settingsFilename)
########################################################################################################################
# get docker based ENV vars
is_in_docker = os.environ.get('IS_IN_DOCKER')
instance_sync_interval_seconds = os.environ.get('SYNC_INTERVAL_SECONDS')
if instance_sync_interval_seconds:
instance_sync_interval_seconds = int(instance_sync_interval_seconds)
########################################################################################################################
# get config settings from ENV or config files for Radarr
radarrA_url = get_config_value('RADARR_A_URL', 'url', 'radarrA')
radarrA_key = get_config_value('RADARR_A_KEY', 'key', 'radarrA')
radarrA_profile = get_config_value('RADARR_A_PROFILE', 'profile', 'radarrA')
radarrA_profile_id = get_config_value('RADARR_A_PROFILE_ID', 'profile_id', 'radarrA')
radarrA_profile_filter = get_config_value('RADARR_A_PROFILE_FILTER', 'profile_filter', 'radarrA')
radarrA_profile_filter_id = get_config_value('RADARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'radarrA')
radarrA_path = get_config_value('RADARR_A_PATH', 'path', 'radarrA')
radarrB_url = get_config_value('RADARR_B_URL', 'url', 'radarrB')
radarrB_key = get_config_value('RADARR_B_KEY', 'key', 'radarrB')
radarrB_profile = get_config_value('RADARR_B_PROFILE', 'profile', 'radarrB')
radarrB_profile_id = get_config_value('RADARR_B_PROFILE_ID', 'profile_id', 'radarrB')
radarrB_profile_filter = get_config_value('RADARR_B_PROFILE_FILTER', 'profile_filter', 'radarrB')
radarrB_profile_filter_id = get_config_value('RADARR_B_PROFILE_FILTER_ID', 'profile_filter_id', 'radarrB')
radarrB_path = get_config_value('RADARR_B_PATH', 'path', 'radarrB')
# get config settings from ENV or config files for Sonarr
sonarrA_url = get_config_value('SONARR_A_URL', 'url', 'sonarrA')
sonarrA_key = get_config_value('SONARR_A_KEY', 'key', 'sonarrA')
sonarrA_profile = get_config_value('SONARR_A_PROFILE', 'profile', 'sonarrA')
sonarrA_profile_id = get_config_value('SONARR_A_PROFILE_ID', 'profile_id', 'sonarrA')
sonarrA_profile_filter = get_config_value('SONARR_A_PROFILE_FILTER', 'profile_filter', 'sonarrA')
sonarrA_profile_filter_id = get_config_value('SONARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'sonarrA')
sonarrA_path = get_config_value('SONARR_A_PATH', 'path', 'sonarrA')
sonarrB_url = get_config_value('SONARR_B_URL', 'url', 'sonarrB')
sonarrB_key = get_config_value('SONARR_B_KEY', 'key', 'sonarrB')
sonarrB_profile = get_config_value('SONARR_B_PROFILE', 'profile', 'sonarrB')
sonarrB_profile_id = get_config_value('SONARR_B_PROFILE_ID', 'profile_id', 'sonarrB')
sonarrB_profile_filter = get_config_value('SONARR_A_PROFILE_FILTER', 'profile_filter', 'sonarrB')
sonarrB_profile_filter_id = get_config_value('SONARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'sonarrB')
sonarrB_path = get_config_value('SONARR_B_PATH', 'path', 'sonarrB')
# get config settings from ENV or config files for Lidarr
lidarrA_url = get_config_value('LIDARR_A_URL', 'url', 'lidarrA')
lidarrA_key = get_config_value('LIDARR_A_KEY', 'key', 'lidarrA')
lidarrA_profile = get_config_value('LIDARR_A_PROFILE', 'profile', 'lidarrA')
lidarrA_profile_id = get_config_value('LIDARR_A_PROFILE_ID', 'profile_id', 'lidarrA')
lidarrA_profile_filter = get_config_value('LIDARR_A_PROFILE_FILTER', 'profile_filter', 'lidarrA')
lidarrA_profile_filter_id = get_config_value('LIDARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'lidarrA')
lidarrA_path = get_config_value('LIDARR_A_PATH', 'path', 'lidarrA')
lidarrB_url = get_config_value('LIDARR_B_URL', 'url', 'lidarrB')
lidarrB_key = get_config_value('LIDARR_B_KEY', 'key', 'lidarrB')
lidarrB_profile = get_config_value('LIDARR_B_PROFILE', 'profile', 'lidarrB')
lidarrB_profile_id = get_config_value('LIDARR_B_PROFILE_ID', 'profile_id', 'lidarrB')
lidarrB_profile_filter = get_config_value('LIDARR_A_PROFILE_FILTER', 'profile_filter', 'lidarrB')
lidarrB_profile_filter_id = get_config_value('LIDARR_A_PROFILE_FILTER_ID', 'profile_filter_id', 'lidarrB')
lidarrB_path = get_config_value('LIDARR_B_PATH', 'path', 'lidarrB')
# get general conf options
sync_bidirectionally = get_config_value('SYNCARR_BIDIRECTIONAL_SYNC', 'bidirectional', 'general') or 0
if sync_bidirectionally:
sync_bidirectionally = int(sync_bidirectionally) or 0

@ -0,0 +1,39 @@
from syncarr.config import *
def get_path(instance_url, api_path, key, checkV3=False):
global api_version, api_profile_path
logger.debug(f'checkV3: "{checkV3}" for {instance_url}')
if checkV3:
api_version = 'v3/'
if checkV3 and is_sonarr:
api_profile_path = 'qualityprofile'
url = f"{instance_url}/api/{api_version}{api_path}?apikey={key}"
print(url)
return url
def get_status_path(instance_url, key, checkV3):
url = get_path(instance_url, api_status_path, key, checkV3)
logger.debug('get_status_path: {}'.format(url))
return url
def get_content_path(instance_url, key):
url = get_path(instance_url, api_content_path, key)
logger.debug('get_content_path: {}'.format(url))
return url
def get_search_path(instance_url, key):
url = get_path(instance_url, api_search_path, key)
logger.debug('get_search_path: {}'.format(url))
return url
def get_profile_path(instance_url, key):
url = get_path(instance_url, api_profile_path, key)
logger.debug('get_profile_path: {}'.format(url))
return url
Loading…
Cancel
Save