Reputation: 121
import requests
import json
def get_movies_from_tastedive(movie):
d = {"q":movie,"type":"movie","limit":"5"}
movies = requests.get("https://tastedive.com/api/similar",params=d)
x = movies.json()
print(x)
I am running the above code trying to access TasteDive API but I keep getting this error:
{'error': 'Response not interpretable as json. Try printing the .text attribute'}
Why is this happening?
Upvotes: 1
Views: 1147
Reputation: 1
I too had the same issue in Coursera's system (even with requests_with_caching
) where it worked on my machine. The fix is to change your parameters to reflect proper JSON formatting (i.e. space after each colon and comma):
Your code:
d = {"q":movie,"type":"movie","limit":"5"}
Proper code which fixed my issue (I had already swapped to requests_with_caching before):
d = {"q": movie, "type": "movie", "limit": "5"}
After this change my test began miraculously passing.
Upvotes: 0
Reputation: 1
import requests_with_caching
import json
def get_movies_from_tastedive(name_movie):
parameters = {'q':name_movie, 'type':'movies', 'limit':5}
a = requests_with_caching.get( 'https://tastedive.com/api/similar', params = parameters)
b = a.json()
return b
print(get_movies_from_tastedive("Bridesmaids"))
print(get_movies_from_tastedive("Black Panther"))
----------------------------------------------------------------------------
Output
found in permanent_cache
{'Similar': {'Info': [{'Name': 'Bridesmaids', 'Type': 'movie'}], 'Results': [{'Name': 'Baby Mama', 'Type': 'movie'}, {'Name': 'The Five-Year Engagement', 'Type': 'movie'}, {'Name': 'Bachelorette', 'Type': 'movie'}, {'Name': 'The Heat', 'Type': 'movie'}, {'Name': 'Date Night', 'Type': 'movie'}]}}
found in permanent_cache
{'Similar': {'Info': [{'Name': 'Black Panther', 'Type': 'movie'}], 'Results': [{'Name': 'Captain Marvel', 'Type': 'movie'}, {'Name': 'Avengers: Infinity War', 'Type': 'movie'}, {'Name': 'Ant-Man And The Wasp', 'Type': 'movie'}, {'Name': 'The Fate Of The Furious', 'Type': 'movie'}, {'Name': 'Deadpool 2', 'Type': 'movie'}]}}
Upvotes: 0
Reputation: 3262
Your problem looks very similar to the same I got on Data Collection and Processing with Python final assignment.
I fixed it using requests_with_caching
instead of requests
on Runestone's task
import requests_with_caching
def get_movies_from_tastedive(criteria):
params = {'q': criteria, 'type': 'movies', 'limit': '5'}
return requests_with_caching.get('https://tastedive.com/api/similar', params = params).json()
Upvotes: 0
Reputation: 11505
Actually you are the calling the API
wrongly. first of all you need to use data=
or json=
depending on type of content you are sending to the API
if it's form-data
or JSON
.
Secondly: movie
is invalid value
because it's need to be wrapped with "movie"
< double quotation.
Below you should be able to call the API
correctly :
import requests
import json
data = {
"q": "movie",
"type": "movie",
"limit": "5"
}
def main(url):
r = requests.get(url, json=data).json()
print(r)
#print(r.keys()) # to see the keys as it's now a JSON dict.
dumper = json.dumps(r, indent=4)
# print(dumper) #to see it in nice formatted tree.
main("https://tastedive.com/api/similar")
Output:
{'Similar': {'Info': [{'Name': '!!!', 'Type': 'music'}], 'Results': [{'Name': 'Meeting Of Important People', 'Type': 'music'}, {'Name': 'Soldout', 'Type': 'music'}, {'Name': 'La Tour Montparnasse Infernale', 'Type': 'movie'}, {'Name': 'Young & Sick', 'Type': 'music'}, {'Name': 'The Vanity Project', 'Type': 'music'}, {'Name': 'Tyler Bryant & The Shakedown', 'Type': 'music'}, {'Name': 'Thirsty Fish', 'Type': 'music'}, {'Name': 'Sombear', 'Type': 'music'}, {'Name': 'The High Court', 'Type': 'music'}, {'Name': 'Better Luck Next Time', 'Type': 'music'}, {'Name': 'Stars Of Track And Field',
'Type': 'music'}, {'Name': 'Beachwood Sparks', 'Type': 'music'}, {'Name':
'Tinted Windows', 'Type': 'music'}, {'Name': 'Promise Of Redemption', 'Type': 'music'}, {'Name': 'The Music', 'Type': 'music'}, {'Name': 'Pretty Girls Make Graves', 'Type': 'music'}, {'Name': 'Zach Gill', 'Type': 'music'}, {'Name': 'Chappo', 'Type': 'music'}, {'Name': 'Kisses', 'Type': 'music'}, {'Name': 'Jarle Bernhoft', 'Type': 'music'}]}}
And for the dumper
:
{
"Similar": {
"Info": [
{
"Name": "!!!",
"Type": "music"
}
],
"Results": [
{
"Name": "Meeting Of Important People",
"Type": "music"
},
{
"Name": "Soldout",
"Type": "music"
},
{
"Name": "La Tour Montparnasse Infernale",
"Type": "movie"
},
{
"Name": "Young & Sick",
"Type": "music"
},
{
"Name": "The Vanity Project",
"Type": "music"
},
{
"Name": "Tyler Bryant & The Shakedown",
"Type": "music"
},
{
"Name": "Thirsty Fish",
"Type": "music"
},
{
"Name": "Sombear",
"Type": "music"
},
{
"Name": "The High Court",
"Type": "music"
},
{
"Name": "Better Luck Next Time",
"Type": "music"
},
{
"Name": "Stars Of Track And Field",
"Type": "music"
},
{
"Name": "Beachwood Sparks",
"Type": "music"
},
{
"Name": "Tinted Windows",
"Type": "music"
},
{
"Name": "Promise Of Redemption",
"Type": "music"
},
{
"Name": "The Music",
"Type": "music"
},
{
"Name": "Pretty Girls Make Graves",
"Type": "music"
},
{
"Name": "Zach Gill",
"Type": "music"
},
{
"Name": "Chappo",
"Type": "music"
},
{
"Name": "Kisses",
"Type": "music"
},
{
"Name": "Jarle Bernhoft",
"Type": "music"
}
]
}
}
Upvotes: -1
Reputation: 8579
The endpoint doesn't return JSON. This probably means that you entered a file name having issues (maybe strange / not accepted characters?).
Please try this code to see what it returns, in my examples it returns always JSON data:
import requests
import json
def get_movies_from_tastedive(movie):
d = {"q":movie,"type":"movie","limit":"5"}
movies = requests.get("https://tastedive.com/api/similar",params=d)
try:
return movies.json()
except Exception as e:
print(e)
return movies.text
print("Getting data for movie Seven:")
print(get_movies_from_tastedive("Seven"))
print("\nGetting data for movie Sevssssen:")
print(get_movies_from_tastedive("Sevssssen"))
Output
Getting data for movie Seven:
{u'Similar': {u'Info': [{u'Type': u'movie', u'Name': u'Seven'}], u'Results': [{u'Type': u'movie', u'Name': u'Primal Fear'}, {u'Type': u'movie', u'Name': u'The Usual Suspects'}, {u'Type': u'movie', u'Name': u'The Game'}, {u'Type': u'movie', u'Name': u'Insomnia'}, {u'Type': u'movie', u'Name': u'American History X'}]}}
Getting data for movie Sevssssen:
{u'Similar': {u'Info': [{u'Type': u'unknown', u'Name': u'sevssssen'}, {u'Type': u'unknown', u'Name': u'sevssssen'}], u'Results': []}}
Upvotes: 3