This repository was archived by the owner on Jul 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjmutils.py
More file actions
263 lines (227 loc) · 9.41 KB
/
Copy pathjmutils.py
File metadata and controls
263 lines (227 loc) · 9.41 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
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
import os, shutil, img2pdf, zipfile, jmcomic
from PIL import Image
import cv2
from selenium import webdriver
import math
import zhconv
def compress_image(images):
for image in images:
img = cv2.imread(image)
if img.shape[0] > 2000:
img = cv2.resize(img, (int(img.shape[1] * 2000 / img.shape[0]), 2000))
cv2.imwrite(image, img, [cv2.IMWRITE_JPEG_QUALITY, 30])
async def zip_manga(album_id, password=""):
script_dir = os.path.dirname(os.path.abspath(__file__))
album_dir = os.path.join(script_dir, f"manga/{album_id}")
compressed_dir = os.path.join(script_dir, f"compressed/{album_id}")
# 获取album_dir文件夹的大小,如果album_size大于30MB,则压缩图片
album_size = sum(
sum(os.path.getsize(os.path.join(parent, file)) for file in files)
for parent, dirs, files in os.walk(album_dir)
)
print("[jmcomic]原始本子大小:", album_size)
need_compress = False
if album_size > 30 * 1024 * 1024:
need_compress = True
print("[jmcomic]需要压缩")
# Convert every photo into a PDF file.
for photo in os.listdir(album_dir):
output = f"{album_dir}/{photo}.pdf"
photo_dir = os.path.join(album_dir, photo)
images = [os.path.join(photo_dir, image) for image in os.listdir(photo_dir)]
images.sort()
if need_compress:
compress_image(images)
with open(output, "wb") as f:
f.write(img2pdf.convert(images))
# Zip the PDF files with encryption.
with zipfile.ZipFile(f"{compressed_dir}.zip", "w") as zipf:
for pdf in os.listdir(album_dir):
if pdf.endswith(".pdf"):
zipf.write(f"{album_dir}/{pdf}", pdf)
def delete_temp_file(album_id: int):
script_dir = os.path.dirname(os.path.abspath(__file__))
album_dir = os.path.join(script_dir, f"manga/{album_id}")
if os.path.exists(album_dir):
shutil.rmtree(album_dir)
class JmAlbumInfoPic:
def __init__(self):
# 初始化Firefox浏览器
self.options = webdriver.FirefoxOptions()
self.options.add_argument("--headless") # 无头模式
self.options.add_argument("--disable-gpu")
self.options.add_argument("--width=700")
self.options.add_argument("--height=2000")
self.options.set_preference("app.update.auto", False)
self.options.set_preference("app.update.enabled", False)
self.browser_driver = webdriver.Firefox(options=self.options)
def __del__(self):
self.browser_driver.quit()
def generate(self, album: jmcomic.JmAlbumDetail):
try:
driver = self.browser_driver
# 寻找./plugins/jmcomic/manga/{album.album_id}/1/里面序号最小的图片
min_image = min(os.listdir(f"./plugins/jmcomic/manga/{album.album_id}/1/"))
shutil.copy(
f"./plugins/jmcomic/manga/{album.album_id}/1/{min_image}",
"./plugins/jmcomic/jacket.jpg",
)
title = album.title
tags = album.tags
tags_str = ",".join(tags)
page_url = os.path.dirname(__file__) + "/info.html"
url = f"file:///{page_url}?title={title}&tags={tags_str}"
driver.get(url)
# 获取container元素
container = driver.find_element("css selector", ".container")
# 获取设备像素比(处理高DPI屏幕)
dpr = driver.execute_script("return window.devicePixelRatio")
# 获取元素位置和尺寸
location = container.location
size = container.size
# 计算裁剪区域(考虑设备像素比)
left = math.floor(location["x"] * dpr) - 15
top = math.floor(location["y"] * dpr) - 15
right = math.floor((location["x"] + size["width"]) * dpr) + 15
bottom = math.floor((location["y"] + size["height"]) * dpr) + 15
# 截取完整页面截图
driver.save_screenshot(
os.path.join(os.path.dirname(__file__), "full_page.png")
)
# 打开截图并裁剪
image = Image.open(os.path.join(os.path.dirname(__file__), "full_page.png"))
image = image.crop((left, top, right, bottom))
# 转换为RGB模式
if image.mode == "RGBA":
image = image.convert("RGB")
# 保存裁剪后的图片
save_path = os.path.join(
os.path.dirname(__file__), f"info_image/{album.album_id}.jpg"
)
image.save(save_path)
except Exception as e:
print(f"生成封面图片失败: {e}")
return None
def negtag_to_sql(negative_tags):
if not negative_tags:
return ""
return " AND ".join([f"tags NOT LIKE '%,{tag},%'" for tag in negative_tags])
class RuleLogicExpression:
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __init__(self, attr, exp):
if attr not in ["name", "tags", "authors"]:
raise ValueError(
f"Invalid attribute {attr}, must be one of: name, tags, authors"
)
self.attr = attr
self.precise = attr == "tags"
self.exp = exp
self.parse()
def parse(self):
self.exp = "(" + self.exp + ")"
symbols = ["&", "|", "!", "(", ")"]
priority = {"!": 3, "&": 2, "|": 1, "(": 0}
symbol_stack = []
value_stack = []
value = ""
for c in self.exp:
if c == "(":
c = "("
elif c == ")":
c = ")"
elif c == "'":
c = "''"
if c in symbols:
if value:
value_stack.append(self.Node(value))
value = ""
if c == "(":
symbol_stack.append(c)
elif c == ")":
while symbol_stack[-1] != "(":
new_node = self.Node(symbol_stack.pop())
if new_node.value == "!":
new_node.right = value_stack.pop()
else:
new_node.right = value_stack.pop()
new_node.left = value_stack.pop()
value_stack.append(new_node)
symbol_stack.pop()
else:
while priority[symbol_stack[-1]] >= priority[c]:
new_node = self.Node(symbol_stack.pop())
if new_node.value == "!":
new_node.right = value_stack.pop()
else:
new_node.right = value_stack.pop()
new_node.left = value_stack.pop()
value_stack.append(new_node)
symbol_stack.append(c)
else:
value += c
if value_stack:
self.root = value_stack.pop()
else:
self.root = None
if value_stack or symbol_stack:
raise ValueError("Invalid expression")
def to_sql(self) -> str:
def dfs(node):
if node.value == "!":
return f"NOT ({dfs(node.right)})"
elif node.value == "&":
return f"({dfs(node.left)}) AND ({dfs(node.right)})"
elif node.value == "|":
return f"({dfs(node.left)}) OR ({dfs(node.right)})"
else:
if self.precise:
return f"{self.attr} LIKE '%,{zhconv.convert(node.value.lower(), 'zh-hans')},%'"
else:
return f"{self.attr} LIKE '%{zhconv.convert(node.value.lower(), 'zh-hans')}%'"
return dfs(self.root)
class PackagedJmClient:
def __init__(self, html: jmcomic.JmHtmlClient, api: jmcomic.JmApiClient):
self.html = html
self.api = api
def get_album_detail(self, comic_id: int | str) -> jmcomic.JmAlbumDetail:
try:
album = self.html.get_album_detail(comic_id)
return album
except Exception:
print("[jmcomic]获取本子信息:html接口失败,切换至api接口")
album = self.api.get_album_detail(comic_id)
return album
class PackagedJmOption:
def __init__(self, html: jmcomic.JmOption, api: jmcomic.JmOption):
self.html = html
self.api = api
self.client = None
def build_jm_client(self) -> PackagedJmClient:
if not self.client:
self.client = PackagedJmClient(
self.html.build_jm_client(), self.api.build_jm_client()
)
return self.client
async def packaged_download_album(
option: PackagedJmOption, album: jmcomic.JmAlbumDetail
):
try:
with jmcomic.new_downloader(option.html) as dler:
dler.download_by_album_detail(album)
dler.raise_if_has_exception()
except Exception:
print("[jmcomic]下载本子:html接口失败,切换至api接口")
with jmcomic.new_downloader(option.api) as dler:
dler.download_by_album_detail(album)
dler.raise_if_has_exception()
if __name__ == "__main__":
option = jmcomic.create_option_by_file(
os.path.join(os.path.dirname(__file__), "option.yml")
)
client = option.new_jm_client()
album = client.get_album_detail(810872)
print(album.page_count)