This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbilicomic_old.py
268 lines (228 loc) · 8.24 KB
/
bilicomic_old.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
261
262
263
264
265
266
267
268
# encoding:UTF-8
# python3.6
# support by BiliApi(https://api.kaaass.net/biliapi/)
import tempfile
import io
import json
import os
import zipfile
import requests
import threading
import queue
import time
import toml
conf = 'config.toml'
workDir = os.getcwd()
# access_key = "59f81208b0b95a55c2f99e4e7eddc461"
# appkey = "cc8617fd6961e070"
# comicId = 26399
# beginId = 0
# endId = 99999999999
with open(conf, encoding="utf-8") as f:
dict_conf = toml.load(f)
user = dict_conf['user']['user']
passwd = dict_conf['user']['passwd']
access_key = dict_conf['user']['access_key']
appkey = dict_conf['user']['appkey']
comicId = dict_conf['comic']['comicId']
beginId = int(dict_conf['comic']['beginId'])
endId = int(dict_conf['comic']['endId'])
if access_key != "":
payload = {'access_key': access_key}
r = requests.get(
'https://api.kaaass.net/biliapi/user/info', params=payload)
if r.status_code == 200:
isLogin = True
requests.get(
'https://api.kaaass.net/biliapi/user/refreshToken', params=payload)
else:
isLogin = False
else:
isLogin = False
if not isLogin:
if user == "" or passwd == "":
print("access_key已失效,且缺少用户信息(user,passwd),无法登录获取acess_key")
input('按任意键退出')
exit()
data = {'user': user, 'passwd': passwd}
r = requests.post('https://api.kaaass.net/biliapi/user/login', data=data)
if r.status_code == 200:
result = r.json()
access_key = result['access_key']
dict_conf['user']['access_key'] = access_key
with open(conf, "w", encoding="utf-8") as f:
toml.dump(dict_conf, f)
else:
print("access_key已失效,且用户信息(user,passwd)错误,无法登录获取acess_key")
input('按任意键退出')
exit()
headers = {
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'user-agent': "Mozilla/5.0 BiliComic/2.0.3",
'Host': "manga.bilibili.com",
}
getHeaders = {
'User-Agent': 'okhttp/3.10.0',
'Host': 'manga.hdslb.com'
}
def makeDir(dirPath):
if os.path.isdir(dirPath) == False:
os.makedirs(dirPath)
else:
pass
def getComicDetail(comicId):
url = "https://manga.bilibili.com/twirp/comic.v2.Comic/ComicDetail"
data = {
'access_key': access_key,
'appkey': appkey,
'comic_id': comicId,
'device': 'android',
}
r = requests.post(url, data=data, headers=headers)
if r.status_code == requests.codes.ok:
try:
data = r.json()
return data['data']
except Exception as e:
print(e)
else:
print(f"getComicDetail fail,id={comicId},{r.status_code}")
return 0
def printList(ep_list,path):
file=os.path.join(path,"漫画详情.txt")
text=""
for ep in ep_list:
text=text+"章节id:{},章节名:{} {}\n".format(ep['id'],ep['short_title'],ep["title"])
with open(file,"w+", encoding="utf-8") as f:
f.write(text)
def getEpList(ep_list, filter=True, beginId=0, endId=9999999):
EpList = []
for ep in ep_list:
n = int(ep['id'])
if n <= beginId or n > endId:
continue
epDict = {"episodeId": ep['id'], "name": ep['short_title']}
if filter:
if ep["is_locked"] == False or ep["is_in_free"]:
EpList.append(epDict)
else:
pass
else:
EpList.append(epDict)
return EpList
def getEpIndex(comicId, episodeId):
def generateHashKey(comicId, episodeId):
n = [None for i in range(8)]
e = int(comicId)
t = int(episodeId)
n[0] = t
n[1] = t >> 8
n[2] = t >> 16
n[3] = t >> 24
n[4] = e
n[5] = e >> 8
n[6] = e >> 16
n[7] = e >> 24
for idx in range(8):
n[idx] = n[idx] % 256
return n
def unhashContent(hashKey, indexData):
for idx in range(len(indexData)):
indexData[idx] ^= hashKey[idx % 8]
return bytes(indexData)
url = "https://manga.bilibili.com/twirp/comic.v1.Comic/GetImageIndex"
payload = f"access_key={access_key}&appkey={appkey}&device=android&ep_id={episodeId}&mobi_app=android_comic&platform=android"
r = requests.post(url, headers=headers, data=payload)
data = r.json()["data"]["host"]+r.json()["data"]["path"].replace(r"\u003d", r"=")
r = requests.get(data, headers=getHeaders)
indexData = r.content
hashKey = generateHashKey(comicId, episodeId)
indexData = list(indexData)[9:]
indexData = unhashContent(hashKey=hashKey, indexData=indexData)
file = io.BytesIO(indexData)
tmp_dir = tempfile.TemporaryDirectory()
obj = zipfile.ZipFile(file)
obj.extractall(tmp_dir.name)
json_file = os.path.join(tmp_dir.name, "index.dat")
return json.load(open(json_file))
def getImageToken(imageUrls):
url = "https://manga.bilibili.com/twirp/comic.v1.Comic/ImageToken"
data = {
'access_key': access_key,
'appkey': appkey,
# 必须使用json.dumps否则会400,可能是requests对数组处理有问题
'urls': json.dumps(imageUrls),
}
r = requests.post(url, data=data, headers=headers)
token = r.json()["data"]
return token
def download(url, token, imgPath):
url = url+f"?token={token}"
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(imgPath, 'wb') as f:
for chunk in r:
f.write(chunk)
else:
print(f"图片{url}下载失败!")
def DownloadThread(q):
while True:
try:
# 不阻塞的读取队列数据
task = q.get_nowait()
url = task["url"]
token = task["token"]
imgPath = task["imgPath"]
download(url, token, imgPath)
time.sleep(1)
except queue.Empty as e:
break
except Exception as e:
print(e)
print("{}?token={}".format(task["url"],task["token"]))
break
q.task_done()
def main():
detail = getComicDetail(comicId)
comicName = detail["title"].replace(r"\t","").rstrip()
comicDir = os.path.join(workDir, comicId)
makeDir(comicDir)
print(f"已获取漫画《{comicName}》详情,并建立文件夹/{comicId}")
ep_list = detail["ep_list"]
printList(ep_list,comicDir)
if detail["discount_type"] == 2:
EpList = getEpList(ep_list, filter=False, beginId=beginId, endId=endId)
else:
EpList = getEpList(ep_list, filter=True, beginId=beginId, endId=endId)
print("已获取章节列表")
for ep in EpList:
episodeId = ep["episodeId"]
epDir = os.path.join(comicDir, f"{ep['name']} #{episodeId}#")
makeDir(epDir)
indexData = getEpIndex(comicId, episodeId)
imageUrls = ["https://manga.hdslb.com{}".format(url)
for url in indexData["pics"]]
data = getImageToken(imageUrls)
print(f"已获取章节{ep['name']}的图片链接,章节id:{episodeId}")
n = 1
q = queue.Queue()
for task in data:
imgPath = os.path.join(epDir, f"{n}.jpg".zfill(6))
n = n+1
task["imgPath"] = imgPath
q.put(task)
threads = []
for i in range(10):
# 第一个参数是线程函数变量,第二个参数args是一个数组变量参数,
# 如果只传递一个值,就只需要q, 如果需要传递多个参数,那么还可以继续传递下去其他的参数,
# 其中的逗号不能少,少了就不是数组了,就会出错。
thread = threading.Thread(target=DownloadThread, args=(q,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print(f"已下载章节{ep['name']},章节id:{episodeId}")
print(f"漫画《{comicName}》下载完毕!\n"+"#"*10)
input('按任意键退出')
if __name__ == '__main__':
main()