-
Notifications
You must be signed in to change notification settings - Fork 58
/
imdb_tools.py
executable file
·128 lines (114 loc) · 4.22 KB
/
imdb_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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(plex, data):
tmdb = TMDb()
movie = Movie()
tmdb.api_key = config_tools.TMDB().apikey
imdb_url = data
if imdb_url[-1:] == " ":
imdb_url = imdb_url[:-1]
imdb_map = {}
library_language = plex.MovieLibrary.language
try:
r = requests.get(imdb_url, headers={'Accept-Language': library_language})
except requests.exceptions.MissingSchema:
return
tree = html.fromstring(r.content)
title_ids = tree.xpath("//div[contains(@class, 'lister-item-image')]"
"//a/img//@data-tconst")
if title_ids:
for m in plex.MovieLibrary.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(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().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.MovieLibrary.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(data, type):
collection = Collection()
person = Person()
collection.api_key = config_tools.TMDB().apikey
person.api_key = collection.api_key
collection.language = config_tools.TMDB().language
person.language = collection.language
if type == "overview":
return collection.details(data).overview
elif type == "biography":
return person.details(data).biography