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
|
|
|
|
|
2018-12-29 22:43:10 -08:00
|
|
|
from varken.structures import RadarrMovie, Queue
|
2018-12-17 17:12:37 -08:00
|
|
|
from varken.helpers import hashit, connection_handler
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
|
|
|
|
class RadarrAPI(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}
|
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"<radarr-{self.server.id}>"
|
2018-12-04 19:17:33 -08:00
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
def get_missing(self):
|
|
|
|
endpoint = '/api/movie'
|
2018-12-10 22:01:24 -08:00
|
|
|
now = datetime.now(timezone.utc).astimezone().isoformat()
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload = []
|
|
|
|
missing = []
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-06 09:39:51 -08:00
|
|
|
try:
|
2018-12-29 22:43:10 -08:00
|
|
|
movies = [RadarrMovie(**movie) for movie in get]
|
2018-12-06 09:39:51 -08:00
|
|
|
except TypeError as e:
|
2018-12-29 22:43:10 -08:00
|
|
|
self.logger.error('TypeError has occurred : %s while creating RadarrMovie structure', e)
|
2018-12-06 09:39:51 -08:00
|
|
|
return
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
for movie in movies:
|
2019-06-24 10:57:02 -07:00
|
|
|
if movie.monitored and not movie.downloaded:
|
2018-12-04 19:17:33 -08:00
|
|
|
if movie.isAvailable:
|
2018-12-10 17:55:45 -08:00
|
|
|
ma = 0
|
2018-12-04 08:45:18 -08:00
|
|
|
else:
|
2018-12-10 17:55:45 -08:00
|
|
|
ma = 1
|
2018-12-10 15:06:43 -08:00
|
|
|
|
2018-12-17 17:12:37 -08:00
|
|
|
movie_name = f'{movie.title} ({movie.year})'
|
2018-12-10 16:49:22 -08:00
|
|
|
missing.append((movie_name, ma, movie.tmdbId, movie.titleSlug))
|
2018-12-10 15:06:43 -08:00
|
|
|
|
|
|
|
for title, ma, mid, title_slug in missing:
|
2018-12-17 17:12:37 -08:00
|
|
|
hash_id = hashit(f'{self.server.id}{title}{mid}')
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload.append(
|
|
|
|
{
|
|
|
|
"measurement": "Radarr",
|
|
|
|
"tags": {
|
|
|
|
"Missing": True,
|
|
|
|
"Missing_Available": ma,
|
|
|
|
"tmdbId": mid,
|
|
|
|
"server": self.server.id,
|
2018-12-10 15:06:43 -08:00
|
|
|
"name": title,
|
|
|
|
"titleSlug": title_slug
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.dbmanager.write_points(influx_payload)
|
|
|
|
|
|
|
|
def get_queue(self):
|
|
|
|
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
|
|
|
influx_payload = []
|
|
|
|
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
|
|
|
|
|
|
|
|
for movie in get:
|
2018-12-06 09:39:51 -08:00
|
|
|
try:
|
2018-12-29 22:43:10 -08:00
|
|
|
movie['movie'] = RadarrMovie(**movie['movie'])
|
2018-12-06 09:39:51 -08:00
|
|
|
except TypeError as e:
|
2018-12-29 22:43:10 -08:00
|
|
|
self.logger.error('TypeError has occurred : %s while creating RadarrMovie structure', e)
|
2018-12-06 09:39:51 -08:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
download_queue = [Queue(**movie) for movie in get]
|
|
|
|
except TypeError as e:
|
2018-12-06 10:04:02 -08:00
|
|
|
self.logger.error('TypeError has occurred : %s while creating Queue structure', e)
|
2018-12-06 09:39:51 -08:00
|
|
|
return
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
for queue_item in download_queue:
|
2018-12-06 10:04:02 -08:00
|
|
|
movie = queue_item.movie
|
2018-12-10 15:06:43 -08:00
|
|
|
|
2018-12-17 17:12:37 -08:00
|
|
|
name = f'{movie.title} ({movie.year})'
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
if queue_item.protocol.upper() == 'USENET':
|
|
|
|
protocol_id = 1
|
|
|
|
else:
|
|
|
|
protocol_id = 0
|
|
|
|
|
|
|
|
queue.append((name, queue_item.quality['quality']['name'], queue_item.protocol.upper(),
|
2018-12-10 16:49:22 -08:00
|
|
|
protocol_id, queue_item.id, movie.titleSlug))
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-10 15:06:43 -08:00
|
|
|
for name, quality, protocol, protocol_id, qid, title_slug in queue:
|
2018-12-17 17:12:37 -08:00
|
|
|
hash_id = hashit(f'{self.server.id}{name}{quality}')
|
2018-12-04 08:45:18 -08:00
|
|
|
influx_payload.append(
|
|
|
|
{
|
|
|
|
"measurement": "Radarr",
|
|
|
|
"tags": {
|
|
|
|
"type": "Queue",
|
|
|
|
"tmdbId": qid,
|
|
|
|
"server": self.server.id,
|
2018-12-06 10:04:02 -08:00
|
|
|
"name": name,
|
2018-12-04 08:45:18 -08:00
|
|
|
"quality": quality,
|
|
|
|
"protocol": protocol,
|
2018-12-10 15:06:43 -08:00
|
|
|
"protocol_id": protocol_id,
|
|
|
|
"titleSlug": title_slug
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.dbmanager.write_points(influx_payload)
|