Reduce movie set/collection boilerplate

- To increase code maintainability
This commit is contained in:
tomkat83 2016-06-08 19:50:24 +02:00
parent f3afb01f5e
commit faf95f671c
2 changed files with 42 additions and 158 deletions

View file

@ -1895,24 +1895,33 @@ class API():
allartworks = self.getFanartTVArt(externalId, allartworks) allartworks = self.getFanartTVArt(externalId, allartworks)
return allartworks return allartworks
def getExternalItemId(self): def getExternalItemId(self, collection=False):
""" """
Returns the item's IMDB id for movies or tvdb id for TV shows Returns the item's IMDB id for movies or tvdb id for TV shows
If not found in item's Plex metadata, check themovidedb.org If not found in item's Plex metadata, check themovidedb.org
collection=True will try to return the collection's ID
None is returned if unsuccessful
""" """
item = self.item.attrib item = self.item.attrib
media_type = item.get('type') media_type = item.get('type')
externalId = None mediaId = None
if media_type == 'movie': # Return the saved Plex id's, if applicable
externalId = self.getProvider('imdb') # Always seek collection's ids since not provided by PMS
elif media_type == 'show': if collection is False:
externalId = self.getProvider('tvdb') if media_type == 'movie':
if externalId is not None: mediaId = self.getProvider('imdb')
return externalId elif media_type == 'show':
mediaId = self.getProvider('tvdb')
if mediaId is not None:
return mediaId
self.logMsg('Plex did not provide ID for IMDB or TVDB. Start '
'lookup process', 1)
else:
self.logMsg('Start movie set/collection lookup on themoviedb', 1)
self.logMsg('Plex did not provide ID for IMDB or TVDB. Start lookup '
'process', 1)
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1) KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
apiKey = utils.settings('themoviedbAPIKey') apiKey = utils.settings('themoviedbAPIKey')
if media_type == 'show': if media_type == 'show':
@ -2010,7 +2019,6 @@ class API():
parameters = { parameters = {
'api_key': apiKey 'api_key': apiKey
} }
mediaId = None
for language in [KODILANGUAGE, "en"]: for language in [KODILANGUAGE, "en"]:
parameters['language'] = language parameters['language'] = language
if media_type == "movie": if media_type == "movie":
@ -2030,144 +2038,20 @@ class API():
self.logMsg('Could not download %s with parameters %s' self.logMsg('Could not download %s with parameters %s'
% (url, parameters), -1) % (url, parameters), -1)
continue continue
if data.get("imdb_id") is not None: if collection is True:
mediaId = str(data.get("imdb_id")) if data.get("imdb_id") is not None:
break mediaId = str(data.get("imdb_id"))
if data.get("external_ids") is not None: break
mediaId = str(data["external_ids"].get("tvdb_id")) if data.get("external_ids") is not None:
break mediaId = str(data["external_ids"].get("tvdb_id"))
break
else:
if data.get("belongs_to_collection") is not None:
mediaId = str(data.get("belongs_to_collection").get("id"))
self.logMsg('Retrieved collections tmdb id %s'
% mediaId, 1)
return mediaId return mediaId
def getExternalItemSetId(self):
"""
Returns the item's IMDB id for movies or tvdb id for TV shows
If not found in item's Plex metadata, check themovidedb.org
"""
item = self.item.attrib
media_type = item.get('type')
externalId = None
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
apiKey = utils.settings('themoviedbAPIKey')
title = item.get('title', '')
# if the title has the year in remove it as tmdb cannot deal with it...
# replace e.g. 'The Americans (2015)' with 'The Americans'
title = re.sub(r'\s*\(\d{4}\)$', '', title, count=1)
url = 'http://api.themoviedb.org/3/search/%s' % media_type
parameters = {
'api_key': apiKey,
'language': KODILANGUAGE,
'query': utils.tryEncode(title)
}
data = downloadutils.DownloadUtils().downloadUrl(
url,
authenticate=False,
parameters=parameters,
timeout=7)
try:
data.get('test')
except:
self.logMsg('Could not download data from TMDB', -1)
return
if data.get('results') is None:
self.logMsg('No match found on themoviedb for type: %s, title: %s'
% (media_type, title), 1)
return
year = item.get('year')
matchFound = None
# find year match
if year is not None:
for entry in data["results"]:
if year in entry.get("first_air_date", ""):
matchFound = entry
break
elif year in entry.get("release_date", ""):
matchFound = entry
break
# find exact match based on title, if we haven't found a year match
if matchFound is None:
self.logMsg('No themoviedb match found using year %s' % year, 1)
replacements = (
' ',
'-',
'&',
',',
':',
';'
)
for entry in data["results"]:
name = entry.get("name", entry.get("title", ""))
original_name = entry.get("original_name", "")
title_alt = title.lower()
name_alt = name.lower()
org_name_alt = original_name.lower()
for replaceString in replacements:
title_alt = title_alt.replace(replaceString, '')
name_alt = name_alt.replace(replaceString, '')
org_name_alt = org_name_alt.replace(replaceString, '')
if name == title or original_name == title:
# match found for exact title name
matchFound = entry
break
elif (name.split(" (")[0] == title or title_alt == name_alt
or title_alt == org_name_alt):
# match found with substituting some stuff
matchFound = entry
break
# if a match was not found, we accept the closest match from TMDB
if matchFound is None and len(data.get("results")) > 0:
self.logMsg('Using very first match from themoviedb', 1)
matchFound = entry = data.get("results")[0]
if matchFound is None:
self.logMsg('Still no themoviedb match for type: %s, title: %s, '
'year: %s' % (media_type, title, year), 1)
self.logMsg('themoviedb answer was %s' % data['results'], 1)
return
self.logMsg('Found themoviedb match for %s: %s'
% (item.get('title'), matchFound), 1)
tmdbId = str(entry.get("id", ""))
if tmdbId == '':
self.logMsg('No themoviedb ID found, aborting', -1)
return
if media_type == "multi" and entry.get("media_type"):
media_type = entry.get("media_type")
name = entry.get("name", entry.get("title"))
# lookup external tmdbId and perform artwork lookup on fanart.tv
parameters = {
'api_key': apiKey
}
mediaId = None
for language in [KODILANGUAGE, "en"]:
parameters['language'] = language
if media_type == "movie":
url = 'http://api.themoviedb.org/3/movie/%s' % tmdbId
parameters['append_to_response'] = 'videos'
data = downloadutils.DownloadUtils().downloadUrl(
url,
authenticate=False,
parameters=parameters,
timeout=7)
try:
data.get('test')
except:
self.logMsg('Could not download %s with parameters %s'
% (url, parameters), -1)
continue
if data.get("belongs_to_collection") is not None:
mediaId = str(data.get("belongs_to_collection").get("id"))
self.logMsg('Retrieved collections tmdb id %s' %mediaId, 1)
break
return mediaId
def getFanartTVArt(self, mediaId, allartworks, setInfo=False): def getFanartTVArt(self, mediaId, allartworks, setInfo=False):
""" """
perform artwork lookup on fanart.tv perform artwork lookup on fanart.tv
@ -2267,6 +2151,8 @@ class API():
Gets the URLs to the Plex artwork, or empty string if not found. Gets the URLs to the Plex artwork, or empty string if not found.
parentInfo=True will check for parent's artwork if None is found parentInfo=True will check for parent's artwork if None is found
Only call on movies
Output: Output:
{ {
'Primary' 'Primary'
@ -2278,8 +2164,6 @@ class API():
'Backdrop' : LIST with the first entry xml key "art" 'Backdrop' : LIST with the first entry xml key "art"
} }
""" """
item = self.item.attrib
allartworks = { allartworks = {
'Primary': "", 'Primary': "",
'Art': "", 'Art': "",
@ -2290,14 +2174,11 @@ class API():
'Backdrop': [] 'Backdrop': []
} }
# Plex does not get much artwork - go ahead and get the rest from # Plex does not get much artwork - go ahead and get the rest from
# fanart tv only for movie or tv show # fanart tv only for movie or tv show
externalId = self.getExternalItemId(collection=True)
if item.get('type') in ('movie'): if externalId is not None:
externalId = self.getExternalItemSetId() allartworks = self.getFanartTVArt(externalId, allartworks, True)
if externalId is not None:
allartworks = self.getFanartTVArt(externalId, allartworks, True)
return allartworks return allartworks
def shouldStream(self): def shouldStream(self):

View file

@ -7,7 +7,7 @@ from ntpath import dirname
import artwork import artwork
import clientinfo import clientinfo
import utils from utils import settings
from utils import logging, kodiSQL from utils import logging, kodiSQL
############################################################################### ###############################################################################
@ -1186,8 +1186,11 @@ class Kodidb_Functions():
for setname in collections: for setname in collections:
setid = self.createBoxset(setname) setid = self.createBoxset(setname)
# Process artwork # Process artwork
if utils.settings('FanartTV') == 'true': if settings('FanartTV') == 'true':
self.artwork.addArtwork(API.getSetArtwork(), setid, "set", kodicursor) self.artwork.addArtwork(API.getSetArtwork(),
setid,
"set",
kodicursor)
self.assignBoxset(setid, movieid) self.assignBoxset(setid, movieid)
def createBoxset(self, boxsetname): def createBoxset(self, boxsetname):