2018-12-17 17:12:37 -08:00
|
|
|
from logging import getLogger
|
2018-12-04 08:45:18 -08:00
|
|
|
from requests import Session, Request
|
|
|
|
from datetime import datetime, timezone, date, timedelta
|
|
|
|
|
2018-12-29 22:43:10 -08:00
|
|
|
from varken.structures import Queue, SonarrTVShow
|
2018-12-17 17:12:37 -08:00
|
|
|
from varken.helpers import hashit, connection_handler
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-06 09:17:57 -08:00
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
class SonarrAPI(object):
|
|
|
|
def __init__(self, server, dbmanager):
|
|
|
|
self.dbmanager = dbmanager
|
|
|
|
self.server = server
|
|
|
|
# Create session to reduce server web thread load, and globally define pageSize for all requests
|
|
|
|
self.session = Session()
|
|
|
|
self.session.headers = {'X-Api-Key': self.server.api_key}
|
|
|
|
self.session.params = {'pageSize': 1000}
|
2018-12-17 17:12:37 -08:00
|
|
|
self.logger = getLogger()
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-04 19:17:33 -08:00
|
|
|
def __repr__(self):
|
2018-12-17 17:12:37 -08:00
|
|
|
return f"<sonarr-{self.server.id}>"
|
2018-12-04 19:17:33 -08:00
|
|
|
|
2019-04-24 16:51:32 -07:00
|
|
|
def get_calendar(self, query="Missing"):
|
2018-12-04 08:45:18 -08:00
|
|
|
endpoint = '/api/calendar/'
|
2018-12-10 22:01:24 -08:00
|
|
|
today = str(date.today())
|
2019-04-24 16:51:32 -07:00
|
|
|
last_days = str(date.today() - timedelta(days=self.server.missing_days))
|
2018-12-04 08:45:18 -08:00
|
|
|
future = str(date.today() + timedelta(days=self.server.future_days))
|
2019-04-24 16:51:32 -07:00
|
|
|
now = datetime.now(timezone.utc).astimezone().isoformat()
|
|
|
|
if query == "Missing":
|
|
|
|
params = {'start': last_days, 'end': today}
|
|
|
|
else:
|
|
|
|
params = {'start': today, 'end': future}
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload = []
|
|
|
|
air_days = []
|
2019-04-24 16:51:32 -07:00
|
|
|
missing = []
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
req = self.session.prepare_request(Request('GET', self.server.url + endpoint, params=params))
|
|
|
|
get = connection_handler(self.session, req, self.server.verify_ssl)
|
|
|
|
|
|
|
|
if not get:
|
|
|
|
return
|
|
|
|
|
2019-04-18 19:28:23 -07:00
|
|
|
tv_shows = []
|
|
|
|
for show in get:
|
|
|
|
try:
|
2019-04-19 10:48:04 -07:00
|
|
|
tv_shows.append(SonarrTVShow(**show))
|
2019-04-18 19:28:23 -07:00
|
|
|
except TypeError as e:
|
2019-04-19 10:48:04 -07:00
|
|
|
self.logger.error('TypeError has occurred : %s while creating SonarrTVShow structure for show. Data '
|
|
|
|
'attempted is: %s', e, show)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
for show in tv_shows:
|
2018-12-17 17:12:37 -08:00
|
|
|
sxe = f'S{show.seasonNumber:0>2}E{show.episodeNumber:0>2}'
|
2018-12-04 08:45:18 -08:00
|
|
|
if show.hasFile:
|
|
|
|
downloaded = 1
|
|
|
|
else:
|
|
|
|
downloaded = 0
|
2019-04-24 16:51:32 -07:00
|
|
|
if query == "Missing":
|
2019-08-09 13:59:21 -07:00
|
|
|
if show.monitored and not downloaded:
|
2019-05-08 23:21:17 -07:00
|
|
|
missing.append((show.series['title'], downloaded, sxe, show.title, show.airDateUtc, show.id))
|
2019-04-24 16:51:32 -07:00
|
|
|
else:
|
|
|
|
air_days.append((show.series['title'], downloaded, sxe, show.title, show.airDateUtc, show.id))
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2019-04-24 16:51:32 -07:00
|
|
|
for series_title, dl_status, sxe, episode_title, air_date_utc, sonarr_id in (air_days or missing):
|
2018-12-17 17:12:37 -08:00
|
|
|
hash_id = hashit(f'{self.server.id}{series_title}{sxe}')
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload.append(
|
|
|
|
{
|
|
|
|
"measurement": "Sonarr",
|
|
|
|
"tags": {
|
2019-04-24 16:51:32 -07:00
|
|
|
"type": query,
|
2018-12-04 08:45:18 -08:00
|
|
|
"sonarrId": sonarr_id,
|
|
|
|
"server": self.server.id,
|
|
|
|
"name": series_title,
|
|
|
|
"epname": episode_title,
|
|
|
|
"sxe": sxe,
|
2018-12-14 19:47:27 -08:00
|
|
|
"airsUTC": air_date_utc,
|
2018-12-04 08:45:18 -08:00
|
|
|
"downloaded": dl_status
|
|
|
|
},
|
2018-12-10 22:01:24 -08:00
|
|
|
"time": now,
|
2018-12-04 08:45:18 -08:00
|
|
|
"fields": {
|
|
|
|
"hash": hash_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.dbmanager.write_points(influx_payload)
|
|
|
|
|
|
|
|
def get_queue(self):
|
|
|
|
influx_payload = []
|
|
|
|
endpoint = '/api/queue'
|
2018-12-10 22:01:24 -08:00
|
|
|
now = datetime.now(timezone.utc).astimezone().isoformat()
|
2018-12-04 08:45:18 -08:00
|
|
|
queue = []
|
|
|
|
|
|
|
|
req = self.session.prepare_request(Request('GET', self.server.url + endpoint))
|
|
|
|
get = connection_handler(self.session, req, self.server.verify_ssl)
|
|
|
|
|
|
|
|
if not get:
|
|
|
|
return
|
|
|
|
|
2019-04-18 19:28:23 -07:00
|
|
|
download_queue = []
|
|
|
|
for show in get:
|
|
|
|
try:
|
2019-04-19 10:48:04 -07:00
|
|
|
download_queue.append(Queue(**show))
|
2019-04-18 19:28:23 -07:00
|
|
|
except TypeError as e:
|
2019-04-19 10:48:04 -07:00
|
|
|
self.logger.error('TypeError has occurred : %s while creating Queue structure. Data attempted is: '
|
|
|
|
'%s', e, show)
|
2019-04-18 19:28:23 -07:00
|
|
|
if not download_queue:
|
2018-12-06 09:39:51 -08:00
|
|
|
return
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
for show in download_queue:
|
2019-03-13 11:35:49 -07:00
|
|
|
try:
|
|
|
|
sxe = f"S{show.episode['seasonNumber']:0>2}E{show.episode['episodeNumber']:0>2}"
|
|
|
|
except TypeError as e:
|
|
|
|
self.logger.error('TypeError has occurred : %s while processing the sonarr queue. \
|
2019-04-19 10:48:04 -07:00
|
|
|
Remove invalid queue entry. Data attempted is: %s', e, show)
|
2019-03-13 11:35:49 -07:00
|
|
|
continue
|
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
if show.protocol.upper() == 'USENET':
|
|
|
|
protocol_id = 1
|
|
|
|
else:
|
|
|
|
protocol_id = 0
|
|
|
|
|
|
|
|
queue.append((show.series['title'], show.episode['title'], show.protocol.upper(),
|
2018-12-18 23:52:13 -08:00
|
|
|
protocol_id, sxe, show.id, show.quality['quality']['name']))
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-18 23:52:13 -08:00
|
|
|
for series_title, episode_title, protocol, protocol_id, sxe, sonarr_id, quality in queue:
|
2018-12-17 17:12:37 -08:00
|
|
|
hash_id = hashit(f'{self.server.id}{series_title}{sxe}')
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload.append(
|
|
|
|
{
|
|
|
|
"measurement": "Sonarr",
|
|
|
|
"tags": {
|
|
|
|
"type": "Queue",
|
|
|
|
"sonarrId": sonarr_id,
|
|
|
|
"server": self.server.id,
|
|
|
|
"name": series_title,
|
|
|
|
"epname": episode_title,
|
|
|
|
"sxe": sxe,
|
|
|
|
"protocol": protocol,
|
2018-12-18 23:52:13 -08:00
|
|
|
"protocol_id": protocol_id,
|
|
|
|
"quality": quality
|
2018-12-04 08:45:18 -08:00
|
|
|
},
|
2018-12-10 22:01:24 -08:00
|
|
|
"time": now,
|
2018-12-04 08:45:18 -08:00
|
|
|
"fields": {
|
|
|
|
"hash": hash_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-04-19 10:54:16 -07:00
|
|
|
if influx_payload:
|
|
|
|
self.dbmanager.write_points(influx_payload)
|
|
|
|
else:
|
|
|
|
self.logger.debug("No data to send to influx for sonarr instance, discarding.")
|