Skip to content

Commit

Permalink
[style] refactor methods in notion.py
Browse files Browse the repository at this point in the history
  • Loading branch information
faithk7 committed Nov 16, 2024
1 parent fda8047 commit 9df0b41
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 121 deletions.
66 changes: 66 additions & 0 deletions src/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
WEREAD_READ_INFO_URL,
WEREAD_REVIEW_LIST_URL,
)
from util import get_callout


def get_bookmark_list(session: requests.Session, bookId: str) -> Optional[List[Dict]]:
Expand Down Expand Up @@ -134,3 +135,68 @@ def get_quote(content: str) -> Dict:
"color": "default",
},
}


def get_children(
chapter: Optional[Dict[int, Dict]], summary: List[Dict], bookmark_list: List[Dict]
) -> Tuple[List[Dict], Dict[int, Dict]]:
children = []
grandchild = {}

if chapter is not None:
# 添加目录
children.append(get_table_of_contents())
d = {}
for data in bookmark_list:
chapterUid = data.get("chapterUid", 1)
if chapterUid not in d:
d[chapterUid] = []
d[chapterUid].append(data)

for key, value in d.items():
if key in chapter:
# 添加章节
children.append(
get_heading(
chapter.get(key).get("level"), chapter.get(key).get("title")
)
)

for i in value:
callout = get_callout(
i.get("markText"),
data.get("style"),
i.get("colorStyle"),
i.get("reviewId"),
)
children.append(callout)

if i.get("abstract") is not None and i.get("abstract") != "":
quote = get_quote(i.get("abstract"))
grandchild[len(children) - 1] = quote

else:
# 如果没有章节信息
for data in bookmark_list:
children.append(
get_callout(
data.get("markText"),
data.get("style"),
data.get("colorStyle"),
data.get("reviewId"),
)
)

if summary is not None and len(summary) > 0:
children.append(get_heading(1, "点评"))
for i in summary:
children.append(
get_callout(
i.get("review").get("content"),
i.get("style"),
i.get("colorStyle"),
i.get("review").get("reviewId"),
)
)

return children, grandchild
1 change: 0 additions & 1 deletion src/logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging

# Initialize logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
10 changes: 2 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
get_bookinfo,
get_bookmark_list,
get_chapter_info,
get_children,
get_notebooklist,
get_review_list,
)
from constants import WEREAD_URL
from notion import (
add_children,
add_grandchild,
check,
get_children,
get_sort,
insert_to_notion,
)
from notion import add_children, add_grandchild, check, get_sort, insert_to_notion
from util import parse_cookie_string

if __name__ == "__main__":
Expand Down
115 changes: 3 additions & 112 deletions src/notion.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import hashlib
import re
import time
from datetime import datetime
from typing import Dict, List, Optional, Tuple
from typing import Dict, List, Optional

import requests
from notion_client import Client

from book import get_heading, get_quote, get_read_info, get_table_of_contents
from book import get_read_info
from logger import logger
from util import get_callout
from util import calculate_book_str_id


def check(client: Client, database_id: str, bookId: str) -> None:
Expand Down Expand Up @@ -126,110 +124,3 @@ def get_sort(client: Client, database_id: str) -> int:
return response.get("results")[0].get("properties").get("Sort").get("number")

return 0


def get_children(
chapter: Optional[Dict[int, Dict]], summary: List[Dict], bookmark_list: List[Dict]
) -> Tuple[List[Dict], Dict[int, Dict]]:
children = []
grandchild = {}

if chapter is not None:
# 添加目录
children.append(get_table_of_contents())
d = {}
for data in bookmark_list:
chapterUid = data.get("chapterUid", 1)
if chapterUid not in d:
d[chapterUid] = []
d[chapterUid].append(data)

for key, value in d.items():
if key in chapter:
# 添加章节
children.append(
get_heading(
chapter.get(key).get("level"), chapter.get(key).get("title")
)
)

for i in value:
callout = get_callout(
i.get("markText"),
data.get("style"),
i.get("colorStyle"),
i.get("reviewId"),
)
children.append(callout)

if i.get("abstract") is not None and i.get("abstract") != "":
quote = get_quote(i.get("abstract"))
grandchild[len(children) - 1] = quote

else:
# 如果没有章节信息
for data in bookmark_list:
children.append(
get_callout(
data.get("markText"),
data.get("style"),
data.get("colorStyle"),
data.get("reviewId"),
)
)

if summary is not None and len(summary) > 0:
children.append(get_heading(1, "点评"))
for i in summary:
children.append(
get_callout(
i.get("review").get("content"),
i.get("style"),
i.get("colorStyle"),
i.get("review").get("reviewId"),
)
)

return children, grandchild


def transform_id(book_id: str) -> Tuple[str, List[str]]:
id_length = len(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"))
return "3", ary

result = ""
for i in range(id_length):
result += format(ord(book_id[i]), "x")
return "4", [result]


def calculate_book_str_id(book_id: str) -> str:
md5 = hashlib.md5(usedforsecurity=False)
md5.update(book_id.encode("utf-8"))
digest = md5.hexdigest()
result = digest[0:3]
code, transformed_ids = transform_id(book_id)
result += code + "2" + digest[-2:]

for i in range(len(transformed_ids)):
hex_length_str = format(len(transformed_ids[i]), "x")
if len(hex_length_str) == 1:
hex_length_str = "0" + hex_length_str

result += hex_length_str + transformed_ids[i]

if i < len(transformed_ids) - 1:
result += "g"

if len(result) < 20:
result += digest[0 : 20 - len(result)]

md5 = hashlib.md5(usedforsecurity=False)
md5.update(result.encode("utf-8"))
result += md5.hexdigest()[0:3]
return result
45 changes: 45 additions & 0 deletions src/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
import hashlib
import re
from http.cookies import SimpleCookie
from typing import List, Tuple

from requests.utils import cookiejar_from_dict


def transform_id(book_id: str) -> Tuple[str, List[str]]:
id_length = len(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"))
return "3", ary

result = ""
for i in range(id_length):
result += format(ord(book_id[i]), "x")
return "4", [result]


def calculate_book_str_id(book_id: str) -> str:
md5 = hashlib.md5(usedforsecurity=False)
md5.update(book_id.encode("utf-8"))
digest = md5.hexdigest()
result = digest[0:3]
code, transformed_ids = transform_id(book_id)
result += code + "2" + digest[-2:]

for i in range(len(transformed_ids)):
hex_length_str = format(len(transformed_ids[i]), "x")
if len(hex_length_str) == 1:
hex_length_str = "0" + hex_length_str

result += hex_length_str + transformed_ids[i]

if i < len(transformed_ids) - 1:
result += "g"

if len(result) < 20:
result += digest[0 : 20 - len(result)]

md5 = hashlib.md5(usedforsecurity=False)
md5.update(result.encode("utf-8"))
result += md5.hexdigest()[0:3]
return result


def parse_cookie_string(cookie_string):
cookie = SimpleCookie()
cookie.load(cookie_string)
Expand Down

0 comments on commit 9df0b41

Please sign in to comment.