Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions module/search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ async def s_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not results:
return await msg.edit_text("未搜索到文件,换个关键词试试吧")

text, button = await build_result(results, update, k)
text, button = await build_result(results, msg, k)
await msg.edit_text(text=text, reply_markup=InlineKeyboardMarkup(button), disable_web_page_preview=True)


async def build_result(content: list[PanSouResult], update: Update, keyword: str):
chat_id = update.effective_chat.id
message_id = update.message.id + 1
async def build_result(content: list[PanSouResult], msg, keyword: str):
chat_id = msg.chat.id
message_id = msg.message_id
cmid = f"{chat_id}|{message_id}"

page = Page(content, keyword)
Expand All @@ -195,6 +195,10 @@ async def search_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
page = PAGE.get(cmid)

if not page:
try:
await query.answer("搜索结果已过期,请重新搜索", show_alert=True)
except Exception:
pass
return

if data == "search_next_page":
Expand Down
24 changes: 14 additions & 10 deletions module/tmdb_search/tmdb_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ async def sm_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
return

chat_id = update.effective_chat.id
RESULT_TMDB.clear()
cmid = f"{chat_id}|{search_msg.message_id}"
keys_to_delete = [k for k in RESULT_TMDB if k.startswith(f"{cmid}_")]
for k in keys_to_delete:
del RESULT_TMDB[k]
for i, r in enumerate(results):
RESULT_TMDB[f"{chat_id}_{i}"] = r
RESULT_TMDB[f"{cmid}_{i}"] = r

page = TMDbPage(results, keyword)
PAGE[chat_id] = page
PAGE[cmid] = page

text, buttons = page.now_page_text()
await search_msg.edit_text(text=text, reply_markup=InlineKeyboardMarkup(buttons), parse_mode="Markdown")
except Exception as e:
Expand All @@ -143,15 +146,16 @@ async def tmdb_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
data = query.data
msg = query.message
chat_id = msg.chat.id

page = PAGE.get(chat_id)
cmid = f"{chat_id}|{msg.message_id}"

page = PAGE.get(cmid)
if not page:
try:
await query.answer("会话已过期,请重新搜索", show_alert=True)
except Exception:
pass
return

if data == "tmdb_next":
page.next_page()
text, buttons = page.now_page_text()
Expand All @@ -167,8 +171,8 @@ async def tmdb_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
index = int(data.split("_")[-1])
except ValueError:
return
result_key = f"{chat_id}_{index}"

result_key = f"{cmid}_{index}"
result = RESULT_TMDB.get(result_key)
if not result:
try:
Expand Down
39 changes: 28 additions & 11 deletions module/torrent_search/torrent_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
PAGE: dict[str, "TorrentPage"] = {}
RESULT_INDEXER: dict[str, ProwlarrResult] = {}
INDEXERS: dict[int, str] = {}
KEYWORD_CACHE: dict[int, str] = {}

# 下载信息缓存(用于路径/工具选择)
DOWNLOAD_INFO: dict[int, dict] = {}
Expand Down Expand Up @@ -221,25 +222,28 @@ async def sb_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
return await update.message.reply_text("请加上关键词,例:`/sb 电影名称`")

await load_indexers()

if not INDEXERS:
return await update.message.reply_text("无法获取索引器列表,请检查 Prowlarr 配置")


# 将关键词存入缓存,callback_data 只携带 chat_id,避免超过 64 字节限制
KEYWORD_CACHE[chat.id] = keyword

keyboard = []
row = []
for idx_id, idx_name in INDEXERS.items():
row.append(InlineKeyboardButton(
idx_name[:12],
callback_data=f"sb_indexer_{idx_id}_{keyword}"
callback_data=f"sb_indexer_{idx_id}_{chat.id}"
))
if len(row) == 2:
keyboard.append(row)
row = []
if row:
keyboard.append(row)
keyboard.append([InlineKeyboardButton("🔍 全部索引器", callback_data=f"sb_indexer_all_{keyword}")])

keyboard.append([InlineKeyboardButton("🔍 全部索引器", callback_data=f"sb_indexer_all_{chat.id}")])

await update.message.reply_text(
f"🔍 选择要使用的索引器:\n关键词: {keyword}",
reply_markup=InlineKeyboardMarkup(keyboard)
Expand Down Expand Up @@ -271,7 +275,9 @@ async def search_with_indexer(update: Update, context: ContextTypes.DEFAULT_TYPE
if not results:
return await original_msg.edit_text("未搜索到结果")

RESULT_INDEXER.clear()
keys_to_delete = [k for k in RESULT_INDEXER if k.startswith(f"{cmid}_")]
for k in keys_to_delete:
del RESULT_INDEXER[k]
for i, r in enumerate(results):
RESULT_INDEXER[f"{cmid}_{i}"] = r

Expand All @@ -297,18 +303,29 @@ async def torrent_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
parts = data.replace("sb_indexer_", "").split("_", 1)
if len(parts) != 2:
return

indexer_part = parts[0]
keyword = parts[1]

try:
source_chat_id = int(parts[1])
except ValueError:
return

keyword = KEYWORD_CACHE.get(source_chat_id, "")
if not keyword:
try:
await query.answer("会话已过期,请重新搜索", show_alert=True)
except Exception:
pass
return

if indexer_part == "all":
indexer_id = -1
else:
try:
indexer_id = int(indexer_part)
except ValueError:
return

await search_with_indexer(update, context, indexer_id, keyword, query.message.text)
return

Expand Down