Plex-Meta-Manager/modules/tautulli.py

70 lines
3 KiB
Python
Raw Normal View History

2021-07-14 14:47:20 +00:00
import logging
2021-01-20 21:37:59 +00:00
from modules import util
from modules.util import Failed
2021-05-11 01:22:18 +00:00
from plexapi.exceptions import BadRequest, NotFound
2021-01-20 21:37:59 +00:00
logger = logging.getLogger("Plex Meta Manager")
2021-03-30 05:50:53 +00:00
builders = ["tautulli_popular", "tautulli_watched"]
2021-06-14 15:24:11 +00:00
class Tautulli:
2021-07-14 14:47:20 +00:00
def __init__(self, config, params):
self.config = config
2021-05-28 16:02:33 +00:00
self.url = params["url"]
self.apikey = params["apikey"]
2021-01-20 21:37:59 +00:00
try:
2021-05-28 16:02:33 +00:00
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_library_names")
2021-02-24 06:42:58 +00:00
except Exception:
2021-01-20 21:37:59 +00:00
util.print_stacktrace()
raise Failed("Tautulli Error: Invalid url")
if response["response"]["result"] != "success":
2021-02-24 06:44:06 +00:00
raise Failed(f"Tautulli Error: {response['response']['message']}")
2021-01-20 21:37:59 +00:00
2021-08-07 06:01:21 +00:00
def get_rating_keys(self, library, params):
2021-05-12 14:25:48 +00:00
query_size = int(params["list_size"]) + int(params["list_buffer"])
logger.info(f"Processing Tautulli Most {params['list_type'].capitalize()}: {params['list_size']} {'Movies' if library.is_movie else 'Shows'}")
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_home_stats&time_range={params['list_days']}&stats_count={query_size}")
stat_id = f"{'popular' if params['list_type'] == 'popular' else 'top'}_{'movies' if library.is_movie else 'tv'}"
2021-01-20 21:37:59 +00:00
items = None
for entry in response["response"]["data"]:
if entry["stat_id"] == stat_id:
items = entry["rows"]
break
if items is None:
raise Failed("Tautulli Error: No Items found in the response")
2021-05-07 19:53:54 +00:00
section_id = self._section_id(library.name)
2021-01-20 21:37:59 +00:00
rating_keys = []
count = 0
for item in items:
2021-05-12 14:25:48 +00:00
if item["section_id"] == section_id and count < int(params['list_size']):
2021-05-11 01:22:18 +00:00
try:
library.fetchItem(int(item["rating_key"]))
2021-05-12 14:25:48 +00:00
rating_keys.append(item["rating_key"])
2021-05-11 01:22:18 +00:00
except (BadRequest, NotFound):
2021-05-12 14:25:48 +00:00
new_item = library.exact_search(item["title"], year=item["year"])
2021-05-11 01:22:18 +00:00
if new_item:
2021-05-12 14:25:48 +00:00
rating_keys.append(new_item[0].ratingKey)
2021-05-11 01:22:18 +00:00
else:
logger.error(f"Plex Error: Item {item} not found")
continue
2021-01-20 21:37:59 +00:00
count += 1
2021-08-07 06:01:21 +00:00
logger.debug("")
logger.debug(f"{len(rating_keys)} Keys Found: {rating_keys}")
2021-01-20 21:37:59 +00:00
return rating_keys
2021-05-07 19:53:54 +00:00
def _section_id(self, library_name):
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_library_names")
2021-01-20 21:37:59 +00:00
section_id = None
for entry in response["response"]["data"]:
if entry["section_name"] == library_name:
section_id = entry["section_id"]
break
if section_id: return section_id
2021-02-24 06:44:06 +00:00
else: raise Failed(f"Tautulli Error: No Library named {library_name} in the response")
2021-01-20 21:37:59 +00:00
2021-05-07 19:53:54 +00:00
def _request(self, url):
2021-05-28 04:01:12 +00:00
logger.debug(f"Tautulli URL: {url.replace(self.apikey, '###############')}")
2021-07-14 14:47:20 +00:00
return self.config.get_json(url)