From c41aee25311d9658211e69155fb6cd9a0931fc15 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Sat, 11 Dec 2021 14:44:30 +0800 Subject: [PATCH] bot search comment --- yyetsbot/fansub.py | 26 +++++++++++++++++++++----- yyetsbot/yyetsbot.py | 40 +++++----------------------------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/yyetsbot/fansub.py b/yyetsbot/fansub.py index 76d74f5..5d000f1 100644 --- a/yyetsbot/fansub.py +++ b/yyetsbot/fansub.py @@ -22,6 +22,7 @@ from bs4 import BeautifulSoup from config import (BD2020_SEARCH, FANSUB_ORDER, FIX_SEARCH, MONGO, NEWZMZ_RESOURCE, NEWZMZ_SEARCH, REDIS, WORKERS, XL720_SEARCH, ZHUIXINFAN_RESOURCE, ZHUIXINFAN_SEARCH) +from bson.objectid import ObjectId logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s') @@ -220,11 +221,26 @@ class YYeTsOffline(BaseFansub): @Redis.result_cache(10 * 60) def search_result(self, resource_url) -> dict: # yyets offline - # https://yyets.dmesg.app/resource.html?id=37089 - rid = resource_url.split("id=")[1] - data: dict = self.collection.find_one({"data.info.id": int(rid)}, {'_id': False}) - name = data["data"]["info"]["cnname"] - return {"all": json.dumps(data, ensure_ascii=False), "share": WORKERS.format(id=rid), "cnname": name} + + # resource: https://yyets.dmesg.app/resource.html?id=37089 + # comment: 'https://yyets.dmesg.app/resource.html?id=233#61893ae51e9152e43fa24124' + if "#" in resource_url: + cid = resource_url.split("#")[1] + data: dict = self.db["comment"].find_one( + {"_id": ObjectId(cid)}, + {'_id': False, "ip": False, "type": False, "children": False, "browser": False} + ) + share = resource_url + name = f"{data['username']} 的分享" + t = "comment" + else: + rid = resource_url.split("id=")[1] + data: dict = self.collection.find_one({"data.info.id": int(rid)}, {'_id': False}) + name = data["data"]["info"]["cnname"] + share = WORKERS.format(id=rid) + t = "resource" + + return {"all": json.dumps(data, ensure_ascii=False, indent=4), "share": share, "cnname": name, "type": t} def __del__(self): self.mongo.close() diff --git a/yyetsbot/yyetsbot.py b/yyetsbot/yyetsbot.py index 3ce95e8..04e29b6 100644 --- a/yyetsbot/yyetsbot.py +++ b/yyetsbot/yyetsbot.py @@ -269,48 +269,18 @@ def choose_link(call): if magic_recycle(fan, call, resource_url_hash): return - markup = types.InlineKeyboardMarkup() - # add class - btn1 = types.InlineKeyboardButton("分享页面", callback_data="share%s" % resource_url_hash) - btn2 = types.InlineKeyboardButton("我全都要", callback_data="all%s" % resource_url_hash) - markup.add(btn1, btn2) - - text = "想要分享页面,还是我全都要?\n\n" \ - "名词解释:“分享页面”会返回给你一个网站,从那里可以看到全部的下载链接。\n" \ - "“我全都要”会给你发送一个txt文件,文件里包含全部下载连接\n" - bot.reply_to(call.message, text, reply_markup=markup) - - -@bot.callback_query_handler(func=lambda call: re.findall(r"share(\S*)", call.data)) -def share_page(call): - fan = fansub.FansubEntrance() - # need class name as str - bot.send_chat_action(call.message.chat.id, 'typing') - resource_url_hash = re.findall(r"share(\S*)", call.data)[0] - if magic_recycle(fan, call, resource_url_hash): - return - - result = fan.search_result(resource_url_hash) - bot.send_message(call.message.chat.id, "{} {}".format(result['cnname'], result['share'])) - - -@bot.callback_query_handler(func=lambda call: re.findall(r"all(\S*)", call.data)) -def all_episode(call): - # just send a file - fan = fansub.FansubEntrance() - bot.send_chat_action(call.message.chat.id, 'typing') - resource_url_hash = re.findall(r"all(\S*)", call.data)[0] - if magic_recycle(fan, call, resource_url_hash): - return - result = fan.search_result(resource_url_hash) with tempfile.NamedTemporaryFile(mode='wb+', prefix=result["cnname"], suffix=".txt") as tmp: bytes_data = json.dumps(result["all"], ensure_ascii=False, indent=4).encode('u8') tmp.write(bytes_data) tmp.flush() with open(tmp.name, "rb") as f: + if result.get("type") == "resource": + caption = "{}\n\n{}".format(result["cnname"], result["share"]) + else: + caption = result["all"] bot.send_chat_action(call.message.chat.id, 'upload_document') - bot.send_document(call.message.chat.id, f, caption="%s" % result["cnname"]) + bot.send_document(call.message.chat.id, f, caption=caption) @bot.callback_query_handler(func=lambda call: re.findall(r"unwelcome(\d*)", call.data))