Skip to content

Commit

Permalink
添加豆瓣
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Jan 25, 2024
1 parent e36eea5 commit 41dbe50
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/weread.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
NOTION_PAGE: ${{ secrets.NOTION_PAGE }}
WEREAD_COOKIE: ${{ secrets.WEREAD_COOKIE }}
DOUBAN_NAME: ${{ secrets.DOUBAN_NAME }}
BOOK_DATABASE_NAME: ${{ vars.BOOK_DATABASE_NAME }}
AUTHOR_DATABASE_NAME: ${{ vars.AUTHOR_DATABASE_NAME }}
CATEGORY_DATABASE_NAME: ${{ vars.CATEGORY_DATABASE_NAME }}
Expand All @@ -41,6 +42,9 @@ jobs:
- name: weread book sync
run: |
python -u scripts/book.py
- name: douban book sync
run: |
python -u scripts/douban.py
- name: weread sync
run: |
python -u scripts/weread.py
2 changes: 0 additions & 2 deletions OUT_FOLDER/8db68104-0045-4d64-8334-e219bb6aa007.svg

This file was deleted.

Binary file removed cover/868ec61f125b61b403a64bfdb433ee2d.jpg
Binary file not shown.
Binary file modified scripts/__pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file modified scripts/__pycache__/notion_helper.cpython-311.pyc
Binary file not shown.
Binary file modified scripts/__pycache__/utils.cpython-311.pyc
Binary file not shown.
6 changes: 2 additions & 4 deletions scripts/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
book_properties_type_dict,
)
from retrying import retry
from config import TAG_ICON_URL, USER_ICON_URL, BOOK_ICON_URL

TAG_ICON_URL = "https://www.notion.so/icons/tag_gray.svg"
USER_ICON_URL = "https://www.notion.so/icons/user-circle-filled_gray.svg"
BOOK_ICON_URL = "https://www.notion.so/icons/book_gray.svg"

rating = {"poor": "⭐️", "fair": "⭐️⭐️⭐️", "good": "⭐️⭐️⭐️⭐️⭐️"}

Expand Down Expand Up @@ -88,7 +86,7 @@ def insert_book_to_notion(books, index, bookId):
book["评分"] = book.get("newRating")
if book.get("newRatingDetail") and book.get("newRatingDetail").get("myRating"):
book["我的评分"] = rating.get(book.get("newRatingDetail").get("myRating"))
elif status=="已读":
elif status == "已读":
book["我的评分"] = "未评分"
date = None
if book.get("finishedDate"):
Expand Down
4 changes: 4 additions & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
TITLE = "title"
SELECT = "select"

TAG_ICON_URL = "https://www.notion.so/icons/tag_gray.svg"
USER_ICON_URL = "https://www.notion.so/icons/user-circle-filled_gray.svg"
BOOK_ICON_URL = "https://www.notion.so/icons/book_gray.svg"

book_properties_type_dict = {
"书名":TITLE,
"BookId":RICH_TEXT,
Expand Down
121 changes: 121 additions & 0 deletions scripts/douban.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import json
import os
import pendulum

import requests
from notion_helper import NotionHelper
from config import TAG_ICON_URL, USER_ICON_URL, BOOK_ICON_URL, book_properties_type_dict
import utils

DOUBAN_API_HOST = os.getenv("DOUBAN_API_HOST", "frodo.douban.com")
DOUBAN_API_KEY = os.getenv("DOUBAN_API_KEY", "0ac44ae016490db2204ce0a042db2916")
rating = {
1: "⭐️",
2: "⭐️⭐️",
3: "⭐️⭐️⭐️",
4: "⭐️⭐️⭐️⭐️",
5: "⭐️⭐️⭐️⭐️⭐️",
}
status = {
"mark": "想读",
"doing": "在读",
"done": "已读",
}
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
headers = {
"host": DOUBAN_API_HOST,
"authorization": f"Bearer {AUTH_TOKEN}" if AUTH_TOKEN else "",
"user-agent": "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.16(0x18001023) NetType/WIFI Language/zh_CN",
"referer": "https://servicewechat.com/wx2f9b06c1de1ccfca/84/page-frame.html",
}


def fetch_subjects(user, type_, status):
offset = 0
page = 0
url = f"https://{DOUBAN_API_HOST}/api/v2/user/{user}/interests"
total = 0
results = []
has_next = True
while has_next:
params = {
"type": type_,
"count": 50,
"status": status,
"start": offset,
"apiKey": DOUBAN_API_KEY,
}
response = requests.get(url, headers=headers, params=params)
response = response.json()
results.extend(response.get("interests"))
total = response.get("total")
print(total)
page += 1
offset = page * 50
has_next = len(results) < total
return results


if __name__ == "__main__":
douban_name = os.getenv("DOUBAN_NAME",None)
if douban_name and douban_name.strip():
notion_helper = NotionHelper()
results = []
notion_books = notion_helper.get_all_book()
notion_books = {value.get("douban_url"):value for value in notion_books.values()}
for i in status.keys():
results.extend(fetch_subjects(douban_name, "book", i))
for result in results:
book = {}
subject = result.get("subject")
url = subject.get("url")
"""获取评论"""
comment = result.get("comment")
if url in notion_books:
"""如果评论不为空并且和notion中的豆瓣短评不同则更新"""
if (comment and comment.strip()) and comment != notion_books.get(url).get("comment"):
book["豆瓣短评"] = result.get("comment")
properties = utils.get_properties(book, book_properties_type_dict)
notion_helper.update_pag2(
page_id=notion_books.get(url).get("pageId"),
properties=properties,
)
continue
book["豆瓣链接"] = url
book["书名"] = subject.get("title")
book["简介"] = subject.get("intro")
book["豆瓣短评"] = comment
book["阅读状态"] = status.get(subject.get("status"))
if result.get("rating"):
book["我的评分"] = rating(result.get("rating").get("value"))
book["作者"] = [
notion_helper.get_relation_id(
x, notion_helper.author_database_id, USER_ICON_URL
)
for x in subject.get("author")
]
if len(result.get("tags")) > 0:
book["分类"] = [
notion_helper.get_relation_id(
x, notion_helper.category_database_id, TAG_ICON_URL
)
for x in result.get("tags")
]
date_format = "YYYY-MM-DD HH:mm:ss"
dt = pendulum.from_format(
result.get("create_time"), date_format, tz="Asia/Shanghai"
)
book["日期"] = dt.int_timestamp
book["封面"] = subject.get("cover_url")
properties = utils.get_properties(book, book_properties_type_dict)
if book.get("日期"):
notion_helper.get_date_relation(
properties,
pendulum.from_timestamp(book.get("日期"), tz="Asia/Shanghai"),
)
parent = {"database_id": notion_helper.book_database_id, "type": "database_id"}
notion_helper.create_page(
parent=parent,
properties=properties,
icon=utils.get_icon(subject.get("cover_url")),
)
6 changes: 6 additions & 0 deletions scripts/notion_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ def update_page(self, page_id, properties, icon):
return self.client.pages.update(
page_id=page_id, icon=icon, properties=properties
)

@retry(stop_max_attempt_number=3, wait_fixed=5000)
def update_pag2(self, page_id, properties):
return self.client.pages.update(
page_id=page_id, properties=properties
)

@retry(stop_max_attempt_number=3, wait_fixed=5000)
def create_page(self, parent, properties, icon):
Expand Down

0 comments on commit 41dbe50

Please sign in to comment.