Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Mar 21, 2024
1 parent 4048263 commit f2f7ce2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 70 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/weread.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ jobs:
sync:
name: Sync
runs-on: ubuntu-latest
env:
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
NOTION_PAGE: ${{ secrets.NOTION_PAGE }}
NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
WEREAD_COOKIE: ${{ secrets.WEREAD_COOKIE }}
CC_URL: ${{ secrets.CC_URL }}
CC_ID: ${{ secrets.CC_ID }}
CC_PASSWORD: ${{ secrets.CC_PASSWORD }}
REF: ${{ github.ref }}
REPOSITORY: ${{ github.repository }}
YEAR: ${{ vars.YEAR }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
Expand All @@ -24,13 +32,13 @@ jobs:
pip install -r requirements.txt
- name: weread sync
run: |
python scripts/weread.py "${{secrets.WEREAD_COOKIE}}" "${{secrets.NOTION_TOKEN}}" "${{secrets.NOTION_DATABASE_ID}}" "${{ github.ref }}" "${{ github.repository }}" --styles 0 1 2 --colors 0 1 2 3 4 5 --cc_url "${{ secrets.CC_URL }}" --cc_id "${{ secrets.CC_ID }}" --cc_secret "${{ secrets.CC_PASSWORD }}"
python scripts/weread.py
- name: Set default year if not provided
run: echo "YEAR=$(date +"%Y")" >> $GITHUB_ENV
if: env.YEAR == ''
- name: weread heatmap
run: |
github_heatmap weread --weread_cookie "${{secrets.WEREAD_COOKIE}}" --year $YEAR --me "${{secrets.NAME}}" --with-animation --background-color=${{ vars.background_color||'#FFFFFF'}} --track-color=${{ vars.track_color||'#ACE7AE'}} --special-color1=${{ vars.special_color||'#69C16E'}} --special-color2=${{ vars.special_color2||'#549F57'}} --dom-color=${{ vars.dom_color||'#EBEDF0'}} --text-color=${{ vars.text_color||'#000000'}}
github_heatmap weread --weread_cookie "${{env.WEREAD_COOKIE}}" --year $YEAR --me "${{secrets.NAME}}" --with-animation --background-color=${{ vars.background_color||'#FFFFFF'}} --track-color=${{ vars.track_color||'#ACE7AE'}} --special-color1=${{ vars.special_color||'#69C16E'}} --special-color2=${{ vars.special_color2||'#549F57'}} --dom-color=${{ vars.dom_color||'#EBEDF0'}} --text-color=${{ vars.text_color||'#000000'}}
- name: push
run: |
git config --local user.email "[email protected]"
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scripts/__pycache__/
scripts/__pycache__/
.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
> 欢迎加入微信群讨论。可以讨论使用中遇到的任何问题,也可以讨论Notion使用,后续我也会在群中分享更多Notion自动化工具。

![](https://drive.malinkang.com/api/raw/?path=/Images/group2.jpg)
![](https://images.malinkang.com/group.jpg)

## 捐赠

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
notion-client
github-heatmap
github-heatmap
python-dotenv
152 changes: 89 additions & 63 deletions scripts/weread.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@
from http.cookies import SimpleCookie
from datetime import datetime
import hashlib

from utils import get_callout, get_date, get_file, get_heading, get_icon, get_multi_select, get_number, get_quote, get_rich_text, get_select, get_table_of_contents, get_title, get_url

from dotenv import load_dotenv
import os
from utils import (
get_callout,
get_date,
get_file,
get_heading,
get_icon,
get_multi_select,
get_number,
get_quote,
get_rich_text,
get_select,
get_table_of_contents,
get_title,
get_url,
)
load_dotenv()
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 @@ -112,17 +127,19 @@ def insert_to_notion(bookName, bookId, cover, sort, author, isbn, rating, catego
time.sleep(0.3)
parent = {"database_id": database_id, "type": "database_id"}
properties = {
"BookName":get_title(bookName),
"BookName": get_title(bookName),
"BookId": get_rich_text(bookId),
"ISBN": get_rich_text(isbn),
"URL": get_url(f"https://weread.qq.com/web/reader/{calculate_book_str_id(bookId)}"),
"URL": get_url(
f"https://weread.qq.com/web/reader/{calculate_book_str_id(bookId)}"
),
"Author": get_rich_text(author),
"Sort": get_number(sort),
"Rating": get_number(rating),
"Cover": get_file(cover),
}
if categories != None:
properties["Categories"] =get_multi_select(categories)
properties["Categories"] = get_multi_select(categories)
read_info = get_read_info(bookId=bookId)
if read_info != None:
markedStatus = read_info.get("markedStatus", 0)
Expand All @@ -139,9 +156,11 @@ def insert_to_notion(bookName, bookId, cover, sort, author, isbn, rating, catego
properties["ReadingTime"] = get_rich_text(format_time)
properties["Progress"] = get_number(readingProgress)
if "finishedDate" in read_info:
properties["Date"] = get_date(datetime.utcfromtimestamp(
read_info.get("finishedDate")
).strftime("%Y-%m-%d %H:%M:%S"))
properties["Date"] = get_date(
datetime.utcfromtimestamp(read_info.get("finishedDate")).strftime(
"%Y-%m-%d %H:%M:%S"
)
)

if cover.startswith("http"):
icon = get_icon(cover)
Expand Down Expand Up @@ -220,11 +239,6 @@ def get_children(chapter, summary, bookmark_list):
)
)
for i in value:
if data.get("reviewId") == None and "style" in i and "colorStyle" in i:
if i.get("style") not in styles:
continue
if i.get("colorStyle") not in colors:
continue
markText = i.get("markText")
for j in range(0, len(markText) // 2000 + 1):
children.append(
Expand All @@ -242,15 +256,6 @@ def get_children(chapter, summary, bookmark_list):
else:
# 如果没有章节信息
for data in bookmark_list:
if (
data.get("reviewId") == None
and "style" in data
and "colorStyle" in data
):
if data.get("style") not in styles:
continue
if data.get("colorStyle") not in colors:
continue
markText = data.get("markText")
for i in range(0, len(markText) // 2000 + 1):
children.append(
Expand Down Expand Up @@ -344,54 +349,72 @@ def download_image(url, save_dir="cover"):
return save_path


def try_get_cloud_cookie(cc_url, cc_id, cc_secret):
if cc_url == "" or cc_id == "" or cc_secret == "":
return ""
req_url = cc_url + "/get/" + cc_id
result = ""
data = {"password": cc_secret}
def try_get_cloud_cookie(url, id, password):
if url.endswith("/"):
url = url[:-1]
req_url = f"{url}/get/{id}"
data = {"password": password}
result = None
response = requests.post(req_url, data=data)
print(response.text)
if response.status_code == 200:
data = response.json()
cookie_data = data.get("cookie_data")
if cookie_data and "weread.qq.com" in cookie_data:
cookies = cookie_data["weread.qq.com"]
cookie_str = "; ".join([f"{cookie['name']}={cookie['value']}" for cookie in cookies])
cookie_str = "; ".join(
[f"{cookie['name']}={cookie['value']}" for cookie in cookies]
)
result = cookie_str
return result


def get_cookie():
url = os.getenv("CC_URL")
if not url:
url = "https://cookiecloud.malinkang.com/"
id = os.getenv("CC_ID")
password = os.getenv("CC_PASSWORD")
cookie = os.getenv("WEREAD_COOKIE")
if url and id and password:
cookie = try_get_cloud_cookie(url, id, password)
if not cookie or not cookie.strip():
raise Exception("没有找到cookie,请按照文档填写cookie")
return cookie



def extract_page_id():
url = os.getenv("NOTION_PAGE")
if not url:
url = os.getenv("NOTION_DATABASE_ID")
if not url:
raise Exception("没有找到NOTION_PAGE,请按照文档填写")
# 正则表达式匹配 32 个字符的 Notion page_id
match = re.search(
r"([a-f0-9]{32}|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})",
url,
)
if match:
return match.group(0)
else:
raise Exception(f"获取NotionID失败,请检查输入的Url是否正确")



def write_cookie_id(cookie):
env_file = os.getenv('GITHUB_ENV')
# 将值写入环境文件
with open(env_file, "a") as file:
file.write(f"WEREAD_COOKIE={cookie}\n")

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("weread_cookie")
parser.add_argument("notion_token")
parser.add_argument("database_id")
parser.add_argument("ref")
parser.add_argument("repository")
parser.add_argument("--styles", nargs="+", type=int, help="划线样式")
parser.add_argument("--colors", nargs="+", type=int, help="划线颜色")
parser.add_argument("--cc_url", nargs="+", type=str, help="CookieCloud url")
parser.add_argument("--cc_id", nargs="+", type=str, help="CookieCloud id")
parser.add_argument("--cc_secret", nargs="+", type=str, help="CookieCloud secret")
options = parser.parse_args()
weread_cookie = options.weread_cookie
database_id = options.database_id
notion_token = options.notion_token
# 如果 cc 相关的配置全部不为空,则启用 cc,重新获取 weread_cookie
if options.cc_url and options.cc_id and options.cc_secret:
cc_cookie = try_get_cloud_cookie(options.cc_url[0], options.cc_id[0], options.cc_secret[0])
if cc_cookie != "":
print("use cloud cookie")
weread_cookie = cc_cookie
else:
print("use local cookie")

ref = options.ref
branch = ref.split("/")[-1]
repository = options.repository
styles = options.styles
colors = options.colors
weread_cookie = get_cookie()
write_cookie_id(weread_cookie)
database_id = extract_page_id()
notion_token = os.getenv("NOTION_TOKEN")
session = requests.Session()
session.cookies = parse_cookie_string(weread_cookie)
client = Client(auth=notion_token, log_level=logging.ERROR)
Expand All @@ -410,9 +433,7 @@ def try_get_cloud_cookie(cc_url, cc_id, cc_secret):
cover += ".jpg"
if cover.startswith("http") and not cover.endswith(".jpg"):
path = download_image(cover)
cover = (
f"https://raw.githubusercontent.com/{repository}/{branch}/{path}"
)
cover = f"https://raw.githubusercontent.com/{os.getenv('repository')}/{os.getenv('ref').split('/')[-1]}/{path}"
bookId = book.get("bookId")
author = book.get("author")
categories = book.get("categories")
Expand All @@ -432,9 +453,14 @@ def try_get_cloud_cookie(cc_url, cc_id, cc_secret):
bookmark_list,
key=lambda x: (
x.get("chapterUid", 1),
0
if (x.get("range", "") == "" or x.get("range").split("-")[0] == "")
else int(x.get("range").split("-")[0]),
(
0
if (
x.get("range", "") == ""
or x.get("range").split("-")[0] == ""
)
else int(x.get("range").split("-")[0])
),
),
)
children, grandchild = get_children(chapter, summary, bookmark_list)
Expand Down

0 comments on commit f2f7ce2

Please sign in to comment.