Python Forum
Last Step of my Project! Getting data from an API with a secret key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Last Step of my Project! Getting data from an API with a secret key
#1
Hey guys! I'm so close! I'm almost done with this project! Don't worry though, there are 3 more projects so I'll be here for the months to come but I want to get this one done since the others don't have time limits (they are changing this project on the 1st of July so it is no longer available for submission).

Anyways. For the final step, I have to display data from the GoodReads.com API. I have the secret key already. What's the best way to go about including this data? I've never done it before.

Here is what I have to do:

Goodreads Review Data: On your book page, you should also display (if available) the average rating and number of ratings the work has received from Goodreads.

Goodreads API
Goodreads is a popular book review website, and we’ll be using their API in this project to get access to their review data for individual books.
1. Go to https://www.goodreads.com/api and sign up for a Goodreads account if you don’t already have one.
2. Navigate to https://www.goodreads.com/api/keys and apply for an API key. For “Application name” and “Company name” feel free to just write “project1,” and no need to include an application URL, callback URL, or support URL.
3. You should then see your API key. (For this project, we’ll care only about the “key”, not the “secret”.)
4. You can now use that API key to make requests to the Goodreads API, documented here. In particular, Python code like the below

import requests
res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": " NoDjSCv8OsMB1ZFqjfRxQ", "isbns": "9781632168146"})
print(res.json())
where KEY is your API key, will give you the review and rating data for the book with the provided ISBN number. In particular, you might see something like this dictionary:
{'books': [{
'id': 29207858,
'isbn': '1632168146',
'isbn13': '9781632168146',
'ratings_count': 0,
'reviews_count': 1,
'text_reviews_count': 0,
'work_ratings_count': 26,
'work_reviews_count': 113,
'work_text_reviews_count': 10,
'average_rating': '4.04'
}]
}
Note that work_ratings_count here is the number of ratings that this particular book has received, and average_rating is the book’s average score out of 5.
Reply
#2
(Jun-23-2020, 02:23 AM)card51shor Wrote: Anyways. For the final step, I have to display data from the GoodReads.com API. I have the secret key already. What's the best way to go about including this data? I've never done it before.

I'm not really sure what your question is. It's clear from your assignment text that you need to display the data on the page and you know how to do that with templates. You just need to make the HTTP requests and then pass the data in the response to your template. You're given an example of how to make the HTTP request and res.json() gives you a dictionary containing the response body. It should be pretty simple from there.
Reply
#3
OK. I am getting the information from Good reads however it's in a form that is not allowing me to access the certain elements. I looked it up and people transfer it to json and then access by its index. However, I'm trying that and getting syntax errors every time I try it.

Here's the code I'm using:

res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": " NoDjSCv8OsMB1ZFqjfRxQ", "isbns": isbn})
    print(res.json())
when I try to print res[0] it doesn't work. if I try res['id'] it doesn't work.

I can see that there's a ["books"] index in front of the data i'm getting from the request. Anyone know why exactly?

Here is what I'm seeing when it prints out. I see all the info but I need the work_ratings_count and average_review elements. Any idea of how to get these? Thanks!

İmage


(Jun-23-2020, 05:45 AM)ndc85430 Wrote: I'm not really sure what your question is. It's clear from your assignment text that you need to display the data on the page and you know how to do that with templates. You just need to make the HTTP requests and then pass the data in the response to your template. You're given an example of how to make the HTTP request and res.json() gives you a dictionary containing the response body. It should be pretty simple from there.


You would think so! However I can't get to the specific elements for some reason!! argg! Thanks buddy I'm sure u know what's up.
Reply
#4
So JSON basically ends up as Python dictionaries. From your screenshot, there's no key in res called "id", only "books". So you have to ask for the value for "books", which will give you the list of data for all of the books and you can process that however you like.

Also, a note on terminology: we use the term "key" for dictionaries, not "index", which is used just for sequential containers.
Reply
#5
Oh sorry. Thanks a lot - but I actually already tried that. I used one variable to grab the "books" field...and then I tried to use the same on the books for the ID but it didn't work. Here's my code and the error.

res2 = res["books"]
    id2 = res2["id"]
    print(id2)
I try the method above and I get the following error:

Error:
TypeError TypeError: 'Response' object is not subscriptable Traceback (most recent call last) File "C:\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "C:\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\school\project1\application.py", line 78, in book [Open an interactive python shell in this frame] res2 = res["books"]
Any suggestions? Credit me for trying that method already :)

Got it!

res2 = res.json()["books"]
    grCount = res2[0]['work_ratings_count']
    grAverage = res2[0]['average_rating']
Thanks guys! Think I'm pretty much done for this project. I learned a lot. It was a wild ride.

I have 3 more projects left so you'll see me around again soon. And I might have some more questions for this one.

Thanks man!!!

actually...this is what they're saying I have to do:

•	API Access: If users make a GET request to your website’s /api/<isbn> route, where <isbn> is an ISBN number, your website should return a JSON response containing the book’s title, author, publication date, ISBN number, review count, and average score. The resulting JSON should follow the format:
{
    "title": "Memory",
    "author": "Doug Lloyd",
    "year": 2015,
    "isbn": "1632168146",
    "review_count": 28,
    "average_score": 5.0
}
How exactly would I go about setting this up? This is the final step. Can u help me out a bit here? thanks
Reply
#6
What do you think you need to do?
Reply
#7
(Jun-24-2020, 03:19 AM)ndc85430 Wrote: What do you think you need to do?

Have I already done it here? :

@app.route("/book", methods=["GET", "POST"])
@app.route("/book/<isbn>")
Reply
#8
You need to start doing some of the thinking for yourself. Make a list of the things you think you need to do to accomplish the task.
Reply
#9
I don't know what they are saying by a GET request. How would someone do this?
Reply
#10
How have you been building a web app all this time and not know what kinds of HTTP requests there are? Have you really learnt nothing from this course? I thought you said you also had previous experience building web apps in PHP.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please explain me step by step. ITMani 1 2,747 Jul-26-2017, 08:28 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020