migrated sonarr.py to new style

This commit is contained in:
Nicholas St. Germain 2018-11-28 22:20:42 -06:00
parent 5f782cc1ad
commit 1fce9df22a

View file

@ -7,38 +7,22 @@ from influxdb import InfluxDBClient
from datetime import datetime, timezone, date, timedelta from datetime import datetime, timezone, date, timedelta
from Varken import configuration as config from Varken import configuration as config
from Varken.helpers import Server, TVShow, Queue from Varken.helpers import TVShow, Queue
class SonarrAPI(object): class SonarrAPI(object):
# Sets None as default for all TVShow NamedTuples, because sonarr's response json is inconsistent def __init__(self, servers):
TVShow.__new__.__defaults__ = (None,) * len(TVShow._fields)
def __init__(self):
# Set Time of initialization # Set Time of initialization
self.now = datetime.now(timezone.utc).astimezone().isoformat() self.now = datetime.now(timezone.utc).astimezone().isoformat()
self.today = str(date.today()) self.today = str(date.today())
self.influx = InfluxDBClient(config.influxdb_url, config.influxdb_port, config.influxdb_username, self.influx = InfluxDBClient(config.influxdb_url, config.influxdb_port, config.influxdb_username,
config.influxdb_password, config.sonarr_influxdb_db_name) config.influxdb_password, config.sonarr_influxdb_db_name)
self.influx_payload = [] self.influx_payload = []
self.servers = self.get_servers() self.servers = servers
# Create session to reduce server web thread load, and globally define pageSize for all requests # Create session to reduce server web thread load, and globally define pageSize for all requests
self.session = requests.Session() self.session = requests.Session()
self.session.params = {'pageSize': 1000} self.session.params = {'pageSize': 1000}
@staticmethod
def get_servers():
# Ensure sonarr servers have been defined
if not config.sonarr_server_list:
sys.exit("No Sonarr servers defined in config")
# Build Server Objects from config
servers = []
for url, api_key, server_id in config.sonarr_server_list:
servers.append(Server(url=url, api_key=api_key, id=server_id))
return servers
def get_missing(self, days_past): def get_missing(self, days_past):
endpoint = '/api/calendar' endpoint = '/api/calendar'
last_days = str(date.today() + timedelta(days=-days_past)) last_days = str(date.today() + timedelta(days=-days_past))
@ -77,39 +61,6 @@ class SonarrAPI(object):
} }
) )
def get_upcoming(self):
endpoint = '/api/calendar/'
for server in self.servers:
upcoming = []
headers = {'X-Api-Key': server.api_key}
get = self.session.get(server.url + endpoint, headers=headers).json()
tv_shows = [TVShow(**show) for show in get]
for show in tv_shows:
sxe = 'S{:0>2}E{:0>2}'.format(show.seasonNumber, show.episodeNumber)
upcoming.append((show.series['title'], sxe, show.id, show.title, show.airDate))
for series_title, sxe, sonarr_id, episode_title, air_date in upcoming:
self.influx_payload.append(
{
"measurement": "Sonarr",
"tags": {
"type": "Soon",
"sonarrId": sonarr_id,
"server": server.id
},
"time": self.now,
"fields": {
"name": series_title,
"epname": episode_title,
"sxe": sxe,
"airs": air_date
}
}
)
def get_future(self, future_days): def get_future(self, future_days):
endpoint = '/api/calendar/' endpoint = '/api/calendar/'
future = str(date.today() + timedelta(days=future_days)) future = str(date.today() + timedelta(days=future_days))
@ -204,7 +155,6 @@ if __name__ == "__main__":
help='legacy command. Deprecated in favor of --missing' help='legacy command. Deprecated in favor of --missing'
'\nfunctions identically to --missing' '\nfunctions identically to --missing'
'\nNote: Will be removed in a future release') '\nNote: Will be removed in a future release')
parser.add_argument("--upcoming", action='store_true', help='Get upcoming TV shows')
parser.add_argument("--future", metavar='$days', type=int, parser.add_argument("--future", metavar='$days', type=int,
help='Get TV shows on X days into the future. Includes today.' help='Get TV shows on X days into the future. Includes today.'
'\ni.e. --future 2 is Today and Tomorrow') '\ni.e. --future 2 is Today and Tomorrow')