Skip to content

Instantly share code, notes, and snippets.

@dhillan99
Last active September 15, 2020 18:48
Show Gist options
  • Save dhillan99/019a4d7e5dc2dfccf08ec865a52916f6 to your computer and use it in GitHub Desktop.
Save dhillan99/019a4d7e5dc2dfccf08ec865a52916f6 to your computer and use it in GitHub Desktop.
import requests_with_caching
import json
def get_movies_from_tastedive(movie):
baseurl = "https://tastedive.com/api/similar"
params_diction = {}
params_diction['q'] = movie
params_diction['type'] = 'movies'
params_diction['limit'] = 5
resp = requests_with_caching.get(baseurl, params=params_diction)
#word_d=json.loads(resp.text)
#w=word_d['Similar']
#print(w.keys())
#print(resp.url)
return json.loads(resp.text)
def extract_movie_titles(dic):
return ([i['Name'] for i in dic['Similar']['Results']])
#############################
def get_related_titles(movie_list):
li = []
for movie in movie_list:
li.extend(extract_movie_titles(get_movies_from_tastedive(movie)))
print(li)
return list(set(li))
def get_movie_data(title):
endpoint = 'http://www.omdbapi.com/'
param = {}
param['t'] = title
param['r'] = 'json'
this_page_cache = requests_with_caching.get(endpoint, params=param)
print(this_page_cache.url)
return json.loads(this_page_cache.text)
#print(get_movie_data("Black Panther")['Ratings'][1])
def get_movie_rating(dic):
ranking = dic['Ratings']
for dic_item in ranking:
if dic_item['Source'] == 'Rotten Tomatoes':
return int(dic_item['Value'][:-1])
return 0
###################
def get_sorted_recommendations(list):
new_list = get_related_titles(list)
new_dict = {}
for i in new_list:
rating = get_movie_rating(get_movie_data(i))
new_dict[i] = rating
print(new_dict)
print(sorted(new_dict, reverse=True))
return [i[0] for i in sorted(new_dict.items(), key=lambda item: (item[1], item[0]), reverse=True)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment