sonarr refactor
This commit is contained in:
parent
f943968087
commit
73ea3d5176
2 changed files with 21 additions and 70 deletions
16
Varken.py
16
Varken.py
|
@ -29,12 +29,8 @@ from varken.varkenlogger import VarkenLogger
|
||||||
PLATFORM_LINUX_DISTRO = ' '.join(x for x in linux_distribution() if x)
|
PLATFORM_LINUX_DISTRO = ' '.join(x for x in linux_distribution() if x)
|
||||||
|
|
||||||
|
|
||||||
def thread(job):
|
def thread(job, **kwargs):
|
||||||
worker = Thread(target=job)
|
worker = Thread(target=job, kwargs=dict(**kwargs))
|
||||||
if isinstance(job, tuple):
|
|
||||||
job, query = job[0], job[1]
|
|
||||||
worker = Thread(target=job, kwargs={'query': query})
|
|
||||||
|
|
||||||
worker.start()
|
worker.start()
|
||||||
|
|
||||||
|
|
||||||
|
@ -108,10 +104,10 @@ if __name__ == "__main__":
|
||||||
at_time.do(thread, SONARR.get_queue).tag("sonarr-{}-get_queue".format(server.id))
|
at_time.do(thread, SONARR.get_queue).tag("sonarr-{}-get_queue".format(server.id))
|
||||||
if server.missing_days > 0:
|
if server.missing_days > 0:
|
||||||
at_time = schedule.every(server.missing_days_run_seconds).seconds
|
at_time = schedule.every(server.missing_days_run_seconds).seconds
|
||||||
at_time.do(thread, SONARR.get_missing).tag("sonarr-{}-get_missing".format(server.id))
|
at_time.do(thread, SONARR.get_calendar, query="Missing").tag("sonarr-{}-get_missing".format(server.id))
|
||||||
if server.future_days > 0:
|
if server.future_days > 0:
|
||||||
at_time = schedule.every(server.future_days_run_seconds).seconds
|
at_time = schedule.every(server.future_days_run_seconds).seconds
|
||||||
at_time.do(thread, SONARR.get_future).tag("sonarr-{}-get_future".format(server.id))
|
at_time.do(thread, SONARR.get_calendar, query="Future").tag("sonarr-{}-get_future".format(server.id))
|
||||||
|
|
||||||
if CONFIG.tautulli_enabled:
|
if CONFIG.tautulli_enabled:
|
||||||
GEOIPHANDLER = GeoIPHandler(DATA_FOLDER)
|
GEOIPHANDLER = GeoIPHandler(DATA_FOLDER)
|
||||||
|
@ -143,11 +139,11 @@ if __name__ == "__main__":
|
||||||
at_time.do(thread, LIDARR.get_queue).tag("lidarr-{}-get_queue".format(server.id))
|
at_time.do(thread, LIDARR.get_queue).tag("lidarr-{}-get_queue".format(server.id))
|
||||||
if server.missing_days > 0:
|
if server.missing_days > 0:
|
||||||
at_time = schedule.every(server.missing_days_run_seconds).seconds
|
at_time = schedule.every(server.missing_days_run_seconds).seconds
|
||||||
at_time.do(thread, (LIDARR.get_calendar, "Missing")).tag(
|
at_time.do(thread, LIDARR.get_calendar, query="Missing").tag(
|
||||||
"lidarr-{}-get_missing".format(server.id))
|
"lidarr-{}-get_missing".format(server.id))
|
||||||
if server.future_days > 0:
|
if server.future_days > 0:
|
||||||
at_time = schedule.every(server.future_days_run_seconds).seconds
|
at_time = schedule.every(server.future_days_run_seconds).seconds
|
||||||
at_time.do(thread, (LIDARR.get_calendar, "Future")).tag("lidarr-{}-get_future".format(
|
at_time.do(thread, LIDARR.get_calendar, query="Future").tag("lidarr-{}-get_future".format(
|
||||||
server.id))
|
server.id))
|
||||||
|
|
||||||
if CONFIG.ombi_enabled:
|
if CONFIG.ombi_enabled:
|
||||||
|
|
|
@ -19,68 +19,19 @@ class SonarrAPI(object):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<sonarr-{self.server.id}>"
|
return f"<sonarr-{self.server.id}>"
|
||||||
|
|
||||||
def get_missing(self):
|
def get_calendar(self, query="Missing"):
|
||||||
endpoint = '/api/calendar'
|
|
||||||
today = str(date.today())
|
|
||||||
last_days = str(date.today() + timedelta(days=-self.server.missing_days))
|
|
||||||
now = datetime.now(timezone.utc).astimezone().isoformat()
|
|
||||||
params = {'start': last_days, 'end': today}
|
|
||||||
influx_payload = []
|
|
||||||
missing = []
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# Iteratively create a list of SonarrTVShow Objects from response json
|
|
||||||
tv_shows = []
|
|
||||||
for show in get:
|
|
||||||
try:
|
|
||||||
tv_shows.append(SonarrTVShow(**show))
|
|
||||||
except TypeError as e:
|
|
||||||
self.logger.error('TypeError has occurred : %s while creating SonarrTVShow structure for show. Data '
|
|
||||||
'attempted is: %s', e, show)
|
|
||||||
|
|
||||||
# Add show to missing list if file does not exist
|
|
||||||
for show in tv_shows:
|
|
||||||
if not show.hasFile:
|
|
||||||
sxe = f'S{show.seasonNumber:0>2}E{show.episodeNumber:0>2}'
|
|
||||||
missing.append((show.series['title'], sxe, show.airDateUtc, show.title, show.id))
|
|
||||||
|
|
||||||
for series_title, sxe, air_date_utc, episode_title, sonarr_id in missing:
|
|
||||||
hash_id = hashit(f'{self.server.id}{series_title}{sxe}')
|
|
||||||
influx_payload.append(
|
|
||||||
{
|
|
||||||
"measurement": "Sonarr",
|
|
||||||
"tags": {
|
|
||||||
"type": "Missing",
|
|
||||||
"sonarrId": sonarr_id,
|
|
||||||
"server": self.server.id,
|
|
||||||
"name": series_title,
|
|
||||||
"epname": episode_title,
|
|
||||||
"sxe": sxe,
|
|
||||||
"airsUTC": air_date_utc
|
|
||||||
},
|
|
||||||
"time": now,
|
|
||||||
"fields": {
|
|
||||||
"hash": hash_id
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
self.dbmanager.write_points(influx_payload)
|
|
||||||
|
|
||||||
def get_future(self):
|
|
||||||
endpoint = '/api/calendar/'
|
endpoint = '/api/calendar/'
|
||||||
today = str(date.today())
|
today = str(date.today())
|
||||||
now = datetime.now(timezone.utc).astimezone().isoformat()
|
last_days = str(date.today() - timedelta(days=self.server.missing_days))
|
||||||
future = str(date.today() + timedelta(days=self.server.future_days))
|
future = str(date.today() + timedelta(days=self.server.future_days))
|
||||||
|
now = datetime.now(timezone.utc).astimezone().isoformat()
|
||||||
|
if query == "Missing":
|
||||||
|
params = {'start': last_days, 'end': today}
|
||||||
|
else:
|
||||||
|
params = {'start': today, 'end': future}
|
||||||
influx_payload = []
|
influx_payload = []
|
||||||
air_days = []
|
air_days = []
|
||||||
params = {'start': today, 'end': future}
|
missing = []
|
||||||
|
|
||||||
req = self.session.prepare_request(Request('GET', self.server.url + endpoint, params=params))
|
req = self.session.prepare_request(Request('GET', self.server.url + endpoint, params=params))
|
||||||
get = connection_handler(self.session, req, self.server.verify_ssl)
|
get = connection_handler(self.session, req, self.server.verify_ssl)
|
||||||
|
@ -102,15 +53,19 @@ class SonarrAPI(object):
|
||||||
downloaded = 1
|
downloaded = 1
|
||||||
else:
|
else:
|
||||||
downloaded = 0
|
downloaded = 0
|
||||||
|
if query == "Missing":
|
||||||
|
if not downloaded:
|
||||||
|
missing.append((show.series['title'], downloaded, sxe, show.airDateUtc, show.title, show.id))
|
||||||
|
else:
|
||||||
air_days.append((show.series['title'], downloaded, sxe, show.title, show.airDateUtc, show.id))
|
air_days.append((show.series['title'], downloaded, sxe, show.title, show.airDateUtc, show.id))
|
||||||
|
|
||||||
for series_title, dl_status, sxe, episode_title, air_date_utc, sonarr_id in air_days:
|
for series_title, dl_status, sxe, episode_title, air_date_utc, sonarr_id in (air_days or missing):
|
||||||
hash_id = hashit(f'{self.server.id}{series_title}{sxe}')
|
hash_id = hashit(f'{self.server.id}{series_title}{sxe}')
|
||||||
influx_payload.append(
|
influx_payload.append(
|
||||||
{
|
{
|
||||||
"measurement": "Sonarr",
|
"measurement": "Sonarr",
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "Future",
|
"type": query,
|
||||||
"sonarrId": sonarr_id,
|
"sonarrId": sonarr_id,
|
||||||
"server": self.server.id,
|
"server": self.server.id,
|
||||||
"name": series_title,
|
"name": series_title,
|
||||||
|
|
Loading…
Reference in a new issue