2.6.1 Created a mapping for IMDb ID to Plex Rating Key once each run instead of every time tmdb, imdb, tvdb, or trakt list was run.

This commit is contained in:
meisnate12 2020-11-13 19:41:17 -05:00
parent 4a813bbee9
commit 967b2c8e7d
6 changed files with 172 additions and 259 deletions

View file

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [2.6.1] - 2020-11-14 - [#115](https://github.com/mza921/Plex-Auto-Collections/pull/115)
### Changed
- Created a mapping for IMDb ID to Plex Rating Key once each run instead of every time tmdb, imdb, tvdb, or trakt list was run.
## [2.6.0] - 2020-11-12 - [#113](https://github.com/mza921/Plex-Auto-Collections/pull/113)
### Added
- [#107](https://github.com/mza921/Plex-Auto-Collections/issues/107) - Added `plex_collection`

View file

@ -1,5 +1,5 @@
# Plex Auto Collections
##### Version 2.6.0
##### Version 2.6.1
Plex Auto Collections is a Python 3 script that works off a configuration file to create/update Plex collections. Collection management with this tool can be automated in a varying degree of customizability. Supports IMDB, TMDb, and Trakt lists as well as built in Plex Searches using actors, genres, year, studio and more.
![https://i.imgur.com/iHAYFIZ.png](https://i.imgur.com/iHAYFIZ.png)
@ -756,7 +756,7 @@ This script can pull items from a Trakt user's Watchlist for [Movies](https://tr
```yaml
collections:
Trakt Watchlist:
trakt_watchlist:
trakt_watchlist:
- me
- friendontrakt
sync_mode: sync

View file

@ -19,12 +19,6 @@ import config_tools
import plex_tools
import trakt
def adjust_space(old_length, display_title):
space_length = old_length - len(display_title)
if space_length > 0:
display_title += " " * space_length
return display_title
def imdb_get_ids(plex, imdb_url):
imdb_url = imdb_url.strip()
if imdb_url.startswith("https://www.imdb.com/list/ls") or imdb_url.startswith("https://www.imdb.com/search/title/?"):
@ -70,61 +64,20 @@ def imdb_get_ids(plex, imdb_url):
print("| Config Error {} must begin with either:\n| https://www.imdb.com/list/ls (For Lists)\n| https://www.imdb.com/search/title/? (For Searches)")
return None
def imdb_get_movies(config_path, plex, data):
def imdb_get_movies(config_path, plex, plex_map, data):
title_ids = data[1]
print("| {} Movies found on IMDb".format(len(title_ids)))
tmdb = TMDb()
tmdb.api_key = config_tools.TMDB(config_path).apikey
movie = Movie()
imdb_map = {}
matched_imdb_movies = []
missing_imdb_movies = []
current_length = 0
current_count = 0
plex_tools.create_cache(config_path)
plex_movies = plex.Library.all()
for m in plex_movies:
current_count += 1
print_display = "| Processing: {}/{} {}".format(current_count, len(plex_movies), m.title)
print(adjust_space(current_length, print_display), end="\r")
current_length = len(print_display)
if 'plex://' in m.guid:
item = m
# Check cache for imdb_id
imdb_id = plex_tools.query_cache(config_path, item.guid, 'imdb_id')
if not imdb_id:
imdb_id, tmdb_id = plex_tools.alt_id_lookup(plex, item)
print(adjust_space(current_length, "| Cache | + | {} | {} | {} | {}".format(item.guid, imdb_id, tmdb_id, item.title)))
plex_tools.update_cache(config_path, item.guid, imdb_id=imdb_id, tmdb_id=tmdb_id)
elif '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 if hasattr(tmdbapi, 'imdb_id') else None
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
print(adjust_space(current_length, "| Processed {} Movies".format(len(plex_movies))))
matched = []
missing = []
for imdb_id in title_ids:
movie = imdb_map.pop(imdb_id, None)
if movie:
matched_imdb_movies.append(plex.Server.fetchItem(movie.ratingKey))
if imdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[imdb_id]))
else:
missing_imdb_movies.append(imdb_id)
missing.append(imdb_id)
return matched, missing
return matched_imdb_movies, missing_imdb_movies
def tmdb_get_movies(config_path, plex, data, method):
def tmdb_get_movies(config_path, plex, plex_map, data, method):
t_movs = []
t_movie = Movie()
t_movie.api_key = config_tools.TMDB(config_path).apikey # Set TMDb api key for Movie
@ -212,61 +165,13 @@ def tmdb_get_movies(config_path, plex, data, method):
raise ValueError("| Config Error: TMDb ID: {} not found".format(tmdb_id))
print("| Processing {}: ({}) {}".format(method, tmdb_id, tmdb_name))
# 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]
elif "plex://" in guid:
guid = guid.split('plex://')[1].split('?')[0]
else:
guid = "None"
p_m_map[m] = guid
matched = []
missing = []
plex_tools.create_cache(config_path)
# 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
item = m
agent_type = urlparse(m.guid).scheme.split('.')[-1]
# Plex movie agent
if agent_type == 'plex':
# Check cache for tmdb_id
tmdb_id = plex_tools.query_cache(config_path, item.guid, 'tmdb_id')
imdb_id = plex_tools.query_cache(config_path, item.guid, 'imdb_id')
if not tmdb_id:
imdb_id, tmdb_id = plex_tools.alt_id_lookup(plex, item)
print("| Cache | + | {} | {} | {} | {}".format(item.guid, imdb_id, tmdb_id, item.title))
plex_tools.update_cache(config_path, item.guid, imdb_id=imdb_id, tmdb_id=tmdb_id)
if int(tmdb_id) == int(mid):
match = True
break
elif agent_type == 'themoviedb':
if int(p_m_map[m]) == int(mid):
match = True
break
elif agent_type == 'imdb':
imdb_id = t_movie.details(mid).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)
for mid in t_movs:
imdb_id = t_movie.details(mid).imdb_id
if imdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[imdb_id]))
else:
# Duplicate TMDb call?
missing.append(t_movie.details(mid).imdb_id)
return matched, missing
@ -399,33 +304,14 @@ def tmdb_get_shows(config_path, plex, data, method):
raise ValueError("| Config Error: TMDb ID: {} not found".format(tmdb_id))
print("| Processing {}: ({}) {}".format(method, tmdb_id, tmdb_name))
p_tv_map = {}
for item in plex.Library.all():
guid = urlparse(item.guid)
item_type = guid.scheme.split('.')[-1]
if item_type == 'thetvdb':
tvdb_id = guid.netloc
elif item_type == 'themoviedb':
tvdb_id = get_tvdb_id_from_tmdb_id(guid.netloc)
else:
tvdb_id = None
p_tv_map[item] = tvdb_id
matched = []
missing = []
for mid in t_tvs:
match = False
tvdb_id = get_tvdb_id_from_tmdb_id(mid)
if tvdb_id is None:
print("| Trakt Error: tmbd_id: {} could not converted to tvdb_id try just using tvdb_id instead".format(mid))
else:
for t in p_tv_map:
if p_tv_map[t] and "tt" not in p_tv_map[t] != "None":
if p_tv_map[t] is not None and int(p_tv_map[t]) == int(tvdb_id):
match = True
break
if match:
matched.append(t)
elif tvdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[tvdb_id]))
else:
missing.append(tvdb_id)
@ -434,32 +320,13 @@ def tmdb_get_shows(config_path, plex, data, method):
def tvdb_get_shows(config_path, plex, data):
config_tools.TraktClient(config_path)
id = int(data)
p_tv_map = {}
for item in plex.Library.all():
guid = urlparse(item.guid)
item_type = guid.scheme.split('.')[-1]
if item_type == 'thetvdb':
tvdb_id = guid.netloc
elif item_type == 'themoviedb' and TraktClient.valid:
tvdb_id = get_tvdb_id_from_tmdb_id(guid.netloc)
else:
tvdb_id = None
p_tv_map[item] = tvdb_id
tvdb_id = int(data)
matched = []
missing = []
match = False
for t in p_tv_map:
if p_tv_map[t] and "tt" not in p_tv_map[t] != "None":
if p_tv_map[t] is not None and int(p_tv_map[t]) == int(id):
match = True
break
if match:
matched.append(t)
if tvdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[tvdb_id]))
else:
missing.append(id)
missing.append(tvdb_id)
return matched, missing

View file

@ -11,6 +11,8 @@ from plexapi.video import Show
from plexapi.library import MovieSection
from plexapi.library import ShowSection
from plexapi.library import Collections
from plex_tools import get_movie_map
from plex_tools import get_show_map
from plex_tools import add_to_collection
from plex_tools import delete_collection
from plex_tools import get_actor_rkey
@ -30,6 +32,7 @@ from config_tools import check_for_attribute
from radarr_tools import add_to_radarr
from urllib.parse import urlparse
def regex_first_int(data, method, id_type="number", default=None):
try:
id = re.search('(\\d+)', str(data)).group(1)
@ -104,12 +107,16 @@ def get_method_pair_year(method_to_parse, values_to_parse):
def update_from_config(config_path, plex, headless=False, no_meta=False, no_images=False):
config = Config(config_path)
collections = config.collections
if isinstance(plex.Library, MovieSection):
libtype = "movie"
elif isinstance(plex.Library, ShowSection):
libtype = "show"
if not headless:
print("|\n|===================================================================================================|")
if isinstance(plex.Library, MovieSection):
libtype = "movie"
print("|\n| Processing Plex Movies")
plex_map = get_movie_map(config_path, plex)
elif isinstance(plex.Library, ShowSection):
libtype = "show"
print("|\n| Processing Plex Shows")
plex_map = get_show_map(config_path, plex)
alias = {
"actors": "actor", "role": "actor", "roles": "actor",
"content_ratings": "content_rating", "contentRating": "content_rating", "contentRatings": "content_rating",
@ -423,7 +430,7 @@ def update_from_config(config_path, plex, headless=False, no_meta=False, no_imag
try:
final_collections.append(get_collection(plex, new_collection, headless))
except ValueError as e:
print("| Config Error: {} {}".format(method_name, new_collection))
print("| Config Error: {} {} Not Found".format(method_name, new_collection))
if len(final_collections) > 0:
methods.append(("plex_collection", final_collections))
elif method_name == "tmdb_collection":
@ -612,7 +619,7 @@ def update_from_config(config_path, plex, headless=False, no_meta=False, no_imag
elif m not in ["plex_search", "tmdb_list", "tmdb_id", "tmdb_movie", "tmdb_collection", "tmdb_company", "tmdb_network", "tmdb_discover", "tmdb_show"]:
print("| \n| Processing {}: {}".format(m, v))
try:
missing, map = add_to_collection(config_path, plex, m, v, c, map, filters)
missing, map = add_to_collection(config_path, plex, m, v, c, plex_map, map, filters)
except (KeyError, ValueError, SystemExit) as e:
print(e)
missing = False
@ -962,7 +969,7 @@ print("| | _/| |/ -_)\ \ / / _ \| || || _|/ _ \ | (__ / _ \| || |/ -_)/ _|
print("| |_| |_|\___|/_\_\ /_/ \_\\\\_,_| \__|\___/ \___|\___/|_||_|\___|\__| \__||_|\___/|_||_|/__/ |")
print("| |")
print("|===================================================================================================|")
print("| Version 2.6.0")
print("| Version 2.6.1")
print("| Locating config...")
config_path = None
app_dir = os.path.dirname(os.path.abspath(__file__))

View file

@ -6,6 +6,7 @@ from plexapi.library import ShowSection
from datetime import datetime, timedelta
import imdb_tools
import trakt_tools
import trakt
from config_tools import Config
from config_tools import TMDB
from config_tools import TraktClient
@ -14,10 +15,18 @@ from bs4 import BeautifulSoup
from urllib.request import Request
from urllib.request import urlopen
from urllib.parse import urlparse
from tmdbv3api import TMDb
from tmdbv3api import Movie as TMDb_Movie
import os
import sqlite3
def adjust_space(old_length, display_title):
space_length = old_length - len(display_title)
if space_length > 0:
display_title += " " * space_length
return display_title
def get_movie(plex, data):
# If an int is passed as data, assume it is a movie's rating key
if isinstance(data, int):
@ -83,6 +92,91 @@ def get_actor_rkey(plex, data):
except UnboundLocalError:
raise ValueError("| Config Error: Actor: {} not found".format(search))
def get_movie_map(config_path, plex):
movie_map = {}
current_length = 0
current_count = 0
if TMDB.valid:
tmdb = TMDb()
tmdb.api_key = TMDB(config_path).apikey
tmovie = TMDb_Movie()
plex_movies = plex.Library.all()
created = False
for m in plex_movies:
current_count += 1
print_display = "| Processing: {}/{} {}".format(current_count, len(plex_movies), m.title)
print(adjust_space(current_length, print_display), end="\r")
current_length = len(print_display)
guid = urlparse(m.guid)
item_type = guid.scheme.split('.')[-1]
if item_type == 'plex':
if created == False:
create_cache()
created = True
imdb_id = query_cache(config_path, m.guid, 'imdb_id')
if not imdb_id:
imdb_id, tmdb_id = alt_id_lookup(plex, m)
print(adjust_space(current_length, "| Cache | + | {} | {} | {} | {}".format(m.guid, imdb_id, tmdb_id, m.title)))
update_cache(config_path, m.guid, imdb_id=imdb_id, tmdb_id=tmdb_id)
elif item_type == 'imdb':
imdb_id = guid.netloc
elif item_type == 'themoviedb':
tmdb_id = guid.netloc
imdb_id = None
if TMDB.valid and imdb_id is None:
tmdbapi = tmovie.details(tmdb_id)
if hasattr(tmdbapi, 'imdb_id'):
imdb_id = tmdbapi.imdb_id
if TraktClient.valid and imdb_id is None:
lookup = trakt.Trakt['search'].lookup(tmdb_id, 'tmdb', 'movie')
if lookup:
if isinstance(lookup, list):
imdb_id = lookup[0].get_key('imdb')
else:
imdb_id = lookup.get_key('imdb')
else:
imdb_id = None
if imdb_id:
movie_map[imdb_id] = m.ratingKey
else:
print(adjust_space(current_length, "| Unable to map IMDb ID for {} [GUID]: {}".format(m.title, m.guid)))
print(adjust_space(current_length, "| Processed {} Movies".format(len(plex_movies))))
return movie_map
def get_show_map(config_path, plex):
show_map = {}
current_length = 0
current_count = 0
if TMDB.valid:
tmdb = TMDb()
tmdb.api_key = TMDB(config_path).apikey
plex_shows = plex.Library.all()
for s in plex_shows:
current_count += 1
print_display = "| Processing: {}/{} {}".format(current_count, len(plex_shows), s.title)
print(adjust_space(current_length, print_display), end="\r")
current_length = len(print_display)
guid = urlparse(s.guid)
item_type = guid.scheme.split('.')[-1]
if item_type == 'thetvdb':
tvdb_id = guid.netloc
elif item_type == 'themoviedb' and TraktClient.valid:
tmdb_id = guid.netloc
lookup = trakt.Trakt['search'].lookup(tmdb_id, 'tmdb', 'show')
if lookup:
lookup = lookup[0] if isinstance(lookup, list) else lookup
tvdb_id = lookup.get_key('tvdb')
else:
tvdb_id = None
else:
tvdb_id = None
if tvdb_id:
show_map[tvdb_id] = s.ratingKey
else:
print(adjust_space(current_length, "| Unable to map TVDb ID for {} [GUID]: {}".format(s.title, s.guid)))
print(adjust_space(current_length, "| Processed {} Shows".format(len(plex_shows))))
return show_map
# subtype can be 'movie', 'show', or None (movie/tv combined)
def get_collection(plex, data, exact=None, subtype=None):
collection_list = plex.Library.search(title=data, libtype="collection")
@ -105,12 +199,14 @@ def get_collection(plex, data, exact=None, subtype=None):
print("| Invalid entry")
except (IndexError, ValueError) as E:
print("| Invalid entry")
elif len(collection_list) == 1 and (exact is None or (exact and collection_list[0].title == data)):
elif len(collection_list) == 1 and (exact is None or collection_list[0].title == data):
return collection_list[0]
else:
raise ValueError("Collection {} not found".format(data))
raise ValueError("Collection {} Not Found".format(data))
def add_to_collection(config_path, plex, method, value, c, map, filters=None):
def add_to_collection(config_path, plex, method, value, c, plex_map=None, map=None, filters=None):
if map is None:
map = {}
movies = []
shows = []
items = []
@ -143,14 +239,16 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
elif method == "tautulli" and not Tautulli.valid:
raise KeyError("| tautulli connection required for {}",format(method))
elif plex.library_type == "movie":
if plex_map is None:
plex_map = get_movie_map()
if method == "plex_collection":
movies = value.children
elif method == "imdb_list":
movies, missing = imdb_tools.imdb_get_movies(config_path, plex, value)
movies, missing = imdb_tools.imdb_get_movies(config_path, plex, plex_map, value)
elif "tmdb" in method:
movies, missing = imdb_tools.tmdb_get_movies(config_path, plex, value, method)
movies, missing = imdb_tools.tmdb_get_movies(config_path, plex, plex_map, value, method)
elif "trakt" in method:
movies, missing = trakt_tools.trakt_get_movies(config_path, plex, value, method)
movies, missing = trakt_tools.trakt_get_movies(config_path, plex, plex_map, value, method)
elif method == "tautulli":
movies, missing = imdb_tools.get_tautulli(config_path, plex, value)
elif method == "all":
@ -160,14 +258,16 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
else:
print("| Config Error: {} method not supported".format(method))
elif plex.library_type == "show":
if plex_map is None:
plex_map = get_show_map()
if method == "plex_collection":
shows = value.children
elif "tmdb" in method:
shows, missing = imdb_tools.tmdb_get_shows(config_path, plex, value, method)
shows, missing = imdb_tools.tmdb_get_shows(config_path, plex, plex_map, value, method)
elif method == "tvdb_show":
shows, missing = imdb_tools.tvdb_get_shows(config_path, plex, value)
shows, missing = imdb_tools.tvdb_get_shows(config_path, plex, plex_map, value)
elif "trakt" in method:
shows, missing = trakt_tools.trakt_get_shows(config_path, plex, value, method)
shows, missing = trakt_tools.trakt_get_shows(config_path, plex, plex_map, value, method)
elif method == "tautulli":
shows, missing = imdb_tools.get_tautulli(config_path, plex, value)
elif method == "all":
@ -215,7 +315,7 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
if filters:
for f in filters:
print_display = "| Filtering {}/{} {}".format(display_count, movie_max, current_m.title)
print(imdb_tools.adjust_space(current_length, print_display), end = "\r")
print(adjust_space(current_length, print_display), end = "\r")
current_length = len(print_display)
modifier = f[0][-4:]
method = filter_alias[f[0][:-4]] if modifier in [".not", ".lte", ".gte"] else filter_alias[f[0]]
@ -264,8 +364,8 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
map[current_m.ratingKey] = None
else:
current_m.addCollection(c)
print(imdb_tools.adjust_space(current_length, "| {} Collection | {} | {}".format(c, "=" if current_m in fs else "+", current_m.title)))
print(imdb_tools.adjust_space(current_length, "| Processed {} Movies".format(movie_max)))
print(adjust_space(current_length, "| {} Collection | {} | {}".format(c, "=" if current_m in fs else "+", current_m.title)))
print(adjust_space(current_length, "| Processed {} Movies".format(movie_max)))
elif plex.library_type == "movie":
print("| No movies found")
@ -287,7 +387,7 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
if filters:
for f in filters:
print_display = "| Filtering {}/{} {}".format(show_count, show_max, current_s.title)
print(imdb_tools.adjust_space(current_length, print_display), end = "\r")
print(adjust_space(current_length, print_display), end = "\r")
current_length = len(print_display)
modifier = f[0][-4:]
method = filter_alias[f[0][:-4]] if modifier in [".not", ".lte", ".gte"] else filter_alias[f[0]]
@ -336,8 +436,8 @@ def add_to_collection(config_path, plex, method, value, c, map, filters=None):
map[current_s.ratingKey] = None
else:
current_s.addCollection(c)
print(imdb_tools.adjust_space(current_length, "| {} Collection | {} | {}".format(c, "=" if current_s in fs else "+", current_s.title)))
print(imdb_tools.adjust_space(current_length, "| Processed {} Shows".format(show_max)))
print(adjust_space(current_length, "| {} Collection | {} | {}".format(c, "=" if current_s in fs else "+", current_s.title)))
print(adjust_space(current_length, "| Processed {} Shows".format(show_max)))
elif plex.library_type == "show":
print("| No shows found")

View file

@ -5,7 +5,7 @@ import trakt
import os
def trakt_get_movies(config_path, plex, data, method):
def trakt_get_movies(config_path, plex, plex_map, data, method):
config_tools.TraktClient(config_path)
if method == "trakt_trending":
max_items = int(data)
@ -24,52 +24,17 @@ def trakt_get_movies(config_path, plex, data, method):
trakt_list_items = trakt.Trakt[trakt_list_path].items()
title_ids = [m.pk[1] for m in trakt_list_items if isinstance(m, trakt.objects.movie.Movie)]
imdb_map = {}
plex_tools.create_cache(config_path)
if title_ids:
for item in plex.Library.all():
item_type = urlparse(item.guid).scheme.split('.')[-1]
if item_type == 'plex':
# Check cache for imdb_id
imdb_id = plex_tools.query_cache(config_path, item.guid, 'imdb_id')
if not imdb_id:
imdb_id, tmdb_id = plex_tools.alt_id_lookup(plex, item)
print("| Cache | + | {} | {} | {} | {}".format(item.guid, imdb_id, tmdb_id, item.title))
plex_tools.update_cache(config_path, item.guid, imdb_id=imdb_id, tmdb_id=tmdb_id)
elif item_type == 'imdb':
imdb_id = urlparse(item.guid).netloc
elif item_type == 'themoviedb':
tmdb_id = urlparse(item.guid).netloc
# lookup can sometimes return a list
lookup = trakt.Trakt['search'].lookup(tmdb_id, 'tmdb', 'movie')
if lookup:
lookup = lookup[0] if isinstance(lookup, list) else lookup
imdb_id = lookup.get_key('imdb')
else:
imdb_id = None
else:
imdb_id = None
print("| {} Movies found on Trakt".format(len(title_ids)))
matched = []
missing = []
for imdb_id in title_ids:
if imdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[imdb_id]))
else:
missing.append(imdb_id)
return matched, missing
if imdb_id and imdb_id in title_ids:
imdb_map[imdb_id] = item
else:
imdb_map[item.ratingKey] = item
matched_imdb_movies = []
missing_imdb_movies = []
for imdb_id in title_ids:
movie = imdb_map.pop(imdb_id, None)
if movie:
matched_imdb_movies.append(plex.Server.fetchItem(movie.ratingKey))
else:
missing_imdb_movies.append(imdb_id)
return matched_imdb_movies, missing_imdb_movies
else:
# No movies
return None, None
def trakt_get_shows(config_path, plex, data, method):
def trakt_get_shows(config_path, plex, plex_map, data, method):
config_tools.TraktClient(config_path)
if method == "trakt_trending":
max_items = int(data)
@ -87,7 +52,6 @@ def trakt_get_shows(config_path, plex, data, method):
trakt_list_path = urlparse(trakt_url).path
trakt_list_items = trakt.Trakt[trakt_list_path].items()
tvdb_map = {}
title_ids = []
for m in trakt_list_items:
if isinstance(m, trakt.objects.show.Show):
@ -100,40 +64,11 @@ def trakt_get_shows(config_path, plex, data, method):
if m.show.pk[1] not in title_ids:
title_ids.append(m.show.pk[1])
if title_ids:
for item in plex.Library.all():
guid = urlparse(item.guid)
item_type = guid.scheme.split('.')[-1]
# print('item_type', item, item_type)
if item_type == 'thetvdb':
tvdb_id = guid.netloc
elif item_type == 'themoviedb':
tmdb_id = guid.netloc
lookup = trakt.Trakt['search'].lookup(tmdb_id, 'tmdb', 'show')
if lookup:
lookup = lookup[0] if isinstance(lookup, list) else lookup
tvdb_id = lookup.get_key('tvdb')
else:
tvdb_id = None
else:
tvdb_id = None
if tvdb_id and tvdb_id in title_ids:
tvdb_map[tvdb_id] = item
else:
tvdb_map[item.ratingKey] = item
matched_tvdb_shows = []
missing_tvdb_shows = []
for tvdb_id in title_ids:
show = tvdb_map.pop(tvdb_id, None)
if show:
matched_tvdb_shows.append(plex.Server.fetchItem(show.ratingKey))
else:
missing_tvdb_shows.append(tvdb_id)
return matched_tvdb_shows, missing_tvdb_shows
else:
# No shows
return None, None
matched = []
missing = []
for tvdb_id in title_ids:
if tvdb_id in plex_map:
matched.append(plex.Server.fetchItem(plex_map[tvdb_id]))
else:
missing.append(tvdb_id)
return matched, missing