forked from github/copilot-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodreads.py
45 lines (40 loc) · 1.31 KB
/
goodreads.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
# import beautiful soup
from bs4 import BeautifulSoup
import requests
import json
# import plotting library from matplotlib
import matplotlib.pyplot as plt
def get_list_of_titles() :
"""Ask the user for a list of books, one per line."""
titles = []
while True:
new_title = input("Title: ")
if new_title:
titles.append(new_title)
else:
break
return titles
# Get the GoodReads API key from the environment
key = os.environ.get("GOODREADS_API_KEY")
def scrape_goodreads(titles) :
"""Get the average rating of each book from GoodReads, and return a list of floats."""
ratings = []
for title in titles:
url = "https://www.goodreads.com/book/title.xml?key={}&title={}".format(key, title)
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
rating = soup.find("average_rating").text
ratings.append(float(rating))
return ratings
def show(titles,ratings) :
"""Show the titles and ratings in a bar chart."""
# Make a bar chart
plt.bar(range(len(ratings)), ratings)
# Add labels to the ticks of the bar chart
plt.xticks(range(len(ratings)), titles)
# Show the plot
plt.show()
titles = get_list_of_titles()
ratings = scrape_goodreads(titles)
show(titles, ratings)