diff --git a/.github/workflows/weread.yml b/.github/workflows/weread.yml index 8d492b1c2e81..2cf12e6976ff 100644 --- a/.github/workflows/weread.yml +++ b/.github/workflows/weread.yml @@ -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: @@ -24,7 +32,7 @@ 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 == '' diff --git a/.gitignore b/.gitignore index 6d06afbdb8c8..998a4269d2fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -scripts/__pycache__/ \ No newline at end of file +scripts/__pycache__/ +.env \ No newline at end of file diff --git a/README.md b/README.md index d5812356044f..3805dacb1042 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ > 欢迎加入微信群讨论。可以讨论使用中遇到的任何问题,也可以讨论Notion使用,后续我也会在群中分享更多Notion自动化工具。 -![群](https://drive.malinkang.com/api/raw/?path=/Images/group2.jpg) +![群](https://images.malinkang.com/group.jpg) ## 捐赠 diff --git a/cover/CB_2VL42J41E5Q86ki6l32129S1_parsecover.jpg b/cover/CB_2VL42J41E5Q86ki6l32129S1_parsecover.jpg new file mode 100644 index 000000000000..3dc93f376044 Binary files /dev/null and b/cover/CB_2VL42J41E5Q86ki6l32129S1_parsecover.jpg differ diff --git a/requirements.txt b/requirements.txt index 2a44b1b0a65c..703e1abda184 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests notion-client -github-heatmap \ No newline at end of file +github-heatmap +python-dotenv \ No newline at end of file diff --git a/scripts/weread.py b/scripts/weread.py index d20433851e3a..dac78e7ab289 100644 --- a/scripts/weread.py +++ b/scripts/weread.py @@ -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" @@ -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) @@ -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) @@ -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( @@ -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( @@ -344,54 +349,62 @@ 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) 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","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: + 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是否正确") + + 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() + 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) @@ -410,9 +423,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") @@ -432,9 +443,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)