From 5b3e9d864908a3cbd4a161764ab206c57676c968 Mon Sep 17 00:00:00 2001 From: "Nicholas St. Germain" Date: Wed, 28 Nov 2018 10:55:58 -0600 Subject: [PATCH] PEP8 Cleanup --- sonarr.py | 116 ++++++++++++++++++++---------------------------------- 1 file changed, 43 insertions(+), 73 deletions(-) diff --git a/sonarr.py b/sonarr.py index 8be5f6c..f504b74 100644 --- a/sonarr.py +++ b/sonarr.py @@ -9,8 +9,8 @@ import configuration def now_iso(): - now_iso = datetime.now(timezone.utc).astimezone().isoformat() - return now_iso + now = datetime.now(timezone.utc).astimezone().isoformat() + return now def influx_sender(influx_payload): @@ -22,10 +22,7 @@ def influx_sender(influx_payload): def get_all_missing_shows(): # Set the time here so we have one timestamp to work with now = now_iso() - - missing = [] - - influx_payload = [] + missing, influx_payload = [], [] for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list: @@ -36,19 +33,18 @@ def get_all_missing_shows(): tv_shows = {d['id']: d for d in get_tv_shows} - for show in tv_shows.keys(): series_title = '{}'.format(tv_shows[show]['series']['title']) - sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['seasonNumber'],tv_shows[show]['episodeNumber']) + sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['seasonNumber'], tv_shows[show]['episodeNumber']) missing.append((series_title, sxe, tv_shows[show]['id'], tv_shows[show]['title'])) - for series_title, sxe, id, episode_title in missing: + for series_title, sxe, sonarr_id, episode_title in missing: influx_payload.append( { "measurement": "Sonarr", "tags": { "type": "Missing", - "sonarrId": id, + "sonarrId": sonarr_id, "server": server_id }, "time": now, @@ -68,20 +64,16 @@ def get_all_missing_shows(): def get_missing_shows(days_past): # Set the time here so we have one timestamp to work with now = now_iso() - last_days = str(date.today()+timedelta(days=-days_past)) - today = str(date.today()) - - missing = [] - - influx_payload = [] + missing, influx_payload = [], [] for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list: headers = {'X-Api-Key': sonarr_api_key} - get_tv_shows = requests.get('{}/api/calendar/?start={}&end={}&pageSize=1000'.format(sonarr_url, last_days, today), + get_tv_shows = requests.get('{}/api/calendar/?start={}&end={}&pageSize=1000'.format(sonarr_url, last_days, + today), headers=headers).json() tv_shows = {d['id']: d for d in get_tv_shows} @@ -93,13 +85,13 @@ def get_missing_shows(days_past): air_date = (tv_shows[show]['airDate']) missing.append((series_title, sxe, air_date, tv_shows[show]['id'])) - for series_title, sxe, air_date, id in missing: + for series_title, sxe, air_date, sonarr_id in missing: influx_payload.append( { "measurement": "Sonarr", "tags": { "type": "Missing_Days", - "sonarrId": id, + "sonarrId": sonarr_id, "server": server_id }, "time": now, @@ -120,41 +112,39 @@ def get_missing_shows(days_past): def get_upcoming_shows(): # Set the time here so we have one timestamp to work with now = now_iso() - upcoming = [] - influx_payload = [] for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list: headers = {'X-Api-Key': sonarr_api_key} - get_upcoming_shows = requests.get('{}/api/calendar/'.format(sonarr_url), - headers=headers).json() + upcoming_shows_request = requests.get('{}/api/calendar/'.format(sonarr_url), headers=headers).json() - upcoming_shows = {d['id']: d for d in get_upcoming_shows} + upcoming_shows = {d['id']: d for d in upcoming_shows_request} for show in upcoming_shows.keys(): series_title = '{}'.format(upcoming_shows[show]['series']['title']) - sxe = 'S{:0>2}E{:0>2}'.format(upcoming_shows[show]['seasonNumber'],upcoming_shows[show]['episodeNumber']) - upcoming.append((series_title, sxe, upcoming_shows[show]['id'], upcoming_shows[show]['title'], upcoming_shows[show]['airDate'])) + sxe = 'S{:0>2}E{:0>2}'.format(upcoming_shows[show]['seasonNumber'], upcoming_shows[show]['episodeNumber']) + upcoming.append((series_title, sxe, upcoming_shows[show]['id'], upcoming_shows[show]['title'], + upcoming_shows[show]['airDate'])) - for series_title, sxe, id, episode_title, air_date in upcoming: + for series_title, sxe, sonarr_id, episode_title, air_date in upcoming: influx_payload.append( { "measurement": "Sonarr", "tags": { "type": "Soon", - "sonarrId": id, + "sonarrId": sonarr_id, "server": server_id - }, - "time": now, - "fields": { - "name": series_title, - "epname": episode_title, - "sxe": sxe, - "airs": air_date - } + }, + "time": now, + "fields": { + "name": series_title, + "epname": episode_title, + "sxe": sxe, + "airs": air_date + } } ) # Empty upcoming or else things get foo bared @@ -168,13 +158,8 @@ def get_future_shows(future_days): now = now_iso() today = str(date.today()) - future = str(date.today()+timedelta(days=future_days)) - air_days = [] - - downloaded = [] - influx_payload = [] for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list: @@ -190,15 +175,16 @@ def get_future_shows(future_days): series_title = '{}'.format(tv_shows[show]['series']['title']) dl_status = int(tv_shows[show]['hasFile']) sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['seasonNumber'], tv_shows[show]['episodeNumber']) - air_days.append((series_title, dl_status, sxe, tv_shows[show]['title'], tv_shows[show]['airDate'], tv_shows[show]['id'])) + air_days.append((series_title, dl_status, sxe, tv_shows[show]['title'], tv_shows[show]['airDate'], + tv_shows[show]['id'])) - for series_title, dl_status, sxe, episode_title, air_date, id in air_days: + for series_title, dl_status, sxe, episode_title, air_date, sonarr_id in air_days: influx_payload.append( { "measurement": "Sonarr", "tags": { "type": "Future", - "sonarrId": id, + "sonarrId": sonarr_id, "server": server_id }, "time": now, @@ -220,11 +206,7 @@ def get_future_shows(future_days): def get_queue_shows(): # Set the time here so we have one timestamp to work with now = now_iso() - queue = [] - - downloaded = [] - influx_payload = [] for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list: @@ -239,8 +221,9 @@ def get_queue_shows(): for show in tv_shows.keys(): series_title = '{}'.format(tv_shows[show]['series']['title']) episode_title = '{}'.format(tv_shows[show]['episode']['title']) - protocol = (tv_shows[show]['protocol'].upper()) - sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['episode']['seasonNumber'], tv_shows[show]['episode']['episodeNumber']) + protocol = tv_shows[show]['protocol'].upper() + sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['episode']['seasonNumber'], + tv_shows[show]['episode']['episodeNumber']) if protocol == 'USENET': protocol_id = 1 else: @@ -248,13 +231,13 @@ def get_queue_shows(): queue.append((series_title, episode_title, protocol, protocol_id, sxe, tv_shows[show]['id'])) - for series_title, episode_title, protocol, protocol_id, sxe, id in queue: + for series_title, episode_title, protocol, protocol_id, sxe, sonarr_id in queue: influx_payload.append( { "measurement": "Sonarr", "tags": { "type": "Queue", - "sonarrId": id, + "sonarrId": sonarr_id, "server": server_id }, @@ -277,41 +260,28 @@ def get_queue_shows(): if __name__ == "__main__": parser = argparse.ArgumentParser(prog='Sonarr stats operations', - description='Script to aid in data gathering from Sonarr', formatter_class=RawTextHelpFormatter) + description='Script to aid in data gathering from Sonarr', + formatter_class=RawTextHelpFormatter) - parser.add_argument("--missing", action='store_true', - help='Get all missing TV shows') - - parser.add_argument("--missing_days", type=int, - help='Get missing TV shows in past X days') - - parser.add_argument("--upcoming", action='store_true', - help='Get upcoming TV shows') - - parser.add_argument("--future", type=int, - help='Get TV shows on X days into the future. Includes today.' - '\ni.e. --future 2 is Today and Tomorrow') - - parser.add_argument("--queue", action='store_true', - help='Get TV shows in queue') + parser.add_argument("--missing", action='store_true', help='Get all missing TV shows') + parser.add_argument("--missing_days", type=int, help='Get missing TV shows in past X days') + parser.add_argument("--upcoming", action='store_true', help='Get upcoming TV shows') + parser.add_argument("--future", type=int, help='Get TV shows on X days into the future. Includes today.' + '\ni.e. --future 2 is Today and Tomorrow') + parser.add_argument("--queue", action='store_true', help='Get TV shows in queue') opts = parser.parse_args() if opts.missing: influx_sender(get_all_missing_shows()) - elif opts.missing_days: influx_sender(get_missing_shows(opts.missing_days)) - elif opts.upcoming: influx_sender(get_upcoming_shows()) - elif opts.future: influx_sender(get_future_shows(opts.future)) - elif opts.queue: influx_sender(get_queue_shows()) - elif len(sys.argv) == 1: parser.print_help(sys.stderr) sys.exit(1)