You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Plex-Meta-Manager/modules/tautulli.py

63 lines
2.8 KiB

4 years ago
from modules import util
from modules.util import Failed
from plexapi.exceptions import BadRequest, NotFound
from plexapi.video import Movie, Show
4 years ago
logger = util.logger
4 years ago
builders = ["tautulli_popular", "tautulli_watched"]
3 years ago
class Tautulli:
def __init__(self, config, library, params):
self.config = config
self.library = library
4 years ago
self.url = params["url"]
self.apikey = params["apikey"]
logger.secret(self.url)
3 years ago
logger.secret(self.apikey)
4 years ago
try:
4 years ago
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_library_names")
4 years ago
except Exception:
logger.stacktrace()
4 years ago
raise Failed("Tautulli Error: Invalid url")
if response["response"]["result"] != "success":
raise Failed(f"Tautulli Error: {response['response']['message']}")
4 years ago
def get_rating_keys(self, params, all_items):
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 self.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 self.library.is_movie else 'tv'}"
3 years ago
stat_type = "users_watched" if params['list_type'] == 'popular' else "total_plays"
4 years ago
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")
section_id = self.library.Plex.key
4 years ago
rating_keys = []
for item in items:
3 years ago
if (all_items or item["section_id"] == section_id) and len(rating_keys) < int(params['list_size']):
if int(item[stat_type]) < params['list_minimum']:
continue
try:
plex_item = self.library.fetch_item(int(item["rating_key"]))
if not isinstance(plex_item, (Movie, Show)):
raise BadRequest
3 years ago
rating_keys.append((item["rating_key"], "ratingKey"))
except Failed as e:
new_item = self.library.exact_search(item["title"], year=item["year"])
if new_item:
3 years ago
rating_keys.append((new_item[0].ratingKey, "ratingKey"))
else:
logger.error(e)
4 years ago
return rating_keys
4 years ago
def _request(self, url):
logger.trace(f"Tautulli URL: {url}")
return self.config.get_json(url)