-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoodreads.py
85 lines (74 loc) · 2.8 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
"""
GoodReads Cover Images
- Script to fetch book covers from GoodReads, after importing .csv
- Based on AmunRa1322's script https://github.com/AmunRa1322/Notion-Scripts
"""
import sys
from notion_client import Client
from bs4 import BeautifulSoup
from urllib.request import urlopen
from utils import get_all_in_collection, get_property_value
"""
Access Settings
"""
# TODO:
# Create an Integration Token here:
# https://www.notion.so/my-integrations
# And invite the Integration app to the collection you want it to access
client = Client(auth=">>ADD SECRET HERE<<")
# TODO:
# Find the database id by viewing the collection in your browser, and copying it from the URL:
# https://www.notion.so/my_workspace/COPY_THIS_DATABASE_ID?v=another_string
database_id = ">>ADD DATABASE ID HERE<<"
"""
Cover fetch options
"""
goodreads_url = "https://www.goodreads.com/book/show/"
book_table = []
all_results = get_all_in_collection(client, database_id)
for result in all_results:
properties = result['properties']
if "Book Id" in properties:
book_id = get_property_value(properties["Book Id"])
if book_id:
title = get_property_value(properties["Title"])
case = { 'id': result['id'], 'Book_Id': book_id, 'Image': result["cover"],
'title': title }
book_table.append(case)
if len(book_table) == 0:
print('No items with property "Book Id" found. Did you import a csv from GoodReads?')
sys.exit(0)
print('Splitting the data into chunks of 10 elements...\n')
def split(list_a, chunk_size):
for i in range(0, len(list_a), chunk_size):
yield list_a[i:i + chunk_size]
book_table = list(split(book_table, 10))
print(str(len(book_table)) + ' chunks created.')
for idy, valA in enumerate(book_table):
print('----')
print('chunk ' + str(idy) + ' in progress')
for idx, val in enumerate(valA):
if not val['Image']:
url = goodreads_url + str(val['Book_Id'])
try:
url_open = urlopen(url)
soup = BeautifulSoup(url_open, 'html.parser')
tag = soup.find("img", {"id": "coverImage"})
img = tag['src']
print(f'Updating image for: {val["title"]}')
client.pages.update(
**{
"page_id": val['id'],
"cover": {
'type': 'external',
'external': {
'url': tag['src']
}
}
}
)
except:
print(f'Image update failed for: {val["title"]}')
else:
print(str(idx + 1) + f' / 10 - Skipping "{val["title"]}", already has a cover image')
print('chunk ' + str(idy) + ' finished')