PEP8 Cleanup
This commit is contained in:
parent
8dfb5cc384
commit
5b3e9d8649
1 changed files with 43 additions and 73 deletions
116
sonarr.py
116
sonarr.py
|
@ -9,8 +9,8 @@ import configuration
|
||||||
|
|
||||||
|
|
||||||
def now_iso():
|
def now_iso():
|
||||||
now_iso = datetime.now(timezone.utc).astimezone().isoformat()
|
now = datetime.now(timezone.utc).astimezone().isoformat()
|
||||||
return now_iso
|
return now
|
||||||
|
|
||||||
|
|
||||||
def influx_sender(influx_payload):
|
def influx_sender(influx_payload):
|
||||||
|
@ -22,10 +22,7 @@ def influx_sender(influx_payload):
|
||||||
def get_all_missing_shows():
|
def get_all_missing_shows():
|
||||||
# Set the time here so we have one timestamp to work with
|
# Set the time here so we have one timestamp to work with
|
||||||
now = now_iso()
|
now = now_iso()
|
||||||
|
missing, influx_payload = [], []
|
||||||
missing = []
|
|
||||||
|
|
||||||
influx_payload = []
|
|
||||||
|
|
||||||
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
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}
|
tv_shows = {d['id']: d for d in get_tv_shows}
|
||||||
|
|
||||||
|
|
||||||
for show in tv_shows.keys():
|
for show in tv_shows.keys():
|
||||||
series_title = '{}'.format(tv_shows[show]['series']['title'])
|
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']))
|
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(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Missing",
|
"type": "Missing",
|
||||||
"sonarrId": id,
|
"sonarrId": sonarr_id,
|
||||||
"server": server_id
|
"server": server_id
|
||||||
},
|
},
|
||||||
"time": now,
|
"time": now,
|
||||||
|
@ -68,20 +64,16 @@ def get_all_missing_shows():
|
||||||
def get_missing_shows(days_past):
|
def get_missing_shows(days_past):
|
||||||
# Set the time here so we have one timestamp to work with
|
# Set the time here so we have one timestamp to work with
|
||||||
now = now_iso()
|
now = now_iso()
|
||||||
|
|
||||||
last_days = str(date.today()+timedelta(days=-days_past))
|
last_days = str(date.today()+timedelta(days=-days_past))
|
||||||
|
|
||||||
today = str(date.today())
|
today = str(date.today())
|
||||||
|
missing, influx_payload = [], []
|
||||||
missing = []
|
|
||||||
|
|
||||||
influx_payload = []
|
|
||||||
|
|
||||||
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
||||||
|
|
||||||
headers = {'X-Api-Key': sonarr_api_key}
|
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()
|
headers=headers).json()
|
||||||
|
|
||||||
tv_shows = {d['id']: d for d in get_tv_shows}
|
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'])
|
air_date = (tv_shows[show]['airDate'])
|
||||||
missing.append((series_title, sxe, air_date, tv_shows[show]['id']))
|
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(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Missing_Days",
|
"type": "Missing_Days",
|
||||||
"sonarrId": id,
|
"sonarrId": sonarr_id,
|
||||||
"server": server_id
|
"server": server_id
|
||||||
},
|
},
|
||||||
"time": now,
|
"time": now,
|
||||||
|
@ -120,41 +112,39 @@ def get_missing_shows(days_past):
|
||||||
def get_upcoming_shows():
|
def get_upcoming_shows():
|
||||||
# Set the time here so we have one timestamp to work with
|
# Set the time here so we have one timestamp to work with
|
||||||
now = now_iso()
|
now = now_iso()
|
||||||
|
|
||||||
upcoming = []
|
upcoming = []
|
||||||
|
|
||||||
influx_payload = []
|
influx_payload = []
|
||||||
|
|
||||||
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
||||||
|
|
||||||
headers = {'X-Api-Key': sonarr_api_key}
|
headers = {'X-Api-Key': sonarr_api_key}
|
||||||
|
|
||||||
get_upcoming_shows = requests.get('{}/api/calendar/'.format(sonarr_url),
|
upcoming_shows_request = requests.get('{}/api/calendar/'.format(sonarr_url), headers=headers).json()
|
||||||
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():
|
for show in upcoming_shows.keys():
|
||||||
series_title = '{}'.format(upcoming_shows[show]['series']['title'])
|
series_title = '{}'.format(upcoming_shows[show]['series']['title'])
|
||||||
sxe = 'S{:0>2}E{:0>2}'.format(upcoming_shows[show]['seasonNumber'],upcoming_shows[show]['episodeNumber'])
|
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']))
|
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(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Soon",
|
"type": "Soon",
|
||||||
"sonarrId": id,
|
"sonarrId": sonarr_id,
|
||||||
"server": server_id
|
"server": server_id
|
||||||
},
|
},
|
||||||
"time": now,
|
"time": now,
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": series_title,
|
"name": series_title,
|
||||||
"epname": episode_title,
|
"epname": episode_title,
|
||||||
"sxe": sxe,
|
"sxe": sxe,
|
||||||
"airs": air_date
|
"airs": air_date
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
# Empty upcoming or else things get foo bared
|
# Empty upcoming or else things get foo bared
|
||||||
|
@ -168,13 +158,8 @@ def get_future_shows(future_days):
|
||||||
now = now_iso()
|
now = now_iso()
|
||||||
|
|
||||||
today = str(date.today())
|
today = str(date.today())
|
||||||
|
|
||||||
future = str(date.today()+timedelta(days=future_days))
|
future = str(date.today()+timedelta(days=future_days))
|
||||||
|
|
||||||
air_days = []
|
air_days = []
|
||||||
|
|
||||||
downloaded = []
|
|
||||||
|
|
||||||
influx_payload = []
|
influx_payload = []
|
||||||
|
|
||||||
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
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'])
|
series_title = '{}'.format(tv_shows[show]['series']['title'])
|
||||||
dl_status = int(tv_shows[show]['hasFile'])
|
dl_status = int(tv_shows[show]['hasFile'])
|
||||||
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'])
|
||||||
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(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Future",
|
"type": "Future",
|
||||||
"sonarrId": id,
|
"sonarrId": sonarr_id,
|
||||||
"server": server_id
|
"server": server_id
|
||||||
},
|
},
|
||||||
"time": now,
|
"time": now,
|
||||||
|
@ -220,11 +206,7 @@ def get_future_shows(future_days):
|
||||||
def get_queue_shows():
|
def get_queue_shows():
|
||||||
# Set the time here so we have one timestamp to work with
|
# Set the time here so we have one timestamp to work with
|
||||||
now = now_iso()
|
now = now_iso()
|
||||||
|
|
||||||
queue = []
|
queue = []
|
||||||
|
|
||||||
downloaded = []
|
|
||||||
|
|
||||||
influx_payload = []
|
influx_payload = []
|
||||||
|
|
||||||
for sonarr_url, sonarr_api_key, server_id in configuration.sonarr_server_list:
|
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():
|
for show in tv_shows.keys():
|
||||||
series_title = '{}'.format(tv_shows[show]['series']['title'])
|
series_title = '{}'.format(tv_shows[show]['series']['title'])
|
||||||
episode_title = '{}'.format(tv_shows[show]['episode']['title'])
|
episode_title = '{}'.format(tv_shows[show]['episode']['title'])
|
||||||
protocol = (tv_shows[show]['protocol'].upper())
|
protocol = tv_shows[show]['protocol'].upper()
|
||||||
sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['episode']['seasonNumber'], tv_shows[show]['episode']['episodeNumber'])
|
sxe = 'S{:0>2}E{:0>2}'.format(tv_shows[show]['episode']['seasonNumber'],
|
||||||
|
tv_shows[show]['episode']['episodeNumber'])
|
||||||
if protocol == 'USENET':
|
if protocol == 'USENET':
|
||||||
protocol_id = 1
|
protocol_id = 1
|
||||||
else:
|
else:
|
||||||
|
@ -248,13 +231,13 @@ def get_queue_shows():
|
||||||
|
|
||||||
queue.append((series_title, episode_title, protocol, protocol_id, sxe, tv_shows[show]['id']))
|
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(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Queue",
|
"type": "Queue",
|
||||||
"sonarrId": id,
|
"sonarrId": sonarr_id,
|
||||||
"server": server_id
|
"server": server_id
|
||||||
|
|
||||||
},
|
},
|
||||||
|
@ -277,41 +260,28 @@ def get_queue_shows():
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(prog='Sonarr stats operations',
|
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',
|
parser.add_argument("--missing", action='store_true', help='Get all missing TV shows')
|
||||||
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("--missing_days", type=int,
|
parser.add_argument("--future", type=int, help='Get TV shows on X days into the future. Includes today.'
|
||||||
help='Get missing TV shows in past X days')
|
'\ni.e. --future 2 is Today and Tomorrow')
|
||||||
|
parser.add_argument("--queue", action='store_true', help='Get TV shows in queue')
|
||||||
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()
|
opts = parser.parse_args()
|
||||||
|
|
||||||
if opts.missing:
|
if opts.missing:
|
||||||
influx_sender(get_all_missing_shows())
|
influx_sender(get_all_missing_shows())
|
||||||
|
|
||||||
elif opts.missing_days:
|
elif opts.missing_days:
|
||||||
influx_sender(get_missing_shows(opts.missing_days))
|
influx_sender(get_missing_shows(opts.missing_days))
|
||||||
|
|
||||||
elif opts.upcoming:
|
elif opts.upcoming:
|
||||||
influx_sender(get_upcoming_shows())
|
influx_sender(get_upcoming_shows())
|
||||||
|
|
||||||
elif opts.future:
|
elif opts.future:
|
||||||
influx_sender(get_future_shows(opts.future))
|
influx_sender(get_future_shows(opts.future))
|
||||||
|
|
||||||
elif opts.queue:
|
elif opts.queue:
|
||||||
influx_sender(get_queue_shows())
|
influx_sender(get_queue_shows())
|
||||||
|
|
||||||
elif len(sys.argv) == 1:
|
elif len(sys.argv) == 1:
|
||||||
parser.print_help(sys.stderr)
|
parser.print_help(sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue