forked from zhongfly/view2webp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview2webp.py
More file actions
153 lines (132 loc) · 4.82 KB
/
view2webp.py
File metadata and controls
153 lines (132 loc) · 4.82 KB
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
# encoding:UTF-8
# python3.6
import os
import io
import json
import zipfile
import re
import requests
import shutil
from distutils.dir_util import copy_tree
workDir=os.getcwd()
def getComicDetail(id):
url = "https://manga.bilibili.com/twirp/comic.v2.Comic/ComicDetail"
payload = "device=android&comic_id="+str(id)
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'user-agent': "Mozilla/5.0 BiliComic/1.9.0",
'Host': "manga.bilibili.com",
'cache-control': "no-cache"
}
r = requests.post(url, data=payload, 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={id},{r.status_code}")
return 0
def getEpDict(ep_list):
epDict = {}
for ep in ep_list:
epDict[str(ep['id'])] = ep['short_title']
return epDict
def getpicDict(comicId, episodeId, indexDataFile):
def unzip(file, target_dir):
obj = zipfile.ZipFile(file)
obj.extractall(target_dir)
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)
key = generateHashKey(comicId, episodeId)
with open(indexDataFile, 'rb') as f:
indexData = f.read()
indexData = list(indexData)[9:]
indexData = unhashContent(hashKey=key, indexData=indexData)
file = io.BytesIO(indexData)
unzip(file, os.path.dirname(indexDataFile))
json_file = os.path.join(indexDataFile)
picData = json.load(open(json_file))["pics"]
picDict = {}
n = 1
for pic in picData:
picName = os.path.basename(pic).replace('.png', '.png.view').replace('.jpg','.jpg.view')
picDict[picName] = str(n)+'.webp'
n = n+1
return picDict
def view2webp(file):
with open(file, 'rb') as f:
data = f.read()
with open(file, 'wb') as f:
f.write(data[9:])
def main():
comics = [entry.path for entry in os.scandir(
workDir) if entry.is_dir() and re.match(r'\d+$', entry.name) != None]
for comic in comics:
folderName = os.path.basename(comic)
detail = getComicDetail(folderName)
name = detail['title']
epDict = getEpDict(detail['ep_list'])
print("[INFO] Now processing: " + name)
epDirs = [entry.path for entry in os.scandir(
comic) if entry.is_dir() and re.match(r'\d+$', entry.name) != None]
for ep in epDirs:
dirs = [entry.path for entry in os.scandir(ep) if entry.is_dir()]
if len(dirs) < 1:
path = ep
else:
path = dirs[0]
fileList = [entry.path for entry in os.scandir(
path) if entry.name.endswith(".view")]
filePath = sorted(fileList, key=lambda x: os.path.getmtime(x))
if os.path.exists(os.path.join(path, 'index.dat')):
indexDataFile = os.path.join(path, 'index.dat')
comicId = folderName
episodeId = os.path.basename(ep)
picDict = getpicDict(comicId, episodeId, indexDataFile)
else:
picDict = False
n = 1
for file in filePath:
view2webp(file)
if picDict != False:
newName = picDict[os.path.basename(file)]
newPath = os.path.join(os.path.dirname(file), newName)
else:
newPath = os.path.join(os.path.dirname(file), f"{n}.webp".zfill(7))
os.rename(file, newPath)
n = n+1
epName = epDict[os.path.basename(ep)]
newPath = os.path.join(os.path.dirname(ep), epName)
try:
os.rename(ep, newPath)
except OSError as e:
os.rename(ep, newPath+os.path.basename(ep))
print(f"[INFO] {epName} has done.")
tgtPath = os.path.join(workDir, name)
if os.path.isdir(tgtPath):
print(f"[INFO] Folder {name} exists, updating...")
copy_tree(comic, tgtPath)
shutil.rmtree(comic)
else: os.rename(comic, tgtPath)
print(f"[INFO] {name} completed.\n")
if __name__ == '__main__':
main()