find tmdbid by imdbid if necessary

This commit is contained in:
Ronald Schaten 2021-03-18 22:11:37 +01:00
parent 207601f395
commit 1dc369bc3b

View File

@ -37,6 +37,22 @@ def files_from_dir(count, location):
files[i] = location + files[i] files[i] = location + files[i]
return files return files
def get_tmdbid(apikey, imdbid):
print("getting tmdbid for imdbid %s" % imdbid)
baseurl = 'https://api.themoviedb.org/3/'
url = baseurl + 'find/%s?api_key=%s&external_source=imdb_id'
url = url % (imdbid, apikey)
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())
print(data)
try:
tmdbid = data['movie_results'][0]['id']
print("tmdbid is %d" % tmdbid)
except:
tmdbid = 0
print("tmdbid could not be found")
return tmdbid
def get_recommendations(apikey, movieid, language, choice): def get_recommendations(apikey, movieid, language, choice):
print("getting %s for %d" % (choice, movieid)) print("getting %s for %d" % (choice, movieid))
baseurl = 'https://api.themoviedb.org/3/' baseurl = 'https://api.themoviedb.org/3/'
@ -99,10 +115,13 @@ def conduct_program(program_file, feature):
elif settings['source'] == 'tmdbtrailer': elif settings['source'] == 'tmdbtrailer':
apikey = settings['apikey'] apikey = settings['apikey']
tmdbid = int(feature['tmdbid']) tmdbid = int(feature['tmdbid'])
imdbid = feature['imdbid']
language = settings['language'] language = settings['language']
choice = settings['choice'] choice = settings['choice']
trailertype = settings['type'] trailertype = settings['type']
count = settings['count'] count = settings['count']
if not tmdbid:
tmdbid = get_tmdbid(apikey, imdbid)
if tmdbid: if tmdbid:
movies = get_recommendations(apikey, tmdbid, language, choice) movies = get_recommendations(apikey, tmdbid, language, choice)
random.shuffle(movies) random.shuffle(movies)
@ -138,11 +157,14 @@ def get_feature(movieid):
response = json.loads(json_response) response = json.loads(json_response)
if not 'tmdb' in response['result']['moviedetails']['uniqueid']: if not 'tmdb' in response['result']['moviedetails']['uniqueid']:
response['result']['moviedetails']['uniqueid']['tmdb'] = 0 response['result']['moviedetails']['uniqueid']['tmdb'] = 0
if not 'imdb' in response['result']['moviedetails']['uniqueid']:
response['result']['moviedetails']['uniqueid']['tmdb'] = ''
feature = { feature = {
'label': response['result']['moviedetails']['label'], 'label': response['result']['moviedetails']['label'],
'file': response['result']['moviedetails']['file'], 'file': response['result']['moviedetails']['file'],
'mpaa': response['result']['moviedetails']['mpaa'], 'mpaa': response['result']['moviedetails']['mpaa'],
'tmdbid': response['result']['moviedetails']['uniqueid']['tmdb'] 'tmdbid': response['result']['moviedetails']['uniqueid']['tmdb'],
'imdbid': response['result']['moviedetails']['uniqueid']['imdb']
} }
return feature return feature