Plex-Meta-Manager/modules/mdblist.py

61 lines
2.7 KiB
Python
Raw Normal View History

2022-01-15 22:40:59 +00:00
import logging
2022-01-24 09:16:45 +00:00
from modules import util
2022-01-15 22:40:59 +00:00
from modules.util import Failed
2022-01-25 07:45:31 +00:00
from urllib.parse import urlparse
2022-01-15 22:40:59 +00:00
logger = logging.getLogger("Plex Meta Manager")
builders = ["mdblist_list"]
base_url = "https://mdblist.com/lists"
2022-01-25 07:45:31 +00:00
headers = {"User-Agent": "Plex-Meta-Manager"}
2022-01-15 22:40:59 +00:00
class Mdblist:
def __init__(self, config):
self.config = config
2022-01-25 07:45:31 +00:00
def validate_mdblist_lists(self, mdb_lists):
2022-01-24 09:16:45 +00:00
valid_lists = []
for mdb_dict in util.get_list(mdb_lists, split=False):
if not isinstance(mdb_dict, dict):
mdb_dict = {"url": mdb_dict}
dict_methods = {dm.lower(): dm for dm in mdb_dict}
if "url" not in dict_methods:
raise Failed(f"Collection Error: mdb_list url attribute not found")
elif mdb_dict[dict_methods["url"]] is None:
raise Failed(f"Collection Error: mdb_list url attribute is blank")
else:
mdb_url = mdb_dict[dict_methods["url"]].strip()
if not mdb_url.startswith(base_url):
raise Failed(f"Mdblist Error: {mdb_url} must begin with: {base_url}")
list_count = None
if "limit" in dict_methods:
if mdb_dict[dict_methods["limit"]] is None:
logger.warning(f"Collection Warning: mdb_list limit attribute is blank using 0 as default")
else:
try:
value = int(str(mdb_dict[dict_methods["limit"]]))
if 0 <= value:
list_count = value
except ValueError:
pass
if list_count is None:
logger.warning(f"Collection Warning: mdb_list limit attribute must be an integer 0 or greater using 0 as default")
if list_count is None:
list_count = 0
valid_lists.append({"url": mdb_url, "limit": list_count})
return valid_lists
2022-01-15 22:40:59 +00:00
def get_mdblist_ids(self, method, data):
if method == "mdblist_list":
2022-01-24 09:16:45 +00:00
limit_status = f" Limit at: {data['limit']} items" if data['limit'] > 0 else ''
logger.info(f"Processing Mdblist.com List: {data['url']}{limit_status}")
2022-01-25 07:45:31 +00:00
parsed_url = urlparse(data["url"])
url_base = parsed_url._replace(query=None).geturl()
2022-02-02 09:47:34 +00:00
url_base = url_base if url_base.endswith("/") else f"{url_base}/"
url_base = url_base if url_base.endswith("json/") else f"{url_base}json/"
2022-01-25 07:45:31 +00:00
params = {"limit": data["limit"]} if data["limit"] > 0 else None
return [(i["imdb_id"], "imdb") for i in self.config.get_json(url_base, headers=headers, params=params)]
2022-01-15 22:40:59 +00:00
else:
raise Failed(f"Mdblist Error: Method {method} not supported")