Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Sep 29, 2024
1 parent 3477930 commit ce29f01
Show file tree
Hide file tree
Showing 39 changed files with 1,889 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/read_time.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ jobs:
git push || echo "nothing to push"
- name: read time sync
run: |
python -u scripts/read_time.py
read_time
4 changes: 2 additions & 2 deletions .github/workflows/weread.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
pip install -r requirements.txt
- name: weread book sync
run: |
python -u scripts/book.py
book
- name: weread sync
run: |
python -u scripts/weread.py
weread
Empty file.
4 changes: 4 additions & 0 deletions build/lib/weread2notionpro/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from book import main

if __name__ == "__main__":
main()
61 changes: 23 additions & 38 deletions scripts/book.py → build/lib/weread2notionpro/book.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
import argparse
from datetime import datetime, timedelta
import os

import pendulum
import requests
from notion_helper import NotionHelper

from weread_api import WeReadApi
import utils
from config import book_properties_type_dict, tz
from weread2notionpro.notion_helper import NotionHelper
from weread2notionpro.weread_api import WeReadApi
from weread2notionpro import utils
from weread2notionpro.config import book_properties_type_dict, tz
from retrying import retry


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": "⭐️⭐️⭐️⭐️⭐️"}


@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_douban_url(isbn):
print(f"get_douban_url {isbn} ")
params = {"query": isbn, "page": "1", "category": "book"}
r = requests.get("https://neodb.social/api/catalog/search", params=params)
if r.ok:
books = r.json().get("data")
if books is None or len(books) == 0:
return None
results = list(filter(lambda x: x.get("isbn") == isbn, books))
if len(results) == 0:
return None
result = results[0]
urls = list(
filter(
lambda x: x.get("url").startswith("https://book.douban.com"),
result.get("external_resources", []),
)
)
if len(urls) == 0:
return None
return urls[0].get("url")


def insert_book_to_notion(books, index, bookId):
Expand Down Expand Up @@ -89,10 +64,10 @@ def insert_book_to_notion(books, index, bookId):
cover = BOOK_ICON_URL
if bookId not in notion_books:
isbn = book.get("isbn")
if isbn and isbn.strip():
douban_url = get_douban_url(isbn)
if douban_url:
book["douban_url"] = douban_url
# if isbn and isbn.strip():
# douban_url = get_douban_url(isbn)
# if douban_url:
# book["douban_url"] = douban_url
book["书名"] = book.get("title")
book["BookId"] = book.get("bookId")
book["ISBN"] = book.get("isbn")
Expand All @@ -118,7 +93,9 @@ def insert_book_to_notion(books, index, bookId):
pendulum.from_timestamp(book.get("时间"), tz="Asia/Shanghai"),
)

print(f"::notice::正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。")
print(
f"::notice::正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。"
)
parent = {"database_id": notion_helper.book_database_id, "type": "database_id"}
result = None
if bookId in notion_books:
Expand Down Expand Up @@ -186,9 +163,13 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
)


if __name__ == "__main__":
weread_api = WeReadApi()
notion_helper = NotionHelper()
weread_api = WeReadApi()
notion_helper = NotionHelper()
archive_dict = {}
notion_books = []


def main():
notion_books = notion_helper.get_all_book()
bookshelf_books = weread_api.get_bookshelf()
bookProgress = bookshelf_books.get("bookProgress")
Expand Down Expand Up @@ -220,3 +201,7 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
books = list((set(notebooks) | set(books)) - set(not_need_sync))
for index, bookId in enumerate(books):
insert_book_to_notion(books, index, bookId)


if __name__ == "__main__":
main()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from notion_client import Client
from retrying import retry
from datetime import timedelta
from dotenv import load_dotenv
from utils import (
from weread2notionpro.utils import (
format_date,
get_date,
get_first_and_last_day_of_month,
Expand All @@ -22,7 +21,6 @@
get_property_value,
)

load_dotenv()
TAG_ICON_URL = "https://www.notion.so/icons/tag_gray.svg"
USER_ICON_URL = "https://www.notion.so/icons/user-circle-filled_gray.svg"
TARGET_ICON_URL = "https://www.notion.so/icons/target_red.svg"
Expand Down
20 changes: 10 additions & 10 deletions scripts/read_time.py → build/lib/weread2notionpro/read_time.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import argparse
from datetime import datetime
from datetime import timedelta
import json
import os
import time

import pendulum

from notion_helper import NotionHelper
from weread_api import WeReadApi
from utils import (
from weread2notionpro.weread_api import WeReadApi
from weread2notionpro.notion_helper import NotionHelper
from weread2notionpro.utils import (
format_date,
get_date,
get_icon,
get_number,
get_relation,
get_title,
get_embed,
)


Expand Down Expand Up @@ -81,9 +77,11 @@ def get_file():
return None

HEATMAP_GUIDE = "https://mp.weixin.qq.com/s?__biz=MzI1OTcxOTI4NA==&mid=2247484145&idx=1&sn=81752852420b9153fc292b7873217651&chksm=ea75ebeadd0262fc65df100370d3f983ba2e52e2fcde2deb1ed49343fbb10645a77570656728&token=157143379&lang=zh_CN#rd"
if __name__ == "__main__":
notion_helper = NotionHelper()
weread_api = WeReadApi()


notion_helper = NotionHelper()
weread_api = WeReadApi()
def main():
image_file = get_file()
if image_file:
image_url = f"https://raw.githubusercontent.com/{os.getenv('REPOSITORY')}/{os.getenv('REF').split('/')[-1]}/OUT_FOLDER/{image_file}"
Expand Down Expand Up @@ -114,3 +112,5 @@ def get_file():
insert_to_notion(page_id=id, timestamp=timestamp, duration=value)
for key, value in readTimes.items():
insert_to_notion(None, int(key), value)
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions scripts/utils.py → build/lib/weread2notionpro/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import requests
import base64
from config import (
from weread2notionpro.config import (
RICH_TEXT,
URL,
RELATION,
Expand Down Expand Up @@ -315,7 +315,7 @@ def calculate_book_str_id(book_id):

def transform_id(book_id):
id_length = len(book_id)
if re.match("^\d*$", book_id):
if re.match("^\\d*$", book_id):
ary = []
for i in range(0, id_length, 9):
ary.append(format(int(book_id[i : min(i + 9, id_length)]), "x"))
Expand Down
25 changes: 10 additions & 15 deletions scripts/weread.py → build/lib/weread2notionpro/weread.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import argparse
import os
import requests
from weread2notionpro.notion_helper import NotionHelper
from weread2notionpro.weread_api import WeReadApi

from notion_helper import NotionHelper
from weread_api import WeReadApi

from utils import (
from weread2notionpro.utils import (
get_callout,
get_heading,
get_number,
Expand Down Expand Up @@ -229,14 +225,9 @@ def append_blocks_to_notion(id, blocks, after, contents):
l.append(content)
return l


if __name__ == "__main__":
parser = argparse.ArgumentParser()
options = parser.parse_args()
branch = os.getenv("REF").split("/")[-1]
repository = os.getenv("REPOSITORY")
weread_api = WeReadApi()
notion_helper = NotionHelper()
weread_api = WeReadApi()
notion_helper = NotionHelper()
def main():
notion_books = notion_helper.get_all_book()
books = weread_api.get_notebooklist()
if books != None:
Expand All @@ -260,3 +251,7 @@ def append_blocks_to_notion(id, blocks, after, contents):
"Sort":get_number(sort)
}
notion_helper.update_book_page(page_id=pageId,properties=properties)

if __name__ == "__main__":
main()

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import hashlib
from http.cookies import SimpleCookie
import os
import re

import requests
from requests.utils import cookiejar_from_dict
from http.cookies import SimpleCookie
from retrying import retry

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"
Expand Down Expand Up @@ -197,7 +194,7 @@ def get_chapter_info(self, bookId):

def transform_id(self, book_id):
id_length = len(book_id)
if re.match("^\d*$", book_id):
if re.match("^\\d*$", book_id):
ary = []
for i in range(0, id_length, 9):
ary.append(format(int(book_id[i : min(i + 9, id_length)]), "x"))
Expand Down
Binary file added dist/weread2notionpro-0.1.1-py3-none-any.whl
Binary file not shown.
Binary file added dist/weread2notionpro-0.1.1-py3.12.egg
Binary file not shown.
Binary file added dist/weread2notionpro-0.1.1.tar.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ notion-client
github-heatmap
retrying
pendulum
python-dotenv
python-dotenv
weread2notionpro
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from setuptools import setup, find_packages

setup(
name="weread2notionpro",
version="0.1.1",
packages=find_packages(),
install_requires=[
"requests",
"pendulum",
"retrying",
"notion-client",
"github-heatmap",
],
entry_points={
"console_scripts": [
"book = weread2notionpro.book:main",
"weread = weread2notionpro.weread:main",
"read_time = weread2notionpro.read_time:main",
],
},
author="malinkang",
author_email="[email protected]",
description="自动将微信读书笔记和阅读记录同步到Notion",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/malinkang/weread2notion-pro",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.6",
)
57 changes: 57 additions & 0 deletions weread2notionpro.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Metadata-Version: 2.1
Name: weread2notionpro
Version: 0.1.1
Summary: 自动将微信读书笔记和阅读记录同步到Notion
Home-page: https://github.com/malinkang/weread2notion-pro
Author: malinkang
Author-email: [email protected]
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: requests
Requires-Dist: pendulum
Requires-Dist: retrying
Requires-Dist: notion-client
Requires-Dist: github-heatmap

# 将微信读书划线和笔记同步到Notion


本项目通过Github Action每天定时同步微信读书划线到Notion。

预览效果:[https://malinkang.notion.site/malinkang/534a7684b30e4a879269313f437f2185](https://malinkang.notion.site/9a311b7413b74c8788752249edd0b256?pvs=25)


## 使用

> [!IMPORTANT]
> 关注公众号获取教程,后续有更新也会第一时间在公众号里同步。

![扫码_搜索联合传播样式-标准色版](https://github.com/malinkang/weread2notion/assets/3365208/191900c6-958e-4f9b-908d-a40a54889b5e)


## 群
> [!IMPORTANT]
> 欢迎加入群讨论。可以讨论使用中遇到的任何问题,也可以讨论Notion使用,后续我也会在群中分享更多Notion自动化工具。微信群失效的话可以添加我的微信malinkang,我拉你入群。

| 微信群 | QQ群 |
| --- | --- |
| <div align="center"><img src="https://github.com/malinkang/weread2notion/assets/3365208/e28d1fb8-a861-47e1-b5e8-b25f17a6a285" ></div> | <div align="center"><img src="https://images.malinkang.com/2024/04/b225b17d60670e4a6ff3459bbde80d28.jpg" width="50%"></div> |


## 捐赠

如果你觉得本项目帮助了你,请作者喝一杯咖啡,你的支持是作者最大的动力。本项目会持续更新。

| 支付宝支付 | 微信支付 |
| --- | --- |
| <div align="center"><img src="https://images.malinkang.com/2024/03/7fd0feb1145f19fab3821ff1d4631f85.jpg" width="50%"></div> | <div align="center"><img src="https://images.malinkang.com/2024/03/d34f577490a32d4440c8a22f57af41da.jpg" width="50%"></div> |

## 其他项目
* [WeRead2Notion-Pro](https://github.com/malinkang/weread2notion-pro)
* [WeRead2Notion](https://github.com/malinkang/weread2notion)
* [Podcast2Notion](https://github.com/malinkang/podcast2notion)
* [Douban2Notion](https://github.com/malinkang/douban2notion)
* [Keep2Notion](https://github.com/malinkang/keep2notion)
17 changes: 17 additions & 0 deletions weread2notionpro.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
README.md
setup.py
weread2notionpro/__init__.py
weread2notionpro/__main__.py
weread2notionpro/book.py
weread2notionpro/config.py
weread2notionpro/notion_helper.py
weread2notionpro/read_time.py
weread2notionpro/utils.py
weread2notionpro/weread.py
weread2notionpro/weread_api.py
weread2notionpro.egg-info/PKG-INFO
weread2notionpro.egg-info/SOURCES.txt
weread2notionpro.egg-info/dependency_links.txt
weread2notionpro.egg-info/entry_points.txt
weread2notionpro.egg-info/requires.txt
weread2notionpro.egg-info/top_level.txt
1 change: 1 addition & 0 deletions weread2notionpro.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions weread2notionpro.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[console_scripts]
book = weread2notionpro.book:main
read_time = weread2notionpro.read_time:main
weread = weread2notionpro.weread:main
Loading

0 comments on commit ce29f01

Please sign in to comment.