[41] update tautulli builders to only look at a single library

This commit is contained in:
meisnate12 2023-04-27 23:01:33 -04:00
parent 07c7e58c37
commit 0fbd026a3c
4 changed files with 36 additions and 18 deletions

View file

@ -1 +1 @@
1.19.0-develop40 1.19.0-develop41

View file

@ -134,6 +134,8 @@ class AniDB:
def _request(self, url, params=None, data=None): def _request(self, url, params=None, data=None):
logger.trace(f"URL: {url}") logger.trace(f"URL: {url}")
if params:
logger.trace(f"Params: {params}")
if data: if data:
return self.config.post_html(url, data=data, headers=util.header(self.language)) return self.config.post_html(url, data=data, headers=util.header(self.language))
else: else:

View file

@ -1664,13 +1664,19 @@ class CollectionBuilder:
def _tautulli(self, method_name, method_data): def _tautulli(self, method_name, method_data):
for dict_data in util.parse(self.Type, method_name, method_data, datatype="listdict"): for dict_data in util.parse(self.Type, method_name, method_data, datatype="listdict"):
dict_methods = {dm.lower(): dm for dm in dict_data} dict_methods = {dm.lower(): dm for dm in dict_data}
self.builders.append((method_name, { final_dict = {
"list_type": "popular" if method_name == "tautulli_popular" else "watched", "list_type": "popular" if method_name == "tautulli_popular" else "watched",
"list_days": util.parse(self.Type, "list_days", dict_data, datatype="int", methods=dict_methods, default=30, parent=method_name), "list_days": util.parse(self.Type, "list_days", dict_data, datatype="int", methods=dict_methods, default=30, parent=method_name),
"list_size": util.parse(self.Type, "list_size", dict_data, datatype="int", methods=dict_methods, default=10, parent=method_name), "list_size": util.parse(self.Type, "list_size", dict_data, datatype="int", methods=dict_methods, default=10, parent=method_name),
"list_buffer": util.parse(self.Type, "list_buffer", dict_data, datatype="int", methods=dict_methods, default=20, parent=method_name),
"list_minimum": util.parse(self.Type, "list_minimum", dict_data, datatype="int", methods=dict_methods, default=0, parent=method_name) "list_minimum": util.parse(self.Type, "list_minimum", dict_data, datatype="int", methods=dict_methods, default=0, parent=method_name)
})) }
if self.library.Tautulli.has_section:
final_dict["list_buffer"] = 0
elif "list_buffer" in dict_methods:
final_dict["list_buffer"] = util.parse(self.Type, "list_buffer", dict_data, datatype="int", methods=dict_methods, default=20, parent=method_name)
else:
final_dict["list_buffer"] = final_dict["list_size"] * 3
self.builders.append((method_name, final_dict))
def _tmdb(self, method_name, method_data): def _tmdb(self, method_name, method_data):
if method_name == "tmdb_discover": if method_name == "tmdb_discover":

View file

@ -1,6 +1,6 @@
from modules import util from modules import util
from modules.util import Failed from modules.util import Failed
from plexapi.exceptions import BadRequest, NotFound from plexapi.exceptions import BadRequest
from plexapi.video import Movie, Show from plexapi.video import Movie, Show
logger = util.logger logger = util.logger
@ -13,22 +13,27 @@ class Tautulli:
self.library = library self.library = library
self.url = params["url"] self.url = params["url"]
self.apikey = params["apikey"] self.apikey = params["apikey"]
self.api = f"{self.url}/api/v2"
logger.secret(self.url) logger.secret(self.url)
logger.secret(self.apikey) logger.secret(self.apikey)
try: try:
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_library_names") response = self._request("get_tautulli_info")
except Exception: except Exception:
logger.stacktrace() logger.stacktrace()
raise Failed("Tautulli Error: Invalid url") raise Failed("Tautulli Error: Invalid url")
if response["response"]["result"] != "success": if response["response"]["result"] != "success":
raise Failed(f"Tautulli Error: {response['response']['message']}") raise Failed(f"Tautulli Error: {response['response']['message']}")
self.has_section = True if int(response["response"]["data"]["tautulli_version"].split(".")[1]) > 11 else False
self.section_id = self.library.Plex.key
def get_rating_keys(self, params, all_items): def get_rating_keys(self, data, all_items):
query_size = int(params["list_size"]) + int(params["list_buffer"]) logger.info(f"Processing Tautulli Most {data['list_type'].capitalize()}: {data['list_size']} {'Movies' if self.library.is_movie else 'Shows'}")
logger.info(f"Processing Tautulli Most {params['list_type'].capitalize()}: {params['list_size']} {'Movies' if self.library.is_movie else 'Shows'}") params = {"time_range": data["list_days"], "stats_count": int(data["list_size"]) + int(data["list_buffer"])}
response = self._request(f"{self.url}/api/v2?apikey={self.apikey}&cmd=get_home_stats&time_range={params['list_days']}&stats_count={query_size}") if self.has_section and not all_items:
stat_id = f"{'popular' if params['list_type'] == 'popular' else 'top'}_{'movies' if self.library.is_movie else 'tv'}" params["section_id"] = self.section_id
stat_type = "users_watched" if params['list_type'] == 'popular' else "total_plays" response = self._request("get_home_stats", params=params)
stat_id = f"{'popular' if data['list_type'] == 'popular' else 'top'}_{'movies' if self.library.is_movie else 'tv'}"
stat_type = "users_watched" if data['list_type'] == 'popular' else "total_plays"
items = None items = None
for entry in response["response"]["data"]: for entry in response["response"]["data"]:
@ -38,11 +43,10 @@ class Tautulli:
if items is None: if items is None:
raise Failed("Tautulli Error: No Items found in the response") raise Failed("Tautulli Error: No Items found in the response")
section_id = self.library.Plex.key
rating_keys = [] rating_keys = []
for item in items: for item in items:
if (all_items or item["section_id"] == section_id) and len(rating_keys) < int(params['list_size']): if (all_items or item["section_id"] == self.section_id) and len(rating_keys) < int(data['list_size']):
if int(item[stat_type]) < params['list_minimum']: if int(item[stat_type]) < data['list_minimum']:
continue continue
try: try:
plex_item = self.library.fetch_item(int(item["rating_key"])) plex_item = self.library.fetch_item(int(item["rating_key"]))
@ -57,6 +61,12 @@ class Tautulli:
logger.error(e) logger.error(e)
return rating_keys return rating_keys
def _request(self, url): def _request(self, cmd, params=None):
logger.trace(f"Tautulli URL: {url}") logger.trace(f"Tautulli CMD: {cmd}")
return self.config.get_json(url) if params:
logger.trace(f"Tautulli Params: {params}")
final_params = {"apikey": self.apikey, "cmd": cmd}
if params:
for k, v in params.items():
final_params[k] = v
return self.config.get_json(self.api, params=final_params)