-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodreads.py
176 lines (160 loc) · 6.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import requests
from bs4 import BeautifulSoup
import pandas as pd
import os
import re
import utils
import math
# import requests
import aiohttp
import asyncio
import settings
def getRatingFromRow(row):
ratingLinks = row.findAll("span", {"class": "minirating"})
for link in ratingLinks:
ratingString = re.split("\s", link.text.strip())
GRrating = float(ratingString[ratingString.index("avg") - 1])
GRnumratings = ratingString[ratingString.index("avg") + 3]
GRnumratings = int(GRnumratings.replace(",", ""))
return GRrating, GRnumratings
def get_rating_from_url(url):
goodreads_rating = None
goodreads_num_ratings = None
try:
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
bookMeta = soup.select(
"body > div.content > div.mainContentContainer > div.mainContent > div.mainContentFloat > div.leftContainer > div#topcol > div#metacol > div#bookMeta"
)
ratingSpan = bookMeta[0].findAll("span", {"itemprop": "ratingValue"})
goodreads_rating = re.split("\s", ratingSpan[0].text.strip())[0]
numRatingsSpan = bookMeta[0].findAll("meta", {"itemprop": "ratingCount"})
goodreads_num_ratings = re.split("\s", numRatingsSpan[0].text.strip())[0]
except:
pass
return goodreads_rating, goodreads_num_ratings
async def get_rating_from_search(title, author):
goodreads_match = None
search_url = get_search_url(title, author)
# goodreads_page = requests.get(search_url)
async with aiohttp.ClientSession() as session:
async with session.get(search_url) as response:
goodreads_page = await response.text()
goodreads_soup = BeautifulSoup(goodreads_page, "html.parser")
rows = goodreads_soup.select(
"body > div > div > div > div > div.leftContainer > table.tableList > tr"
)
possibles = get_possible_matches_from_rows(rows, title, author)
goodreads_match = utils.best_row_item(possibles)
print(goodreads_match)
if goodreads_match:
return (
goodreads_match.goodreads_rating,
goodreads_match.goodreads_num_ratings,
goodreads_match.goodreads_link,
)
title = re.sub(r"\[.+\]", "", title)
author = re.sub(r", The Great Courses", "", author)
search_url = get_search_url(title, author)
async with aiohttp.ClientSession() as session:
async with session.get(search_url) as response:
goodreads_page = await response.text()
goodreads_soup = BeautifulSoup(goodreads_page, "html.parser")
rows = goodreads_soup.select(
"body > div > div > div > div > div.leftContainer > table.tableList > tr"
)
possibles = get_possible_matches_from_rows(rows, title, author)
goodreads_match = utils.best_row_item(possibles)
print(goodreads_match)
if goodreads_match:
return (
goodreads_match.goodreads_rating,
goodreads_match.goodreads_num_ratings,
goodreads_match.goodreads_link,
)
return None, None, None
def get_possible_matches_from_rows(rows, title, author):
possibles = []
for row in rows:
titleLinks = row.findAll("a", {"class": "bookTitle"})
for link in titleLinks:
goodreads_title = link.find("span").text
goodreads_link = "https://www.goodreads.com" + link.get("href")
authorLinks = row.findAll("a", {"class": "authorName"})
for link in authorLinks:
goodreads_author = link.find("span").text
author_tokens = utils.get_tokens(author)
goodreads_author_tokens = utils.get_tokens(goodreads_author)
title_tokens = utils.get_tokens(title)
goodreads_title_tokens = utils.get_tokens(goodreads_title)
# print(author_tokens)
# print(goodreads_author_tokens)
# print(title_tokens)
# print(goodreads_title_tokens)
# print(utils.compare_lists(author_tokens, goodreads_author_tokens))
# print(utils.compare_lists(title_tokens, goodreads_title_tokens))
if utils.compare_lists(author_tokens, goodreads_author_tokens) >= min(
len(goodreads_title_tokens), len(goodreads_title_tokens), 2
) and utils.compare_lists(title_tokens, goodreads_title_tokens) >= min(
len(title_tokens), len(title_tokens), 2
):
# print('Possible match')
goodreads_rating, goodreads_num_ratings = getRatingFromRow(row)
possibles.append(
GoodreadsMatch(
goodreads_link, goodreads_rating, goodreads_num_ratings
)
)
# print(possibles)
break
return possibles
def get_search_url(title, author):
title_adjusted = title.split(":", 1)[0].replace(" ", "+").replace("'", "%27")
author_adjusted = (
author.replace(" ", "+")
.replace("'", "%27")
.replace("PhD", "")
.replace("MD", "")
.replace("Dr ", "")
.replace("Dr.", "")
.replace("translator", "")
.replace("foreword", "")
.replace("featuring", "")
.replace("introduction", "")
.replace("note", "")
.replace("afterword", "")
.replace("essay", "")
.replace("contributor", "")
)
return (
"https://www.goodreads.com/search?q=" + title_adjusted + "+" + author_adjusted
)
class GoodreadsMatch:
def __init__(
self, goodreads_link, goodreads_rating=None, goodreads_num_ratings=None
):
self.goodreads_link = goodreads_link
self.goodreads_rating = goodreads_rating
self.goodreads_num_ratings = goodreads_num_ratings
def __repr__(self) -> str:
return (
self.goodreads_link
+ " "
+ str(self.goodreads_rating)
+ " "
+ str(self.goodreads_num_ratings)
)
async def get_goodreads_ratings(books):
results = await asyncio.gather(
*[get_rating_from_search(book.title, book.author) for book in books]
)
await asyncio.sleep(0.1)
for i in range(len(results)):
(
books[i].average_rating,
books[i].num_ratings,
books[i].goodreads_link,
) = results[i]
if not books[i].goodreads_link:
books[i].goodreads_link = "Failed"
return [book for book in books]