2024-04-17 09:11:41 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from http.cookiejar import LWPCookieJar
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import lxml.html
|
|
|
|
from random import shuffle
|
|
|
|
from time import sleep
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
re_recommend = re.compile(
|
|
|
|
' <a class="actionLinkLite " href="(/recommendations/([^/]*?)/([^/]*?))">'
|
|
|
|
)
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
s = requests.Session()
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
cookie_dir = "/home/edward/lib/cookies"
|
|
|
|
cookie_file = os.path.join(cookie_dir, "goodreads")
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
cj = LWPCookieJar(cookie_file)
|
|
|
|
if os.path.exists(cookie_file):
|
|
|
|
cj.load()
|
|
|
|
s.cookies = cj
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
|
2024-04-17 09:11:41 +01:00
|
|
|
def login():
|
2024-04-17 09:12:11 +01:00
|
|
|
sign_in_page = "https://www.goodreads.com/user/sign_in"
|
2024-04-17 09:11:41 +01:00
|
|
|
page = s.get(sign_in_page).text
|
2024-04-17 09:12:11 +01:00
|
|
|
open("sign_in.html", "w").write(page)
|
2024-04-17 09:11:41 +01:00
|
|
|
if '"name":"Edward Betts"' in page:
|
|
|
|
return # already signed in
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
re_token = re.compile(
|
|
|
|
'<input type="hidden" name="authenticity_token" value="([^"]*?)" />'
|
|
|
|
)
|
2024-04-17 09:11:41 +01:00
|
|
|
re_n = re.compile("<input name='n' type='hidden' value='(\d+)'>")
|
|
|
|
|
|
|
|
token = re_token.search(page).group(1)
|
|
|
|
|
|
|
|
data = {
|
2024-04-17 09:12:11 +01:00
|
|
|
"utf8": "\u2713",
|
|
|
|
"authenticity_token": token,
|
|
|
|
"user[email]": "edward@4angle.com",
|
|
|
|
"user[password]": "8V8~9:3~U!Ly",
|
|
|
|
"remember_me": 1,
|
|
|
|
"next": "Sign in",
|
|
|
|
"n": re_n.search(page).group(1),
|
2024-04-17 09:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print(token)
|
2024-04-17 09:12:11 +01:00
|
|
|
print(data["n"])
|
2024-04-17 09:11:41 +01:00
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
r = s.post(sign_in_page, data=data, headers={"referer": sign_in_page})
|
2024-04-17 09:11:41 +01:00
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
open("signed_in.html", "w").write(r.text)
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
root = lxml.html.fromstring(r.content)
|
2024-04-17 09:12:11 +01:00
|
|
|
flash = root.find_class("flash")
|
2024-04-17 09:11:41 +01:00
|
|
|
if flash:
|
2024-04-17 09:12:11 +01:00
|
|
|
print("flash:", flash[0].text)
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
cj.save(ignore_discard=True)
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
|
2024-04-17 09:11:41 +01:00
|
|
|
def get_index():
|
|
|
|
# url = 'https://www.goodreads.com/recommendations'
|
2024-04-17 09:12:11 +01:00
|
|
|
url = "https://www.goodreads.com/recommendations/?recs_current_view=list"
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
r = s.get(url)
|
2024-04-17 09:12:11 +01:00
|
|
|
open("recommendations.html", "w").write(r.text)
|
|
|
|
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
def get_individual():
|
2024-04-17 09:12:11 +01:00
|
|
|
for line in open("recommendations.html"):
|
|
|
|
if "actionLinkLite" not in line:
|
2024-04-17 09:11:41 +01:00
|
|
|
continue
|
|
|
|
m = re_recommend.match(line)
|
|
|
|
if m:
|
|
|
|
yield m.groups()
|
|
|
|
|
|
|
|
|
|
|
|
# art = 'https://www.goodreads.com/recommendations/genre/art'
|
|
|
|
login()
|
|
|
|
get_index()
|
|
|
|
recommend_list = list(get_individual())
|
|
|
|
shuffle(recommend_list)
|
|
|
|
|
2024-04-17 09:12:11 +01:00
|
|
|
headers = {"Accept": "text/html"}
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
for a, b, c in recommend_list:
|
|
|
|
print((b, c))
|
2024-04-17 09:12:11 +01:00
|
|
|
url = "https://www.goodreads.com" + a
|
2024-04-17 09:11:41 +01:00
|
|
|
|
|
|
|
r = s.get(url, headers=headers)
|
2024-04-17 09:12:11 +01:00
|
|
|
filename = os.path.join(b, c + ".html")
|
|
|
|
open(filename, "w").write(r.text)
|
2024-04-17 09:11:41 +01:00
|
|
|
sleep(0.5)
|