Skip to content

Commit 5330d68

Browse files
committed
refactor: unify constants and add cover caching
1 parent 33f441b commit 5330d68

3 files changed

Lines changed: 23 additions & 86 deletions

File tree

core/auth.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
提供登录、登出、会话管理等认证功能,支持 cookies 持久化。
55
"""
66

7+
import importlib.util
78
import json
89

910
from astrbot.api import logger
1011

1112
from .base import JMClientMixin, JMConfigManager
1213

13-
try:
14-
JMCOMIC_AVAILABLE = True
15-
except ImportError:
16-
JMCOMIC_AVAILABLE = False
14+
JMCOMIC_AVAILABLE = importlib.util.find_spec("jmcomic") is not None
1715

1816

1917
class JMAuthManager(JMClientMixin):

core/browser.py

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
JMCOMIC_AVAILABLE = False
1717

1818
from .base import JMClientMixin, JMConfigManager
19+
from .constants import (
20+
get_category_list,
21+
get_order_list,
22+
get_time_list,
23+
)
1924

2025

2126
class JMBrowser(JMClientMixin):
@@ -221,6 +226,10 @@ def _get_album_cover_sync(
221226
# 封面保存路径
222227
cover_path = save_dir / f"{parsed_id}.jpg"
223228

229+
# 如果封面已存在,直接返回(缓存)
230+
if cover_path.exists():
231+
return cover_path
232+
224233
# 下载封面
225234
client.download_album_cover(parsed_id, str(cover_path))
226235

@@ -325,34 +334,7 @@ def _get_month_ranking_sync(self, page: int, option) -> list[dict]:
325334

326335
# ==================== 分类浏览功能 ====================
327336

328-
# 分类常量映射
329-
CATEGORY_MAP = {
330-
"all": "0",
331-
"doujin": "doujin",
332-
"single": "single",
333-
"short": "short",
334-
"hanman": "hanman",
335-
"meiman": "meiman",
336-
"3d": "3D",
337-
"cosplay": "doujin_cosplay",
338-
"another": "another",
339-
}
340-
341-
# 排序常量映射
342-
ORDER_MAP = {
343-
"new": "mr", # 最新
344-
"hot": "mv", # 最热(观看数)
345-
"pic": "mp", # 图片多
346-
"like": "tf", # 点赞多
347-
}
348-
349-
# 时间常量映射
350-
TIME_MAP = {
351-
"day": "t", # 今日
352-
"week": "w", # 本周
353-
"month": "m", # 本月
354-
"all": "a", # 全部时间
355-
}
337+
# 常量映射已移至 constants.py
356338

357339
async def get_category_albums(
358340
self,
@@ -422,20 +404,10 @@ def _get_category_albums_sync(
422404
logger.error(f"获取分类浏览失败: {e}")
423405
return []
424406

425-
@classmethod
426-
def get_category_list(cls) -> list[str]:
427-
"""获取所有支持的分类"""
428-
return list(cls.CATEGORY_MAP.keys())
429-
430-
@classmethod
431-
def get_order_list(cls) -> list[str]:
432-
"""获取所有支持的排序方式"""
433-
return list(cls.ORDER_MAP.keys())
434-
435-
@classmethod
436-
def get_time_list(cls) -> list[str]:
437-
"""获取所有支持的时间范围"""
438-
return list(cls.TIME_MAP.keys())
407+
# 辅助方法:使用 constants 模块中的函数
408+
get_category_list = staticmethod(get_category_list)
409+
get_order_list = staticmethod(get_order_list)
410+
get_time_list = staticmethod(get_time_list)
439411

440412
# ==================== 收藏夹功能 ====================
441413

utils/formatter.py

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
消息格式化工具
33
"""
44

5+
from ..core.constants import CATEGORY_NAMES, ORDER_NAMES, TIME_NAMES
6+
57

68
class MessageFormatter:
79
"""消息格式化器"""
@@ -138,45 +140,10 @@ def format_ranking_results(
138140

139141
return "\n".join(lines)
140142

141-
# 分类名称映射(用于显示)
142-
CATEGORY_NAMES = {
143-
"all": "全部",
144-
"0": "全部",
145-
"doujin": "同人",
146-
"single": "单本",
147-
"short": "短篇",
148-
"hanman": "韩漫",
149-
"meiman": "美漫",
150-
"3d": "3D",
151-
"3D": "3D",
152-
"cosplay": "Cosplay",
153-
"doujin_cosplay": "Cosplay",
154-
"another": "其他",
155-
}
156-
157-
# 排序名称映射(用于显示)
158-
ORDER_NAMES = {
159-
"new": "最新",
160-
"mr": "最新",
161-
"hot": "热门",
162-
"mv": "热门",
163-
"pic": "图多",
164-
"mp": "图多",
165-
"like": "点赞",
166-
"tf": "点赞",
167-
}
168-
169-
# 时间名称映射(用于显示)
170-
TIME_NAMES = {
171-
"day": "今日",
172-
"t": "今日",
173-
"week": "本周",
174-
"w": "本周",
175-
"month": "本月",
176-
"m": "本月",
177-
"all": "全部时间",
178-
"a": "全部时间",
179-
}
143+
# 常量映射已移至 core/constants.py,这里保留引用以保持兼容性
144+
CATEGORY_NAMES = CATEGORY_NAMES
145+
ORDER_NAMES = ORDER_NAMES
146+
TIME_NAMES = TIME_NAMES
180147

181148
@classmethod
182149
def format_recommend_results(
@@ -381,7 +348,7 @@ def format_help() -> str:
381348
382349
【基本命令】
383350
/jm <ID> - 下载指定ID的本子
384-
/jmc <ID> - 下载指定ID的章节
351+
/jmc <ID> <章节> - 下载指定本子的指定章节
385352
/jms <关键词> - 搜索漫画
386353
/jmi <ID> - 查看本子详情
387354
/jmrank - 查看排行榜

0 commit comments

Comments
 (0)