Refactor main file a bit

pull/54/head
Mitchell Klijs 7 years ago committed by desimaniac
parent ec68232c85
commit a1dd11acc9

@ -62,13 +62,61 @@ def trakt_authentication():
from media.trakt import Trakt
trakt = Trakt(cfg)
response = trakt.oauth_authentication()
if response:
if trakt.oauth_authentication():
log.info("Authentication information saved; please restart the application")
exit()
def validate_trakt(trakt, notifications):
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to validate Trakt API Key'})
exit()
else:
log.info("Validated Trakt API Key")
def validate_pvr(pvr, type, notifications):
if not pvr.validate_api_key():
log.error("Aborting due to failure to validate %s URL / API Key", type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to validate %s URL / API Key' % type})
return None
else:
log.info("Validated %s URL & API Key", type)
def get_profile_id(pvr, profile):
# retrieve profile id for requested profile
profile_id = pvr.get_profile_id(profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", profile)
exit()
log.info("Retrieved Profile ID for %s: %d", profile, profile_id)
return profile_id
def get_profile_tags(pvr):
profile_tags = pvr.get_tags()
if profile_tags is None:
log.error("Aborting due to failure to retrieve Tag ID's")
exit()
log.info("Retrieved %d Tag ID's", len(profile_tags))
return profile_tags
def get_objects(pvr, type, notifications):
objects_list = pvr.get_objects()
if not objects_list:
log.error("Aborting due to failure to retrieve %s shows list", type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to retrieve %s shows list' % type})
exit()
log.info("Retrieved %s shows list, shows found: %d", type, len(objects_list))
return objects_list
############################################################
# SHOWS
############################################################
@ -86,36 +134,14 @@ def show(show_id, folder=None, no_search=False):
if folder:
cfg['sonarr']['root_folder'] = folder
# validate trakt api_key
trakt = Trakt(cfg)
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
return None
else:
log.info("Validated Trakt API Key")
# validate sonarr url & api_key
sonarr = Sonarr(cfg.sonarr.url, cfg.sonarr.api_key)
if not sonarr.validate_api_key():
log.error("Aborting due to failure to validate Sonarr URL / API Key")
return None
else:
log.info("Validated Sonarr URL & API Key")
# retrieve profile id for requested profile
profile_id = sonarr.get_profile_id(cfg.sonarr.profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", cfg.sonarr.profile)
return None
else:
log.info("Retrieved Profile ID for %s: %d", cfg.sonarr.profile, profile_id)
validate_trakt(trakt, False)
validate_pvr(sonarr, 'Sonarr', False)
# retrieve profile tags
profile_tags = sonarr.get_tags()
if profile_tags is None:
log.error("Aborting due to failure to retrieve Tag ID's")
else:
log.info("Retrieved %d Tag ID's", len(profile_tags))
profile_id = get_profile_id(sonarr, cfg.sonarr.profile)
profile_tags = get_profile_tags(sonarr)
# get trakt show
trakt_show = trakt.get_show(show_id)
@ -128,8 +154,7 @@ def show(show_id, folder=None, no_search=False):
trakt_show['year'])
# determine which tags to use when adding this series
use_tags = sonarr_helper.series_tag_id_from_network(profile_tags, cfg.sonarr.tags,
trakt_show['network'])
use_tags = sonarr_helper.series_tag_id_from_network(profile_tags, cfg.sonarr.tags, trakt_show['network'])
# add show to sonarr
if sonarr.add_series(trakt_show['ids']['tvdb'], trakt_show['title'], trakt_show['ids']['slug'], profile_id,
@ -174,73 +199,29 @@ def shows(list_type, add_limit=0, add_delay=2.5, genre=None, folder=None, no_sea
# validate trakt client_id
trakt = Trakt(cfg)
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to validate Trakt API Key'})
return None
else:
log.info("Validated Trakt API Key")
# validate sonarr url & api_key
sonarr = Sonarr(cfg.sonarr.url, cfg.sonarr.api_key)
if not sonarr.validate_api_key():
log.error("Aborting due to failure to validate Sonarr URL / API Key")
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to validate Sonarr URL / API Key'})
return None
else:
log.info("Validated Sonarr URL & API Key")
# retrieve profile id for requested profile
profile_id = sonarr.get_profile_id(cfg.sonarr.profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", cfg.sonarr.profile)
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to retrieve Sonarr Profile ID of %s' % cfg.sonarr.profile})
return None
else:
log.info("Retrieved Profile ID for %s: %d", cfg.sonarr.profile, profile_id)
validate_trakt(trakt, notifications)
validate_pvr(sonarr, 'Sonarr', notifications)
# retrieve profile tags
profile_tags = sonarr.get_tags()
if profile_tags is None:
log.error("Aborting due to failure to retrieve Tag ID's")
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': "Failure to retrieve Sonarr Tag ID's"})
return None
else:
log.info("Retrieved %d Tag ID's", len(profile_tags))
profile_id = get_profile_id(sonarr, cfg.sonarr.profile)
profile_tags = get_profile_tags(sonarr)
# get sonarr series list
sonarr_series_list = sonarr.get_objects()
if not sonarr_series_list:
log.error("Aborting due to failure to retrieve Sonarr shows list")
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to retrieve Sonarr shows list'})
return None
else:
log.info("Retrieved Sonarr shows list, shows found: %d", len(sonarr_series_list))
pvr_objects_list = get_objects(sonarr, 'Sonarr', notifications)
# get trakt series list
trakt_series_list = None
if list_type.lower() == 'anticipated':
trakt_series_list = trakt.get_anticipated_shows()
trakt_objects_list = trakt.get_anticipated_shows()
elif list_type.lower() == 'trending':
trakt_series_list = trakt.get_trending_shows()
trakt_objects_list = trakt.get_trending_shows()
elif list_type.lower() == 'popular':
trakt_series_list = trakt.get_popular_shows()
trakt_objects_list = trakt.get_popular_shows()
elif list_type.lower() == 'watchlist':
trakt_series_list = trakt.get_watchlist_shows(authenticate_user)
trakt_objects_list = trakt.get_watchlist_shows(authenticate_user)
else:
trakt_series_list = trakt.get_user_list_shows(list_type, authenticate_user)
trakt_objects_list = trakt.get_user_list_shows(list_type, authenticate_user)
if not trakt_series_list:
if not trakt_objects_list:
log.error("Aborting due to failure to retrieve Trakt %s shows list", list_type)
if notifications:
callback_notify(
@ -248,10 +229,10 @@ def shows(list_type, add_limit=0, add_delay=2.5, genre=None, folder=None, no_sea
'reason': 'Failure to retrieve Trakt %s shows list' % list_type})
return None
else:
log.info("Retrieved Trakt %s shows list, shows found: %d", list_type, len(trakt_series_list))
log.info("Retrieved Trakt %s shows list, shows found: %d", list_type, len(trakt_objects_list))
# build filtered series list without series that exist in sonarr
processed_series_list = sonarr_helper.remove_existing_series(sonarr_series_list, trakt_series_list)
processed_series_list = sonarr_helper.remove_existing_series(pvr_objects_list, trakt_objects_list)
if processed_series_list is None:
log.error("Aborting due to failure to remove existing Sonarr shows from retrieved Trakt shows list")
if notifications:
@ -284,7 +265,7 @@ def shows(list_type, add_limit=0, add_delay=2.5, genre=None, folder=None, no_sea
# determine which tags to use when adding this series
use_tags = sonarr_helper.series_tag_id_from_network(profile_tags, cfg.sonarr.tags,
series['show']['network'])
series['show']['network'])
# add show to sonarr
if sonarr.add_series(series['show']['ids']['tvdb'], series['show']['title'],
series['show']['ids']['slug'], profile_id, cfg.sonarr.root_folder, use_tags,
@ -335,26 +316,12 @@ def movie(movie_id, folder=None, no_search=False):
# validate trakt api_key
trakt = Trakt(cfg)
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
return None
else:
log.info("Validated Trakt API Key")
# validate radarr url & api_key
radarr = Radarr(cfg.radarr.url, cfg.radarr.api_key)
if not radarr.validate_api_key():
log.error("Aborting due to failure to validate Radarr URL / API Key")
return None
else:
log.info("Validated Radarr URL & API Key")
# retrieve profile id for requested profile
profile_id = radarr.get_profile_id(cfg.radarr.profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", cfg.radarr.profile)
else:
log.info("Retrieved Profile ID for %s: %d", cfg.radarr.profile, profile_id)
validate_trakt(trakt, False)
validate_pvr(radarr, 'Radarr', False)
profile_id = get_profile_id(radarr, cfg.sonarr.profile)
# get trakt movie
trakt_movie = trakt.get_movie(movie_id)
@ -408,65 +375,30 @@ def movies(list_type, add_limit=0, add_delay=2.5, genre=None, folder=None, no_se
# validate trakt api_key
trakt = Trakt(cfg)
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
if notifications:
callback_notify({'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to validate Trakt API Key'})
return None
else:
log.info("Validated Trakt API Key")
# validate radarr url & api_key
radarr = Radarr(cfg.radarr.url, cfg.radarr.api_key)
if not radarr.validate_api_key():
log.error("Aborting due to failure to validate Radarr URL / API Key")
if notifications:
callback_notify(
{'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to validate Radarr URL / API Key'})
return None
else:
log.info("Validated Radarr URL & API Key")
# retrieve profile id for requested profile
profile_id = radarr.get_profile_id(cfg.radarr.profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", cfg.radarr.profile)
if notifications:
callback_notify({'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to retrieve Radarr Profile ID of %s' % cfg.radarr.profile})
return None
else:
log.info("Retrieved Profile ID for %s: %d", cfg.radarr.profile, profile_id)
validate_trakt(trakt, notifications)
validate_pvr(radarr, 'Radarr', notifications)
# get radarr movies list
radarr_movie_list = radarr.get_objects()
if not radarr_movie_list:
log.error("Aborting due to failure to retrieve Radarr movies list")
if notifications:
callback_notify({'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to retrieve Radarr movies list'})
return None
else:
log.info("Retrieved Radarr movies list, movies found: %d", len(radarr_movie_list))
profile_id = get_profile_id(radarr, cfg.sonarr.profile)
pvr_objects_list = get_objects(radarr, 'Radarr', notifications)
# get trakt movies list
trakt_movies_list = None
if list_type.lower() == 'anticipated':
trakt_movies_list = trakt.get_anticipated_movies()
trakt_objects_list = trakt.get_anticipated_movies()
elif list_type.lower() == 'trending':
trakt_movies_list = trakt.get_trending_movies()
trakt_objects_list = trakt.get_trending_movies()
elif list_type.lower() == 'popular':
trakt_movies_list = trakt.get_popular_movies()
trakt_objects_list = trakt.get_popular_movies()
elif list_type.lower() == 'boxoffice':
trakt_movies_list = trakt.get_boxoffice_movies()
trakt_objects_list = trakt.get_boxoffice_movies()
elif list_type.lower() == 'watchlist':
trakt_movies_list = trakt.get_watchlist_movies(authenticate_user)
trakt_objects_list = trakt.get_watchlist_movies(authenticate_user)
else:
trakt_movies_list = trakt.get_user_list_movies(list_type, authenticate_user)
trakt_objects_list = trakt.get_user_list_movies(list_type, authenticate_user)
if not trakt_movies_list:
if not trakt_objects_list:
log.error("Aborting due to failure to retrieve Trakt %s movies list", list_type)
if notifications:
callback_notify(
@ -474,10 +406,10 @@ def movies(list_type, add_limit=0, add_delay=2.5, genre=None, folder=None, no_se
'reason': 'Failure to retrieve Trakt %s movies list' % list_type})
return None
else:
log.info("Retrieved Trakt %s movies list, movies found: %d", list_type, len(trakt_movies_list))
log.info("Retrieved Trakt %s movies list, movies found: %d", list_type, len(trakt_objects_list))
# build filtered movie list without movies that exist in radarr
processed_movies_list = radarr_helper.remove_existing_movies(radarr_movie_list, trakt_movies_list)
processed_movies_list = radarr_helper.remove_existing_movies(pvr_objects_list, trakt_objects_list)
if processed_movies_list is None:
log.error("Aborting due to failure to remove existing Radarr movies from retrieved Trakt movies list")
if notifications:
@ -556,6 +488,9 @@ def callback_notify(data):
elif data['event'] == 'abort':
notify.send(message="Aborted adding Trakt %s %s due to: %s" % (data['list_type'], data['type'], data['reason']))
return
elif data['event'] == 'error':
notify.send(message="Error: %s" % data['reason'])
return
else:
log.error("Unexpected callback: %s", data)
return

Loading…
Cancel
Save