Created
June 15, 2020 20:07
-
-
Save jolbol1/b58afcd63edf7294985346c7f9ca857a to your computer and use it in GitHub Desktop.
Plex-Auto-Collections IMDB List fix for 100 movie limit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import requests | |
from lxml import html | |
from tmdbv3api import TMDb | |
from tmdbv3api import Movie | |
from tmdbv3api import Collection | |
from tmdbv3api import Person | |
import config_tools | |
def imdb_get_movies(config_path, plex, data): | |
tmdb = TMDb() | |
movie = Movie() | |
tmdb.api_key = config_tools.TMDB(config_path).apikey | |
imdb_url = data | |
if imdb_url[-1:] == " ": | |
imdb_url = imdb_url[:-1] | |
imdb_map = {} | |
library_language = plex.Library.language | |
title_ids = [] | |
for i in range(1,10): | |
try: | |
r = requests.get(imdb_url + '?page={}'.format(i), headers={'Accept-Language': library_language}) | |
except requests.exceptions.MissingSchema: | |
return | |
tree = html.fromstring(r.content) | |
title_ids.extend(tree.xpath("//div[contains(@class, 'lister-item-image')]" | |
"//a/img//@data-tconst")) | |
if title_ids: | |
for m in plex.Library.all(): | |
if 'themoviedb://' in m.guid: | |
if not tmdb.api_key == "None": | |
tmdb_id = m.guid.split('themoviedb://')[1].split('?')[0] | |
tmdbapi = movie.details(tmdb_id) | |
imdb_id = tmdbapi.imdb_id | |
else: | |
imdb_id = None | |
elif 'imdb://' in m.guid: | |
imdb_id = m.guid.split('imdb://')[1].split('?')[0] | |
else: | |
imdb_id = None | |
if imdb_id and imdb_id in title_ids: | |
imdb_map[imdb_id] = m | |
else: | |
imdb_map[m.ratingKey] = m | |
matched_imbd_movies = [] | |
missing_imdb_movies = [] | |
for imdb_id in title_ids: | |
movie = imdb_map.pop(imdb_id, None) | |
if movie: | |
matched_imbd_movies.append(plex.Server.fetchItem(movie.ratingKey)) | |
else: | |
missing_imdb_movies.append(imdb_id) | |
return matched_imbd_movies, missing_imdb_movies | |
def tmdb_get_movies(config_path, plex, data): | |
try: | |
tmdb_id = re.search('.*?(\\d+)', data) | |
tmdb_id = tmdb_id.group(1) | |
except AttributeError: # Bad URL Provided | |
return | |
t_movie = Movie() | |
tmdb = Collection() | |
tmdb.api_key = config_tools.TMDB(config_path).apikey # Set TMDb api key for Collection | |
if tmdb.api_key == "None": | |
raise KeyError("Invalid TMDb API Key") | |
t_movie.api_key = tmdb.api_key # Copy same api key to Movie | |
t_col = tmdb.details(tmdb_id) | |
t_movs = [] | |
for tmovie in t_col.parts: | |
t_movs.append(tmovie['id']) | |
# Create dictionary of movies and their guid | |
# GUIDs reference from which source Plex has pulled the metadata | |
p_m_map = {} | |
p_movies = plex.Library.all() | |
for m in p_movies: | |
guid = m.guid | |
if "themoviedb://" in guid: | |
guid = guid.split('themoviedb://')[1].split('?')[0] | |
elif "imdb://" in guid: | |
guid = guid.split('imdb://')[1].split('?')[0] | |
else: | |
guid = "None" | |
p_m_map[m] = guid | |
matched = [] | |
missing = [] | |
# We want to search for a match first to limit TMDb API calls | |
# Too many rapid calls can cause a momentary block | |
# If needed in future maybe add a delay after x calls to let the limit reset | |
for mid in t_movs: # For each TMBd ID in TMBd Collection | |
match = False | |
for m in p_m_map: # For each movie in Plex | |
if "tt" not in p_m_map[m] is not "None": # If the Plex movie's guid does not start with tt | |
if int(p_m_map[m]) == int(mid): | |
match = True | |
break | |
if not match: | |
imdb_id = t_movie.details(mid).entries['imdb_id'] | |
for m in p_m_map: | |
if "tt" in p_m_map[m]: | |
if p_m_map[m] == imdb_id: | |
match = True | |
break | |
if match: | |
matched.append(m) | |
else: | |
missing.append(t_movie.details(mid).entries['imdb_id']) | |
return matched, missing | |
def tmdb_get_summary(config_path, data, type): | |
collection = Collection() | |
person = Person() | |
collection.api_key = config_tools.TMDB(config_path).apikey | |
person.api_key = collection.api_key | |
collection.language = config_tools.TMDB(config_path).language | |
person.language = collection.language | |
if type == "overview": | |
return collection.details(data).overview | |
elif type == "biography": | |
return person.details(data).biography |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment