forked from malinkang/weread2notion-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweread_api.py
260 lines (226 loc) · 9.1 KB
/
weread_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import hashlib
import json
import os
import re
import requests
from requests.utils import cookiejar_from_dict
from retrying import retry
from urllib.parse import quote
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"
WEREAD_CHAPTER_INFO = "https://i.weread.qq.com/book/chapterInfos"
WEREAD_READ_INFO_URL = "https://i.weread.qq.com/book/readinfo"
WEREAD_REVIEW_LIST_URL = "https://i.weread.qq.com/review/list"
WEREAD_BOOK_INFO = "https://i.weread.qq.com/book/info"
WEREAD_READDATA_DETAIL = "https://i.weread.qq.com/readdata/detail"
WEREAD_HISTORY_URL = "https://i.weread.qq.com/readdata/summary?synckey=0"
class WeReadApi:
def __init__(self):
self.cookie = self.get_cookie()
self.session = requests.Session()
self.session.cookies = self.parse_cookie_string()
def try_get_cloud_cookie(self, 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]
)
result = cookie_str
return result
def get_cookie(self):
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 = self.try_get_cloud_cookie(url, id, password)
if not cookie or not cookie.strip():
raise Exception("没有找到cookie,请按照文档填写cookie")
return cookie
def parse_cookie_string(self):
cookies_dict = {}
# 使用正则表达式解析 cookie 字符串
pattern = re.compile(r'([^=]+)=([^;]+);?\s*')
matches = pattern.findall(self.cookie)
for key, value in matches:
cookies_dict[key] = value.encode('unicode_escape').decode('ascii')
# 直接使用 cookies_dict 创建 cookiejar
cookiejar = cookiejar_from_dict(cookies_dict)
return cookiejar
def get_bookshelf(self):
self.session.get(WEREAD_URL)
r = self.session.get(
"https://i.weread.qq.com/shelf/sync?synckey=0&teenmode=0&album=1&onlyBookid=0"
)
if r.ok:
return r.json()
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"Could not get bookshelf {r.text}")
def handle_errcode(self,errcode):
if( errcode== -2012 or errcode==-2010):
print(f"::error::微信读书Cookie过期了,请参考文档重新设置。https://mp.weixin.qq.com/s/B_mqLUZv7M1rmXRsMlBf7A")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_notebooklist(self):
"""获取笔记本列表"""
self.session.get(WEREAD_URL)
r = self.session.get(WEREAD_NOTEBOOKS_URL)
if r.ok:
data = r.json()
books = data.get("books")
books.sort(key=lambda x: x["sort"])
return books
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"Could not get notebook list {r.text}")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_bookinfo(self, bookId):
"""获取书的详情"""
self.session.get(WEREAD_URL)
params = dict(bookId=bookId)
r = self.session.get(WEREAD_BOOK_INFO, params=params)
if r.ok:
return r.json()
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
print(f"Could not get book info {r.text}")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_bookmark_list(self, bookId):
self.session.get(WEREAD_URL)
params = dict(bookId=bookId)
r = self.session.get(WEREAD_BOOKMARKLIST_URL, params=params)
if r.ok:
with open("bookmark.json","w") as f:
f.write(json.dumps(r.json(),indent=4,ensure_ascii=False))
bookmarks = r.json().get("updated")
return bookmarks
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"Could not get {bookId} bookmark list")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_read_info(self, bookId):
self.session.get(WEREAD_URL)
params = dict(
noteCount=1,
readingDetail=1,
finishedBookIndex=1,
readingBookCount=1,
readingBookIndex=1,
finishedBookCount=1,
bookId=bookId,
finishedDate=1,
)
headers = {
"baseapi":"32",
"appver":"8.2.5.10163885",
"basever":"8.2.5.10163885",
"osver":"12",
"User-Agent": "WeRead/8.2.5 WRBrand/xiaomi Dalvik/2.1.0 (Linux; U; Android 12; Redmi Note 7 Pro Build/SQ3A.220705.004)",
}
r = self.session.get(WEREAD_READ_INFO_URL,headers=headers, params=params)
if r.ok:
return r.json()
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"get {bookId} read info failed {r.text}")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_review_list(self, bookId):
self.session.get(WEREAD_URL)
params = dict(bookId=bookId, listType=11, mine=1, syncKey=0)
r = self.session.get(WEREAD_REVIEW_LIST_URL, params=params)
if r.ok:
reviews = r.json().get("reviews")
reviews = list(map(lambda x: x.get("review"), reviews))
reviews = [
{"chapterUid": 1000000, **x} if x.get("type") == 4 else x
for x in reviews
]
return reviews
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"get {bookId} review list failed {r.text}")
def get_api_data(self):
self.session.get(WEREAD_URL)
r = self.session.get(WEREAD_HISTORY_URL)
if r.ok:
return r.json()
else:
errcode = r.json().get("errcode",0)
self.handle_errcode(errcode)
raise Exception(f"get history data failed {r.text}")
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_chapter_info(self, bookId):
self.session.get(WEREAD_URL)
body = {"bookIds": [bookId], "synckeys": [0], "teenmode": 0}
r = self.session.post(WEREAD_CHAPTER_INFO, json=body)
if (
r.ok
and "data" in r.json()
and len(r.json()["data"]) == 1
and "updated" in r.json()["data"][0]
):
update = r.json()["data"][0]["updated"]
update.append(
{
"chapterUid": 1000000,
"chapterIdx": 1000000,
"updateTime": 1683825006,
"readAhead": 0,
"title": "点评",
"level": 1,
}
)
return {item["chapterUid"]: item for item in update}
else:
raise Exception(f"get {bookId} chapter info failed {r.text}")
def transform_id(self, book_id):
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(self, book_id):
md5 = hashlib.md5()
md5.update(book_id.encode("utf-8"))
digest = md5.hexdigest()
result = digest[0:3]
code, transformed_ids = self.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()
md5.update(result.encode("utf-8"))
result += md5.hexdigest()[0:3]
return result
def get_url(self, book_id):
return f"https://weread.qq.com/web/reader/{self.calculate_book_str_id(book_id)}"