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/letterboxd.py

82 lines
3.7 KiB

import logging, time
from modules import util
from modules.util import Failed
logger = logging.getLogger("Plex Meta Manager")
builders = ["letterboxd_list", "letterboxd_list_details"]
base_url = "https://letterboxd.com"
4 years ago
class Letterboxd:
def __init__(self, config):
self.config = config
4 years ago
def _parse_list(self, list_url, language):
response = self.config.get_html(list_url, headers=util.header(language))
letterboxd_ids = response.xpath("//li[contains(@class, 'poster-container')]/div/@data-film-id")
items = []
for letterboxd_id in letterboxd_ids:
slugs = response.xpath(f"//div[@data-film-id='{letterboxd_id}']/@data-film-slug")
items.append((letterboxd_id, slugs[0]))
next_url = response.xpath("//a[@class='next']/@href")
if len(next_url) > 0:
time.sleep(2)
items.extend(self._parse_list(f"{base_url}{next_url[0]}", language))
return items
4 years ago
def _tmdb(self, letterboxd_url, language):
response = self.config.get_html(letterboxd_url, headers=util.header(language))
ids = response.xpath("//a[@data-track-action='TMDb']/@href")
4 years ago
if len(ids) > 0 and ids[0]:
if "themoviedb.org/movie" in ids[0]:
return util.regex_first_int(ids[0], "TMDB Movie ID")
raise Failed(f"Letterboxd Error: TMDb Movie ID not found in {ids[0]}")
4 years ago
raise Failed(f"Letterboxd Error: TMDb Movie ID not found at {letterboxd_url}")
4 years ago
def get_list_description(self, list_url, language):
response = self.config.get_html(list_url, headers=util.header(language))
descriptions = response.xpath("//meta[@property='og:description']/@content")
4 years ago
return descriptions[0] if len(descriptions) > 0 and len(descriptions[0]) > 0 else None
3 years ago
def validate_letterboxd_lists(self, letterboxd_lists, language):
valid_lists = []
for letterboxd_list in util.get_list(letterboxd_lists, split=False):
list_url = letterboxd_list.strip()
if not list_url.startswith(base_url):
raise Failed(f"Letterboxd Error: {list_url} must begin with: {base_url}")
3 years ago
elif len(self._parse_list(list_url, language)) > 0:
3 years ago
valid_lists.append(list_url)
3 years ago
else:
raise Failed(f"Letterboxd Error: {list_url} failed to parse")
3 years ago
return valid_lists
4 years ago
def get_items(self, method, data, language):
pretty = util.pretty_names[method] if method in util.pretty_names else method
movie_ids = []
4 years ago
logger.info(f"Processing {pretty}: {data}")
4 years ago
items = self._parse_list(data, language)
total_items = len(items)
4 years ago
if total_items > 0:
for i, item in enumerate(items, 1):
letterboxd_id, slug = item
util.print_return(f"Finding TMDb ID {i}/{total_items}")
4 years ago
tmdb_id = None
expired = None
4 years ago
if self.config.Cache:
4 years ago
tmdb_id, expired = self.config.Cache.query_letterboxd_map(letterboxd_id)
if not tmdb_id or expired is not False:
try:
tmdb_id = self._tmdb(f"{base_url}{slug}", language)
4 years ago
except Failed as e:
logger.error(e)
continue
if self.config.Cache:
self.config.Cache.update_letterboxd_map(expired, letterboxd_id, tmdb_id)
movie_ids.append(tmdb_id)
logger.info(util.adjust_space(f"Processed {total_items} TMDb IDs"))
4 years ago
else:
logger.error(f"Letterboxd Error: No List Items found in {data}")
logger.debug("")
4 years ago
logger.debug(f"{len(movie_ids)} TMDb IDs Found: {movie_ids}")
return movie_ids, []