Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Apr 17, 2023
0 parents commit 14e7eaa
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/weread.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: douban sync

on:
workflow_dispatch:
jobs:
sync:
name: Sync
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: douban sync
run: |
python scripts/notion/douban.py "${{secrets.WEREAD_COOKIE}}" "${{secrets.NOTION_TOKEN}}" "${{secrets.NOTION_DATABASE_ID}}"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
##
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
notion-client
113 changes: 113 additions & 0 deletions weread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@


import argparse
import json
import logging
import time
from notion_client import Client
import requests
from requests.utils import cookiejar_from_dict
from http.cookies import SimpleCookie
from datetime import datetime

WEREAD_URL = "https://weread.qq.com/"
WEREAD_NOTEBOOKS_URL = "https://i.weread.qq.com/user/notebooks"
WEREAD_BOOKMARKLIST_URL = "https://i.weread.qq.com/book/bookmarklist"

def parse_cookie_string(cookie_string):
cookie = SimpleCookie()
cookie.load(cookie_string)
cookies_dict = {}
cookiejar = None
for key, morsel in cookie.items():
cookies_dict[key] = morsel.value
cookiejar = cookiejar_from_dict(
cookies_dict, cookiejar=None, overwrite=True
)
return cookiejar


def get_hot(bookName,cover, bookId):
"""获取热门划线"""
params = dict(bookId=bookId)
r = session.get(WEREAD_BOOKMARKLIST_URL, params=params)
if r.ok:
datas = r.json()["updated"]
for data in datas:
if not check(data["bookmarkId"]):
insert_to_notion(bookName,cover, data)
else:
print("已经插入过了")


def check(bookmarkId):
"""检查是否已经插入过"""
filter = {
"property": "BookmarkId",
"rich_text": {
"equals": bookmarkId
}
}
response = client.databases.query(database_id=database_id,filter=filter)
return len(response["results"]) > 0



def insert_to_notion(bookName,cover, data):
time.sleep(0.3)
"""插入到notion"""
parent = {
"database_id": database_id,
"type": "database_id"
}

properties = {
"BookName": {"title": [{"type": "text", "text": {"content": data["markText"]}}]},
"BookId": {"rich_text": [{"type": "text", "text": {"content": data["bookId"]}}]},
"BookmarkId": {"rich_text": [{"type": "text", "text": {"content": data["bookmarkId"]}}]},
"MarkText": {"rich_text": [{"type": "text", "text": {"content": bookName}}]},
"Type": {"select": {"name": str(data["type"])}},
"Cover":{"files":[{"type":"external","name":"Cover","external":{"url":cover}}]},

}
if("createTime" in data):
date = datetime.utcfromtimestamp(data["createTime"]).strftime("%Y-%m-%d %H:%M:%S")
properties["Date"] = {"date": {"start":date,"time_zone": "Asia/Shanghai"}}
if("style" in data):
properties["Style"] = {"select": {"name": str(data["style"])}}
client.pages.create(parent=parent, properties=properties)


def get_notebooklist():
"""获取笔记本列表"""
r = session.get(WEREAD_NOTEBOOKS_URL)
books = []
with open("notebooks.json", "w", encoding="utf-8") as f:
f.write(r.text)
if r.ok:
data = r.json()
books = data["books"]
for book in books:
title = book["book"]["title"]
cover = book["book"]["cover"]
bookId = book["book"]["bookId"]
get_hot(title,cover,bookId)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("weread_cookie")
parser.add_argument("notion_token")
parser.add_argument("database_id")
options = parser.parse_args()
weread_cookie = options.weread_cookie
database_id = options.database_id
notion_token = options.notion_token
session = requests.Session()
session.cookies = parse_cookie_string(weread_cookie)
client = Client(
auth=notion_token,
log_level=logging.DEBUG
)
session.get(WEREAD_URL)
get_notebooklist()

0 comments on commit 14e7eaa

Please sign in to comment.