Plex-Meta-Manager/modules/letterboxd.py

70 lines
3.1 KiB
Python
Raw Normal View History

2021-07-14 14:47:20 +00:00
import logging, time
2021-03-04 20:05:51 +00:00
from modules import util
from modules.util import Failed
logger = logging.getLogger("Plex Meta Manager")
2021-03-30 05:50:53 +00:00
builders = ["letterboxd_list", "letterboxd_list_details"]
2021-07-14 14:47:20 +00:00
base_url = "https://letterboxd.com"
2021-03-30 05:50:53 +00:00
2021-06-14 15:24:11 +00:00
class Letterboxd:
2021-03-12 23:16:26 +00:00
def __init__(self, config):
self.config = config
2021-03-08 21:54:19 +00:00
2021-05-07 19:53:54 +00:00
def _parse_list(self, list_url, language):
2021-07-14 14:47:20 +00:00
response = self.config.get_html(list_url, headers=util.header(language))
2021-07-19 20:12:29 +00:00
letterboxd_ids = response.xpath("//li[@class='poster-container']/div/@data-film-id")
2021-03-12 19:49:23 +00:00
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]))
2021-03-04 20:05:51 +00:00
next_url = response.xpath("//a[@class='next']/@href")
if len(next_url) > 0:
2021-07-14 14:47:20 +00:00
time.sleep(2)
items.extend(self._parse_list(f"{base_url}{next_url[0]}", language))
2021-03-12 19:49:23 +00:00
return items
2021-03-04 20:05:51 +00:00
2021-05-07 19:53:54 +00:00
def _tmdb(self, letterboxd_url, language):
2021-07-14 14:47:20 +00:00
response = self.config.get_html(letterboxd_url, headers=util.header(language))
2021-03-12 19:49:23 +00:00
ids = response.xpath("//a[@data-track-action='TMDb']/@href")
2021-03-11 21:51:02 +00:00
if len(ids) > 0 and ids[0]:
2021-03-12 19:49:23 +00:00
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]}")
2021-03-11 21:53:43 +00:00
raise Failed(f"Letterboxd Error: TMDb Movie ID not found at {letterboxd_url}")
2021-03-04 20:05:51 +00:00
2021-05-07 19:53:54 +00:00
def get_list_description(self, list_url, language):
2021-07-14 14:47:20 +00:00
response = self.config.get_html(list_url, headers=util.header(language))
descriptions = response.xpath("//meta[@property='og:description']/@content")
2021-05-07 19:53:54 +00:00
return descriptions[0] if len(descriptions) > 0 and len(descriptions[0]) > 0 else None
2021-05-09 05:38:41 +00:00
def get_items(self, method, data, language):
2021-03-04 20:05:51 +00:00
pretty = util.pretty_names[method] if method in util.pretty_names else method
movie_ids = []
2021-05-09 05:38:41 +00:00
logger.info(f"Processing {pretty}: {data}")
2021-05-07 19:53:54 +00:00
items = self._parse_list(data, language)
2021-03-12 19:49:23 +00:00
total_items = len(items)
2021-05-09 05:38:41 +00:00
if total_items > 0:
for i, item in enumerate(items, 1):
letterboxd_id, slug = item
2021-05-26 13:25:32 +00:00
util.print_return(f"Finding TMDb ID {i}/{total_items}")
2021-05-09 05:38:41 +00:00
tmdb_id = None
expired = None
2021-03-13 01:54:53 +00:00
if self.config.Cache:
2021-05-09 05:38:41 +00:00
tmdb_id, expired = self.config.Cache.query_letterboxd_map(letterboxd_id)
if not tmdb_id or expired is not False:
try:
2021-07-14 14:47:20 +00:00
tmdb_id = self._tmdb(f"{base_url}{slug}", language)
2021-05-09 05:38:41 +00:00
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)
2021-05-26 13:25:32 +00:00
logger.info(util.adjust_space(f"Processed {total_items} TMDb IDs"))
2021-05-09 05:38:41 +00:00
else:
logger.error(f"Letterboxd Error: No List Items found in {data}")
2021-05-24 03:38:46 +00:00
logger.debug("")
2021-07-03 01:47:09 +00:00
logger.debug(f"{len(movie_ids)} TMDb IDs Found: {movie_ids}")
2021-03-04 20:05:51 +00:00
return movie_ids, []