diff --git a/.env.prod b/.env.prod index c7a04ac7..27e88f15 100644 --- a/.env.prod +++ b/.env.prod @@ -11,7 +11,7 @@ RSS_PROXY = '127.0.0.1:7890' # 代理地址 RSSHUB = 'https://rsshub.app' # rsshub订阅地址 RSSHUB_backup = [] # 备用rsshub地址 填写示例 ["https://rsshub.app","https://rsshub.app"],务必使用双引号!!! DB_CACHE_EXPIRE = 30 # 去重数据库的记录清理限定天数 -LIMT = 50 # 缓存rss条数 +LIMIT = 50 # 缓存rss条数 # 非 GIF 图片压缩后的最大长宽值 @@ -43,7 +43,7 @@ qb_web_url='http://127.0.0.1:8081' #qbittorrent 客户端默认是关闭状态 down_status_msg_group=[] # 下载进度消息提示群组 示例 [12345678] 注意:最好是将该群设置为免打扰 down_status_msg_date=10 # 下载进度检查及提示间隔时间,秒,不建议小于 10s -VERSION = 'v2.2.1' +VERSION = 'v2.2.2' # MYELF博客地址 https://myelf.club # 出现问题请在 GitHub 上提 issues diff --git a/src/plugins/ELF_RSS2/RSS/my_trigger.py b/src/plugins/ELF_RSS2/RSS/my_trigger.py index 5c74f8ef..cc509058 100644 --- a/src/plugins/ELF_RSS2/RSS/my_trigger.py +++ b/src/plugins/ELF_RSS2/RSS/my_trigger.py @@ -11,12 +11,12 @@ # 检测某个rss更新 #任务体 @util.time_out(time=300) # 20s 任务超时时间 -async def check_update(rss: rss_class.rss): +async def check_update(rss: rss_class.Rss): logger.info('{} 检查更新'.format(rss.name)) await rss_parsing.start(rss) -async def delJob(rss: rss_class.rss): +async def delete_job(rss: rss_class.Rss): scheduler = require("nonebot_plugin_apscheduler").scheduler try: scheduler.remove_job(rss.name) @@ -24,14 +24,14 @@ async def delJob(rss: rss_class.rss): logger.debug(e) -async def addJob(rss: rss_class.rss): - await delJob(rss) +async def add_job(rss: rss_class.Rss): + await delete_job(rss) # 加入订阅任务队列,加入前判断是否存在群组或用户,二者不能同时为空 if len(rss.user_id) > 0 or len(rss.group_id) > 0: rss_trigger(rss) -def rss_trigger(rss: rss_class.rss): +def rss_trigger(rss: rss_class.Rss): if re.search(r'[_*/,-]', rss.time): my_trigger_cron(rss) return @@ -61,7 +61,7 @@ def rss_trigger(rss: rss_class.rss): # 参考 https://www.runoob.com/linux/linux-comm-crontab.html -def my_trigger_cron(rss: rss_class.rss): +def my_trigger_cron(rss: rss_class.Rss): # 解析参数 tmp_list = rss.time.split('_') times_list = ['*/5', '*', '*', '*', '*'] diff --git a/src/plugins/ELF_RSS2/RSS/qbittorrent_download.py b/src/plugins/ELF_RSS2/RSS/qbittorrent_download.py index 5c3d6cf5..392cab0e 100644 --- a/src/plugins/ELF_RSS2/RSS/qbittorrent_download.py +++ b/src/plugins/ELF_RSS2/RSS/qbittorrent_download.py @@ -2,14 +2,12 @@ import base64 import datetime import re -import time import httpx import nonebot from apscheduler.triggers.interval import IntervalTrigger from nonebot import logger, require from qbittorrent import Client -# from starlette.responses import FileResponse from ..config import config # 计划 @@ -23,7 +21,7 @@ DOWN_STATUS_DOWNING = 1 # 下载中 DOWN_STATUS_UPLOADING = 2 # 上传中 -DOWN_STATUS_UPLOADOK = 3 # 上传完成 +DOWN_STATUS_UPLOAD_OK = 3 # 上传完成 down_info = {} @@ -37,7 +35,7 @@ # } # 发送通知 -async def send_Msg(msg: str) -> list: +async def send_msg(msg: str) -> list: logger.info(msg) bot, = nonebot.get_bots().values() msg_id = [] @@ -46,7 +44,7 @@ async def send_Msg(msg: str) -> list: return msg_id -async def get_qb(): +async def get_qb_client(): try: qb = Client(config.qb_web_url) qb.login() @@ -68,7 +66,7 @@ async def get_qb(): return qb -def getSize(size: int) -> str: +def get_size(size: int) -> str: kb = 1024 mb = kb * 1024 gb = mb * 1024 @@ -101,31 +99,31 @@ def get_torrent_b16Hash(content: bytes) -> str: return b16Hash -async def get_Hash_Name(url: str, proxy=None) -> dict: +async def get_torrent_info_from_hash(url: str, proxy=None) -> dict: if not proxy: proxy = {} - qb = await get_qb() + qb = await get_qb_client() info = None if re.search(r"magnet:\?xt=urn:btih:", url): qb.download_from_link(link=url) - hash = re.search('[a-f0-9]{40}', url)[0] + hash_str = re.search('[a-f0-9]{40}', url)[0] else: async with httpx.AsyncClient(proxies=proxy) as client: try: res = await client.get(url, timeout=100) qb.download_from_file(res.content) - hash = get_torrent_b16Hash(res.content) + hash_str = get_torrent_b16Hash(res.content) except Exception as e: - await send_Msg('下载种子失败,可能需要代理:{}'.format(e)) + await send_msg('下载种子失败,可能需要代理:{}'.format(e)) return None while not info: for tmp_torrent in qb.torrents(): - if tmp_torrent['hash'] == hash and tmp_torrent['size']: + if tmp_torrent['hash'] == hash_str and tmp_torrent['size']: info = { 'hash': tmp_torrent['hash'], 'filename': tmp_torrent['name'], - 'size': getSize(tmp_torrent['size']) + 'size': get_size(tmp_torrent['size']) } await asyncio.sleep(1) return info @@ -133,12 +131,12 @@ async def get_Hash_Name(url: str, proxy=None) -> dict: # 种子地址,种子下载路径,群文件上传 群列表,订阅名称 async def start_down(url: str, path: str, group_ids: list, name: str, proxy=None) -> str: - qb = await get_qb() + qb = await get_qb_client() if not qb: return # 获取种子 hash - info = await get_Hash_Name(url=url, proxy=proxy) - await rss_trigger(hash=info['hash'], group_ids=group_ids, + info = await get_torrent_info_from_hash(url=url, proxy=proxy) + await rss_trigger(hash_str=info['hash'], group_ids=group_ids, name='订阅:{}\n{}\n文件大小:{}'.format(name, info['filename'], info['size'])) down_info[info['hash']] = { "status": DOWN_STATUS_DOWNING, @@ -149,17 +147,17 @@ async def start_down(url: str, path: str, group_ids: list, name: str, proxy=None # 检查下载状态 -async def check_down_status(hash: str, group_ids: list, name: str): - qb = await get_qb() +async def check_down_status(hash_str: str, group_ids: list, name: str): + qb = await get_qb_client() if not qb: return - info = qb.get_torrent(hash) - files = qb.get_torrent_files(hash) + info = qb.get_torrent(hash_str) + files = qb.get_torrent_files(hash_str) bot, = nonebot.get_bots().values() if info['total_downloaded'] - info['total_size'] >= 0.000000: - all_time = (datetime.datetime.now() - down_info[hash]['start_time']).seconds - await send_Msg(str('👏 {}\nHash: {} \n下载完成!耗时:{} s'.format(name, hash, str(all_time)))) - down_info[hash]['status'] = DOWN_STATUS_UPLOADING + all_time = (datetime.datetime.now() - down_info[hash_str]['start_time']).seconds + await send_msg(str('👏 {}\nHash: {} \n下载完成!耗时:{} s'.format(name, hash_str, str(all_time)))) + down_info[hash_str]['status'] = DOWN_STATUS_UPLOADING for group_id in group_ids: for tmp in files: # 异常包起来防止超时报错导致后续不执行 @@ -168,18 +166,19 @@ async def check_down_status(hash: str, group_ids: list, name: str): path = config.qb_down_path + tmp['name'] else: path = info['save_path'] + tmp['name'] - await send_Msg(str('{}\nHash: {} \n开始上传到群:{}'.format(name, hash, group_id))) + await send_msg(str('{}\nHash: {} \n开始上传到群:{}'.format(name, hash_str, group_id))) await bot.call_api('upload_group_file', group_id=group_id, file=path, name=tmp['name']) - except Exception: + except TimeoutError as e: + logger.warning(e) continue scheduler = require("nonebot_plugin_apscheduler").scheduler - scheduler.remove_job(hash) - down_info[hash]['status'] = DOWN_STATUS_UPLOADOK + scheduler.remove_job(hash_str) + down_info[hash_str]['status'] = DOWN_STATUS_UPLOAD_OK else: - await delete_msg(down_info[hash]['downing_tips_msg_id']) - msg_id = await send_Msg(str('{}\nHash: {} \n下载了 {}%\n平均下载速度:{} KB/s'.format(name, hash, round( + await delete_msg(down_info[hash_str]['downing_tips_msg_id']) + msg_id = await send_msg(str('{}\nHash: {} \n下载了 {}%\n平均下载速度:{} KB/s'.format(name, hash_str, round( info['total_downloaded'] / info['total_size'] * 100, 2), round(info['dl_speed_avg'] / 1024, 2)))) - down_info[hash]['downing_tips_msg_id'] = msg_id + down_info[hash_str]['downing_tips_msg_id'] = msg_id # 撤回消息 @@ -189,7 +188,7 @@ async def delete_msg(msg_ids: list): await bot.call_api('delete_msg', message_id=msg_id['message_id']) -async def rss_trigger(hash: str, group_ids: list, name: str): +async def rss_trigger(hash_str: str, group_ids: list, name: str): scheduler = require("nonebot_plugin_apscheduler").scheduler # 制作一个“time分钟/次”触发器 trigger = IntervalTrigger( @@ -197,16 +196,14 @@ async def rss_trigger(hash: str, group_ids: list, name: str): seconds=int(config.down_status_msg_date), jitter=10 ) - job_defaults = {'max_instances': 10} + job_defaults = {'max_instances': 1} # 添加任务 scheduler.add_job( func=check_down_status, # 要添加任务的函数,不要带参数 trigger=trigger, # 触发器 - args=(hash, group_ids, name), # 函数的参数列表,注意:只有一个值时,不能省略末尾的逗号 - id=hash, - # kwargs=None, + args=(hash_str, group_ids, name), # 函数的参数列表,注意:只有一个值时,不能省略末尾的逗号 + id=hash_str, misfire_grace_time=60, # 允许的误差时间,建议不要省略 - # jobstore='default', # 任务储存库,在下一小节中说明 job_defaults=job_defaults, ) - await send_Msg(str('👏 {}\nHash: {} \n下载任务添加成功!'.format(name, hash))) + await send_msg(str('👏 {}\nHash: {} \n下载任务添加成功!'.format(name, hash_str))) diff --git a/src/plugins/ELF_RSS2/RSS/rss_class.py b/src/plugins/ELF_RSS2/RSS/rss_class.py index 97ecbc72..a99e7034 100644 --- a/src/plugins/ELF_RSS2/RSS/rss_class.py +++ b/src/plugins/ELF_RSS2/RSS/rss_class.py @@ -9,11 +9,11 @@ from ..config import config # 存储目录 -file_path = str(str(Path.cwd()) + os.sep + 'data' + os.sep) +FILE_PATH = str(str(Path.cwd()) + os.sep + 'data' + os.sep) -class rss: - # 定义基本属性 +class Rss: + # 定义为类属性,可以方便新增属性时不会无法解析配置文件 name = '' # 订阅名 url = '' # 订阅地址 user_id = [] # 订阅用户(qq) -1 为空 @@ -32,7 +32,6 @@ class rss: duplicate_filter_mode: [str] = None # 去重模式 max_image_number: int = 0 # 图片数量限制,防止消息太长刷屏 - # 定义构造方法 def __init__(self, name: str, url: str, user_id: str, group_id: str, time='5', img_proxy=False, translation=False, only_title=False, only_pic=False, cookies: str = '', down_torrent: bool = False, down_torrent_keyword: str = None, black_keyword: str = None, is_open_upload_group: bool = True, @@ -64,7 +63,7 @@ def __init__(self, name: str, url: str, user_id: str, group_id: str, time='5', i self.max_image_number = max_image_number # 返回订阅链接 - def geturl(self, rsshub: str = config.rsshub) -> str: + def get_url(self, rsshub: str = config.rsshub) -> str: if re.match(u'[hH][tT]{2}[pP][sS]?://', self.url, flags=0): return self.url else: @@ -76,15 +75,15 @@ def geturl(self, rsshub: str = config.rsshub) -> str: # 读取记录 @staticmethod - def readRss() -> list: + def read_rss() -> list: # 如果文件不存在 - if not os.path.isfile(str(file_path + "rss.json")): + if not os.path.isfile(str(FILE_PATH + "rss.json")): return [] rss_list = [] - with codecs.open(str(file_path + "rss.json"), 'r', 'utf-8') as load_f: + with codecs.open(str(FILE_PATH + "rss.json"), 'r', 'utf-8') as load_f: rss_list_json = json.load(load_f) for rss_one in rss_list_json: - tmp_rss = rss('', '', '-1', '-1') + tmp_rss = Rss('', '', '-1', '-1') if type(rss_one) is not str: rss_one = json.dumps(rss_one) tmp_rss.__dict__ = json.loads(rss_one) @@ -92,9 +91,9 @@ def readRss() -> list: return rss_list # 写入记录,传入rss list,不传就把当前 self 写入 - def writeRss(self, rss_new: list = None): + def write_rss(self, rss_new: list = None): # 先读取订阅记录 - rss_old = self.readRss() + rss_old = self.read_rss() # 把当前 self 写入 if not rss_new: rss_new = [self] @@ -114,86 +113,86 @@ def writeRss(self, rss_new: list = None): tmp = {} tmp.update(rss_one.__dict__) rss_json.append(tmp) - if not os.path.isdir(file_path): - os.makedirs(file_path) - with codecs.open(str(file_path + "rss.json"), "w", 'utf-8') as dump_f: + if not os.path.isdir(FILE_PATH): + os.makedirs(FILE_PATH) + with codecs.open(str(FILE_PATH + "rss.json"), "w", 'utf-8') as dump_f: dump_f.write(json.dumps(rss_json, sort_keys=True, indent=4, ensure_ascii=False)) # 查找是否存在当前订阅名 rss 要转换为 rss_ - def findName(self, name: str): + def find_name(self, name: str): # 过滤特殊字符 name = re.sub(r'[?*:"<>\\/|]', '_', name) if name == 'rss': name = 'rss_' - list = self.readRss() + list = self.read_rss() for tmp in list: if tmp.name == name: return tmp return None # 查找是否存在当前订阅链接 - def findURL(self, url: str): - list = self.readRss() - for tmp in list: + def find_url(self, url: str): + url_list = self.read_rss() + for tmp in url_list: if tmp.url == url: return tmp return None # 添加订阅 QQ - def addUser(self, user: str): + def add_user(self, user: str): if str(user) in self.user_id: return self.user_id.append(str(user)) - self.writeRss() + self.write_rss() # 添加订阅 群组 - def addGroup(self, group: str): + def add_group(self, group: str): if str(group) in self.group_id: return self.group_id.append(str(group)) - self.writeRss() + self.write_rss() # 删除订阅 QQ - def delUser(self, user: str) -> bool: + def delete_user(self, user: str) -> bool: if not str(user) in self.user_id: return False self.user_id.remove(str(user)) - self.writeRss() + self.write_rss() return True # 删除订阅 群组 - def delGroup(self, group: str) -> bool: + def delete_group(self, group: str) -> bool: if not str(group) in self.group_id: return False self.group_id.remove(str(group)) - self.writeRss() + self.write_rss() return True # 删除整个订阅 - def delRss(self, delrss): - rss_old = self.readRss() + def delete_rss(self, delrss): + rss_old = self.read_rss() rss_json = [] for rss_one in rss_old: if rss_one.name != delrss.name: rss_json.append(json.dumps( rss_one.__dict__, ensure_ascii=False)) - if not os.path.isdir(file_path): - os.makedirs(file_path) - with codecs.open(str(file_path + "rss.json"), "w", 'utf-8') as dump_f: + if not os.path.isdir(FILE_PATH): + os.makedirs(FILE_PATH) + with codecs.open(str(FILE_PATH + "rss.json"), "w", 'utf-8') as dump_f: dump_f.write(json.dumps(rss_json, sort_keys=True, indent=4, ensure_ascii=False)) self.delete_file() # 删除订阅json文件 def delete_file(self): - this_file_path = str(file_path + self.name + '.json') + this_file_path = str(FILE_PATH + self.name + '.json') if os.path.exists(this_file_path): os.remove(this_file_path) - def findGroup(self, group: str) -> list: - rss_old = self.readRss() + def find_group(self, group: str) -> list: + rss_old = self.read_rss() re = [] for rss_tmp in rss_old: for group_tmp in rss_tmp.group_id: @@ -204,8 +203,8 @@ def findGroup(self, group: str) -> list: re.append(rss_tmp) return re - def findUser(self, user: str) -> list: - rss_old = self.readRss() + def find_user(self, user: str) -> list: + rss_old = self.read_rss() re = [] for rss_tmp in rss_old: for group_tmp in rss_tmp.user_id: @@ -213,7 +212,7 @@ def findUser(self, user: str) -> list: re.append(rss_tmp) return re - def setCookies(self, cookies_str: str) -> bool: + def set_cookies(self, cookies_str: str) -> bool: try: if len(cookies_str) >= 10: cookies = {} @@ -222,7 +221,7 @@ def setCookies(self, cookies_str: str) -> bool: name, value = line.strip().split("=") cookies[name] = value self.cookies = cookies - self.writeRss() + self.write_rss() return True else: self.cookies = None diff --git a/src/plugins/ELF_RSS2/RSS/rss_parsing.py b/src/plugins/ELF_RSS2/RSS/rss_parsing.py index b26b9167..08ab708c 100644 --- a/src/plugins/ELF_RSS2/RSS/rss_parsing.py +++ b/src/plugins/ELF_RSS2/RSS/rss_parsing.py @@ -28,14 +28,13 @@ from ..config import config from . import rss_class, translation_baidu -# 存储目录 from .qbittorrent_download import start_down -file_path = str(str(Path.cwd()) + os.sep + 'data' + os.sep) +FILE_PATH = str(str(Path.cwd()) + os.sep + 'data' + os.sep) # 代理 -def get_Proxy(open_proxy: bool) -> dict: +def get_proxy(open_proxy: bool) -> dict: if not open_proxy: return {} proxy = config.rss_proxy @@ -46,10 +45,10 @@ def get_Proxy(open_proxy: bool) -> dict: ) if proxy else {} -status_code = [200, 301, 302] +STATUS_CODE = [200, 301, 302] # 去掉烦人的 returning true from eof_received() has no effect when using ssl httpx 警告 asyncio.log.logger.setLevel(40) -headers = { +HEADERS = { 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.9', 'Cache-Control': 'max-age=0', @@ -60,7 +59,7 @@ def get_Proxy(open_proxy: bool) -> dict: # 入口 -async def start(rss: rss_class.rss) -> None: +async def start(rss: rss_class.Rss) -> None: # 网络加载 新RSS # 读取 旧RSS 记录 # 检查更新 @@ -68,17 +67,18 @@ async def start(rss: rss_class.rss) -> None: try: new_rss = await get_rss(rss) - except Exception: + except TimeoutError as e: + logger.error(e) return new_rss_list = new_rss.entries try: - old_rss_list = readRss(rss.name)['entries'] - except Exception: - writeRss(name=rss.name, new_rss=new_rss, new_item=None) + old_rss_list = read_rss(rss.name)['entries'] + except ValueError: + write_rss(name=rss.name, new_rss=new_rss, new_item=None) logger.info('{} 订阅第一次抓取成功!'.format(rss.name)) return - change_rss_list = checkUpdate(new=new_rss_list, old=old_rss_list) + change_rss_list = check_update(new=new_rss_list, old=old_rss_list) if len(change_rss_list) <= 0: # 没有更新,返回 logger.info('{} 没有新信息'.format(rss.name)) @@ -86,7 +86,7 @@ async def start(rss: rss_class.rss) -> None: # 检查是否启用去重 使用 duplicate_filter_mode 字段 conn = None if rss.duplicate_filter_mode: - conn = sqlite3.connect(file_path + 'cache.db') + conn = sqlite3.connect(FILE_PATH + 'cache.db') cursor = conn.cursor() # 用来去重的 sqlite3 数据表如果不存在就创建一个 cursor.execute(""" @@ -173,14 +173,14 @@ async def start(rss: rss_class.rss) -> None: except Exception as e: logger.error('下载种子时出错:{}'.format(e)) # 发送消息并写入文件 - if await sendMsg(rss=rss, msg=item_msg, item=item): + if await send_msg(rss=rss, msg=item_msg, item=item): write_item(rss=rss, new_rss=new_rss, new_item=item) if conn is not None: conn.close() # 去重判断 -async def duplicate_exists(rss: rss_class.rss, item: dict, +async def duplicate_exists(rss: rss_class.Rss, item: dict, conn: sqlite3.connect) -> bool: link = item['link'].replace("'", "''") title = item['title'].replace("'", "''") @@ -191,7 +191,8 @@ async def duplicate_exists(rss: rss_class.rss, item: dict, summary = item['summary'] try: summary_doc = pq(summary) - except Exception: + except Exception as e: + logger.warning(e) # 没有正文内容直接跳过 return False img_doc = summary_doc('img') @@ -200,7 +201,7 @@ async def duplicate_exists(rss: rss_class.rss, item: dict, return False url = img_doc.attr("src") # 通过图像的指纹来判断是否实际是同一张图片 - image_hash = await dowimg(url, rss.img_proxy, get_hash=True) + image_hash = await download_image(url, rss.img_proxy, get_hash=True) logger.info(f'image_hash: {image_hash}') sql += f" AND image_hash='{image_hash}'" if 'link' in rss.duplicate_filter_mode: @@ -229,9 +230,9 @@ async def duplicate_exists(rss: rss_class.rss, item: dict, # 写入单条消息 -def write_item(rss: rss_class.rss, new_rss: list, new_item: str): +def write_item(rss: rss_class.Rss, new_rss: list, new_item: str): tmp = [new_item] - writeRss(name=rss.name, new_rss=new_rss, new_item=tmp) + write_rss(name=rss.name, new_rss=new_rss, new_item=tmp) # 下载种子判断 @@ -241,9 +242,9 @@ async def handle_down_torrent(rss: rss_class, item: dict) -> list: if config.is_open_auto_down_torrent and rss.down_torrent: if rss.down_torrent_keyword: if re.search(rss.down_torrent_keyword, item['summary']): - return await down_torrent(rss=rss, item=item, proxy=get_Proxy(rss.img_proxy)) + return await down_torrent(rss=rss, item=item, proxy=get_proxy(rss.img_proxy)) else: - return await down_torrent(rss=rss, item=item, proxy=get_Proxy(rss.img_proxy)) + return await down_torrent(rss=rss, item=item, proxy=get_proxy(rss.img_proxy)) # 创建下载种子任务 @@ -253,13 +254,13 @@ async def down_torrent(rss: rss_class, item: dict, proxy=None) -> list: if tmp['type'] == 'application/x-bittorrent' or tmp['href'].find('.torrent') > 0: hash_list.append(await start_down(url=tmp['href'], group_ids=rss.group_id, name='{}'.format(rss.name), - path=file_path + os.sep + 'torrent' + os.sep, proxy=proxy)) + path=FILE_PATH + os.sep + 'torrent' + os.sep, proxy=proxy)) return hash_list # 获取 RSS 并解析为 json ,失败重试 @retry(stop_max_attempt_number=5, stop_max_delay=30 * 1000) -async def get_rss(rss: rss_class.rss) -> dict: +async def get_rss(rss: rss_class.Rss) -> dict: # 判断是否使用cookies if rss.cookies: cookies = rss.cookies @@ -267,10 +268,10 @@ async def get_rss(rss: rss_class.rss) -> dict: cookies = None # 获取 xml - async with httpx.AsyncClient(proxies=get_Proxy(open_proxy=rss.img_proxy), cookies=cookies, - headers=headers) as client: + async with httpx.AsyncClient(proxies=get_proxy(open_proxy=rss.img_proxy), cookies=cookies, + headers=HEADERS) as client: try: - r = await client.get(rss.geturl()) + r = await client.get(rss.get_url()) # 解析为 JSON d = feedparser.parse(r.content) except Exception: @@ -279,17 +280,17 @@ async def get_rss(rss: rss_class.rss) -> dict: logger.error('RSSHub :' + config.rsshub + ' 访问失败 !使用备用RSSHub 地址!') for rsshub_url in list(config.rsshub_backup): - async with httpx.AsyncClient(proxies=get_Proxy(open_proxy=rss.img_proxy)) as client: + async with httpx.AsyncClient(proxies=get_proxy(open_proxy=rss.img_proxy)) as client: try: - r = await client.get(rss.geturl(rsshub=rsshub_url)) + r = await client.get(rss.get_url(rsshub=rsshub_url)) except Exception: logger.error( - 'RSSHub :' + rss.geturl(rsshub=rsshub_url) + ' 访问失败 !使用备用 RSSHub 地址!') + 'RSSHub :' + rss.get_url(rsshub=rsshub_url) + ' 访问失败 !使用备用 RSSHub 地址!') continue - if r.status_code in status_code: + if r.STATUS_CODE in STATUS_CODE: d = feedparser.parse(r.content) if d.entries: - logger.info(rss.geturl( + logger.info(rss.get_url( rsshub=rsshub_url) + ' 抓取成功!') break try: @@ -300,7 +301,7 @@ async def get_rss(rss: rss_class.rss) -> dict: cookies_str = '\n如果设置了 cookies 请检查 cookies 正确性' else: cookies_str = '' - e_msg = (f'{rss.name} 抓取失败!已经重试 5 次!请检查订阅地址 {rss.geturl()} {cookies_str}\n' + e_msg = (f'{rss.name} 抓取失败!已经重试 5 次!请检查订阅地址 {rss.get_url()} {cookies_str}\n' f'如果是网络问题,请忽略该错误!E: {e}\n' f'new_rss:{d}') logger.error(e_msg) @@ -314,14 +315,14 @@ async def handle_title(title: str) -> str: # 处理正文,图片放后面 -async def handle_summary(summary: str, rss: rss_class.rss) -> str: +async def handle_summary(summary: str, rss: rss_class.Rss) -> str: # 去掉换行 # summary = re.sub('\n', '', summary) # 处理 summary 使其 HTML标签统一,方便处理 try: summary_html = pq(summary) - except Exception: - logger.info('{} 没有正文内容!', rss.name) + except Exception as e: + logger.info('{} 没有正文内容! {}', rss.name, e) return '' # 最终消息初始化 res_msg = '' @@ -418,7 +419,7 @@ def analyse_image(image: Image): # 图片压缩 -async def zipPic(content): +async def zip_pic(content: bytes) -> BytesIO: # 打开一个 JPEG/PNG/GIF 图像文件 im = Image.open(BytesIO(content)) # 获得图像文件类型: @@ -440,7 +441,8 @@ async def zipPic(content): if im.getcolors(): pim[point[0], point[1]] = random.randint(0, 255) else: - pim[point[0], point[1]] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) + pim[point[0], point[1]] = ( + random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) except BaseException as e: logger.error(f'图片和谐失败! E: {e}') raise @@ -452,8 +454,8 @@ async def zipPic(content): # 将图片转化为 base64 -async def get_pic_base64(content) -> str: - im = await zipPic(content) +async def get_pic_base64(content: bytes) -> str: + im = await zip_pic(content) if type(im) == list: image_buffer = BytesIO() if len(im) == 1: @@ -491,18 +493,18 @@ async def fuck_pixiv(url: str) -> str: @retry(stop_max_attempt_number=5, stop_max_delay=30 * 1000) -async def dowimg(url: str, proxy: bool, get_hash: bool = False) -> str: +async def download_image(url: str, proxy: bool, get_hash: bool = False) -> str: try: # 默认超时时长为 5 秒,为了减少超时前图片没完成下载的发生频率,暂时先禁用后观察 - async with httpx.AsyncClient(proxies=get_Proxy(open_proxy=proxy), timeout=None) as client: + async with httpx.AsyncClient(proxies=get_proxy(open_proxy=proxy), timeout=None) as client: if config.close_pixiv_cat: url = await fuck_pixiv(url=url) referer = re.findall('([hH][tT]{2}[pP][sS]?://.*?)/.*?', url)[0] headers = {'referer': referer} pic = await client.get(url, headers=headers) # 如果图片无法访问到,直接返回 - if pic.status_code not in status_code: - logger.info(f'pic.status_code: {pic.status_code}') + if pic.STATUS_CODE not in STATUS_CODE: + logger.info(f'pic.status_code: {pic.STATUS_CODE}') return None if get_hash: # 返回图像的指纹 @@ -510,7 +512,7 @@ async def dowimg(url: str, proxy: bool, get_hash: bool = False) -> str: return imagehash.average_hash(im) return await get_pic_base64(pic.content) except Exception as e: - logger.error(f'图片[{url}]下载失败,将重试 \nE: {e}') + logger.error(f'图片[{url}]下载失败,将重试 \n {e}') raise @@ -520,7 +522,7 @@ async def handle_img(html, img_proxy: bool, img_num: int) -> str: # 处理图片 doc_img = html('img') for img in doc_img.items(): - img_base64 = await dowimg(img.attr("src"), img_proxy) + img_base64 = await download_image(img.attr("src"), img_proxy) if img_base64: img_str += '[CQ:image,file=base64://' + img_base64 + ']' else: @@ -531,7 +533,7 @@ async def handle_img(html, img_proxy: bool, img_num: int) -> str: if doc_video: img_str += '视频封面:' for video in doc_video.items(): - img_base64 = await dowimg(video.attr("poster"), img_proxy) + img_base64 = await download_image(video.attr("poster"), img_proxy) if img_base64: img_str += '[CQ:image,file=base64://' + img_base64 + ']' else: @@ -544,7 +546,7 @@ async def handle_img(html, img_proxy: bool, img_num: int) -> str: img_list = img_list[:img_num] img_str += f'\n因启用图片数量限制,目前只有 {img_num} 张图片:' for img_tmp in img_list: - img_base64 = await dowimg(img_tmp, img_proxy) + img_base64 = await download_image(img_tmp, img_proxy) if img_base64: img_str += '[CQ:image,file=base64://' + img_base64 + ']' else: @@ -553,7 +555,7 @@ async def handle_img(html, img_proxy: bool, img_num: int) -> str: # 一个网站的 RSS 源 description 标签内容格式为: 'Image: ...' image_search = re.search(r'Image: (https?://\S*)', str(html)) if image_search: - img_base64 = await dowimg(image_search.group(1), img_proxy) + img_base64 = await download_image(image_search.group(1), img_proxy) if img_base64: img_str += '[CQ:image,file=base64://' + img_base64 + ']' else: @@ -642,7 +644,7 @@ async def handle_translation(rss_str_tl: str) -> str: # 检查更新 -def checkUpdate(new, old) -> list: +def check_update(new: list, old: list) -> list: a = new b = old c = [] @@ -652,7 +654,7 @@ def checkUpdate(new, old) -> list: try: if i['id'] == j['id']: count = 1 - except Exception: + except ValueError: if i['link'] == j['link']: count = 1 if count == 0: @@ -674,40 +676,40 @@ def checkUpdate(new, old) -> list: # 读取记录 -def readRss(name) -> dict: +def read_rss(name) -> dict: # 检查是否存在rss记录 - if not os.path.isfile(file_path + (name + '.json')): + if not os.path.isfile(FILE_PATH + (name + '.json')): return {} - with codecs.open(file_path + (name + ".json"), 'r', 'utf-8') as load_f: + with codecs.open(FILE_PATH + (name + ".json"), 'r', 'utf-8') as load_f: load_dict = json.load(load_f) return load_dict # 写入记录 -def writeRss(name: str, new_rss: dict, new_item: list = None): +def write_rss(name: str, new_rss: dict, new_item: list = None): if new_item: max_length = len(new_rss.entries) # 防止 rss 超过设置的缓存条数 - if max_length >= config.limt: - LIMT = max_length + config.limt + if max_length >= config.limit: + LIMT = max_length + config.limit else: - LIMT = config.limt - old = readRss(name) + LIMT = config.limit + old = read_rss(name) for tmp in new_item: old['entries'].insert(0, tmp) old['entries'] = old['entries'][0:LIMT] else: old = new_rss - if not os.path.isdir(file_path): - os.makedirs(file_path) - with codecs.open(file_path + (name + ".json"), "w", 'utf-8') as dump_f: + if not os.path.isdir(FILE_PATH): + os.makedirs(FILE_PATH) + with codecs.open(FILE_PATH + (name + ".json"), "w", 'utf-8') as dump_f: dump_f.write(json.dumps(old, sort_keys=True, indent=4, ensure_ascii=False)) # 发送消息,失败重试 @retry(stop_max_attempt_number=5, stop_max_delay=30 * 1000) -async def sendMsg(rss: rss_class, msg: str, item: dict) -> bool: +async def send_msg(rss: rss_class.Rss, msg: str, item: dict) -> bool: bot, = nonebot.get_bots().values() try: if len(msg) <= 0: diff --git a/src/plugins/ELF_RSS2/RSS/translation_baidu.py b/src/plugins/ELF_RSS2/RSS/translation_baidu.py index 091fcadc..74841314 100644 --- a/src/plugins/ELF_RSS2/RSS/translation_baidu.py +++ b/src/plugins/ELF_RSS2/RSS/translation_baidu.py @@ -28,34 +28,34 @@ def baidu_translate(content): appid = config.baiduid - secretKey = config.baidukey - httpClient = None - myurl = '/api/trans/vip/translate' + secret_key = config.baidukey + http_client = None + my_url = '/api/trans/vip/translate' q = content - fromLang = 'auto' # 源语言 - toLang = 'zh' # 翻译后的语言 + from_lang = 'auto' # 源语言 + to_lang = 'zh' # 翻译后的语言 salt = random.randint(32768, 65536) - sign = str(appid) + str(q) + str(salt) + str(secretKey) + sign = str(appid) + str(q) + str(salt) + str(secret_key) sign = hashlib.md5(sign.encode()).hexdigest() - myurl = myurl + '?appid=' + str(appid) + '&q=' + urllib.parse.quote( - q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str( + my_url = my_url + '?appid=' + str(appid) + '&q=' + urllib.parse.quote( + q) + '&from=' + from_lang + '&to=' + to_lang + '&salt=' + str( salt) + '&sign=' + sign try: - httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') - httpClient.request('GET', myurl) + http_client = http.client.HTTPConnection('api.fanyi.baidu.com') + http_client.request('GET', my_url) # response是HTTPResponse对象 - response = httpClient.getresponse() - jsonResponse = response.read().decode("utf-8") # 获得返回的结果,结果为json格式 - js = json.loads(jsonResponse) # 将json格式的结果转换字典结构 + response = http_client.getresponse() + json_response = response.read().decode("utf-8") # 获得返回的结果,结果为json格式 + js = json.loads(json_response) # 将json格式的结果转换字典结构 dst = str(js["trans_result"][0]["dst"]) # 取得翻译后的文本结果 return dst # 打印结果 except Exception as e: logger.error(e) return '翻译失败:{}'.format(e) finally: - if httpClient: - httpClient.close() + if http_client: + http_client.close() if __name__ == '__main__': diff --git a/src/plugins/ELF_RSS2/RSS/util.py b/src/plugins/ELF_RSS2/RSS/util.py index f5da6645..3a99296c 100644 --- a/src/plugins/ELF_RSS2/RSS/util.py +++ b/src/plugins/ELF_RSS2/RSS/util.py @@ -13,5 +13,7 @@ async def wrapper(self, *args, **kwargs): return await asyncio.wait_for(method(self, *args, **kwargs), timeout=time) except asyncio.TimeoutError as te: logger.error(f'{self.name} 检查更新超时,结束此次任务!{te}') + return wrapper + return decorate diff --git a/src/plugins/ELF_RSS2/add_cookies.py b/src/plugins/ELF_RSS2/add_cookies.py index d92895c7..0538e3ec 100644 --- a/src/plugins/ELF_RSS2/add_cookies.py +++ b/src/plugins/ELF_RSS2/add_cookies.py @@ -1,53 +1,53 @@ from nonebot import on_command from nonebot import permission as SUPERUSER from nonebot.adapters.cqhttp import Bot, Event, permission, unescape -# from nonebot.adapters import Bot from nonebot.rule import to_me from .RSS import my_trigger as TR from .RSS import rss_class -Addcookies = on_command('addcookies', aliases={'添加cookies'}, rule=to_me( +ADD_COOKIES = on_command('addcookies', aliases={'添加cookies'}, rule=to_me( ), priority=5, permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) -@Addcookies.handle() +@ADD_COOKIES.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: - state["Addcookies"] = unescape(args) # 如果用户发送了参数则直接赋值 + state["ADD_COOKIES"] = unescape(args) # 如果用户发送了参数则直接赋值 + # 如果只有名称就把该 名称订阅 订阅到当前账号或群组 -@Addcookies.got("Addcookies", - prompt="请输入\n名称 cookies\n空格分割\n获取方式:\nPC端 chrome 浏览器按 F12\n找到Consle选项卡,输入:\ndocument.cookie\n输出的字符串就是了") -async def handle_Addcookies(bot: Bot, event: Event, state: dict): - rss_cookies = unescape(state["Addcookies"]) +@ADD_COOKIES.got("ADD_COOKIES", + prompt="请输入\n名称 cookies\n空格分割\n获取方式:\nPC端 chrome 浏览器按 F12\n找到Consle选项卡,输入:\ndocument.cookie\n输出的字符串就是了") +async def handle_add_cookies(bot: Bot, event: Event, state: dict): + rss_cookies = unescape(state["ADD_COOKIES"]) dy = rss_cookies.split(' ', 1) - rss = rss_class.rss(name='', url='', user_id='-1', group_id='-1') + rss = rss_class.Rss(name='', url='', user_id='-1', group_id='-1') # 判断是否有该名称订阅 try: name = dy[0] - except Exception: - await Addcookies.send('❌ 输入的订阅名为空!') + except ValueError: + await ADD_COOKIES.send('❌ 输入的订阅名为空!') return - if not rss.findName(name=name): - await Addcookies.send('❌ 不存在该订阅: {}'.format(name)) + if not rss.find_name(name=name): + await ADD_COOKIES.send('❌ 不存在该订阅: {}'.format(name)) return - rss = rss.findName(name=name) + rss = rss.find_name(name=name) try: cookies = dy[1] - except Exception: - await Addcookies.send('❌ 输入的cookies为空!') + except ValueError: + await ADD_COOKIES.send('❌ 输入的cookies为空!') return rss.name = name - if rss.setCookies(cookies): - await TR.addJob(rss) - await Addcookies.send('👏 {}的Cookies添加成功!\nCookies:{}\n'.format(rss.name, rss.cookies)) + if rss.set_cookies(cookies): + await TR.add_job(rss) + await ADD_COOKIES.send('👏 {}的Cookies添加成功!\nCookies:{}\n'.format(rss.name, rss.cookies)) else: - await Addcookies.send('👏 {}的Cookies添加失败!\nCookies:{}\n'.format(rss.name, rss.cookies)) + await ADD_COOKIES.send('👏 {}的Cookies添加失败!\nCookies:{}\n'.format(rss.name, rss.cookies)) diff --git a/src/plugins/ELF_RSS2/add_dy.py b/src/plugins/ELF_RSS2/add_dy.py index e00b2439..f18725dc 100644 --- a/src/plugins/ELF_RSS2/add_dy.py +++ b/src/plugins/ELF_RSS2/add_dy.py @@ -6,23 +6,23 @@ from .RSS import rss_class from .RSS import my_trigger as TR -RssAdd = on_command('add', aliases={'添加订阅', 'sub'}, rule=to_me( +RSS_ADD = on_command('add', aliases={'添加订阅', 'sub'}, rule=to_me( ), priority=5, permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) -@RssAdd.handle() +@RSS_ADD.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: - state["RssAdd"] = unescape(args) # 如果用户发送了参数则直接赋值 + state["RSS_ADD"] = unescape(args) # 如果用户发送了参数则直接赋值 # 如果只有名称就把该 名称订阅 订阅到当前账号或群组 -@RssAdd.got("RssAdd", - prompt="请输入\n名称 [订阅地址]\n空格分割、[]表示可选\n私聊默认订阅到当前账号,群聊默认订阅到当前群组\n更多信息可通过 change 命令修改") -async def handle_RssAdd(bot: Bot, event: Event, state: dict): - rss_dy_link = unescape(state["RssAdd"]) +@RSS_ADD.got("RSS_ADD", + prompt="请输入\n名称 [订阅地址]\n空格分割、[]表示可选\n私聊默认订阅到当前账号,群聊默认订阅到当前群组\n更多信息可通过 change 命令修改") +async def handle_rss_add(bot: Bot, event: Event, state: dict): + rss_dy_link = unescape(state["RSS_ADD"]) user_id = event.user_id group_id = None if event.message_type == 'group': @@ -30,30 +30,30 @@ async def handle_RssAdd(bot: Bot, event: Event, state: dict): dy = rss_dy_link.split(' ') - rss = rss_class.rss(name='', url='', user_id='-1', group_id='-1') + rss = rss_class.Rss(name='', url='', user_id='-1', group_id='-1') # 判断是否有该名称订阅,有就将当前qq或群加入订阅 try: name = dy[0] - except Exception: - await RssAdd.send('❌ 输入的订阅名为空!') + except ValueError: + await RSS_ADD.send('❌ 输入的订阅名为空!') return - if rss.findName(name=name): - rss = rss.findName(name=name) + if rss.find_name(name=name): + rss = rss.find_name(name=name) if group_id: - rss.addGroup(group=group_id) - await TR.addJob(rss) - await RssAdd.send('👏 订阅到当前群组成功!') + rss.add_group(group=group_id) + await TR.add_job(rss) + await RSS_ADD.send('👏 订阅到当前群组成功!') else: - rss.addUser(user=user_id) - await TR.addJob(rss) - await RssAdd.send('👏 订阅到当前账号成功!') + rss.add_user(user=user_id) + await TR.add_job(rss) + await RSS_ADD.send('👏 订阅到当前账号成功!') return try: url = dy[1] - except Exception: - await RssAdd.send('❌ 输入的订阅地址为空!') + except ValueError: + await RSS_ADD.send('❌ 输入的订阅地址为空!') return # 去除判断,订阅链接不再唯一,可不同名同链接 @@ -63,21 +63,21 @@ async def handle_RssAdd(bot: Bot, event: Event, state: dict): # if group_id: # rss.addGroup(group=group_id) # await TR.addJob(rss) - # await RssAdd.send('当前订阅地址已存在,将 {} 订阅到当前群组成功!'.format(rss.name)) + # await RSS_ADD.send('当前订阅地址已存在,将 {} 订阅到当前群组成功!'.format(rss.name)) # else: # rss.addUser(user=user_id) # await TR.addJob(rss) - # await RssAdd.send('当前订阅地址已存在,将 {} 订阅到当前账号成功!'.format(rss.name)) + # await RSS_ADD.send('当前订阅地址已存在,将 {} 订阅到当前账号成功!'.format(rss.name)) # return # 当前名称、url都不存在 rss.name = name rss.url = url if group_id: - rss.addGroup(group=group_id) - await TR.addJob(rss) - await RssAdd.send('👏 订阅到当前群组成功!') + rss.add_group(group=group_id) + await TR.add_job(rss) + await RSS_ADD.send('👏 订阅到当前群组成功!') else: - rss.addUser(user=user_id) - await TR.addJob(rss) - await RssAdd.send('👏 订阅到当前账号成功!') + rss.add_user(user=user_id) + await TR.add_job(rss) + await RSS_ADD.send('👏 订阅到当前账号成功!') diff --git a/src/plugins/ELF_RSS2/change_dy.py b/src/plugins/ELF_RSS2/change_dy.py index 69b00fec..b157949e 100644 --- a/src/plugins/ELF_RSS2/change_dy.py +++ b/src/plugins/ELF_RSS2/change_dy.py @@ -15,36 +15,36 @@ # 存储目录 # file_path = './data/' -RssChange = on_command('change', aliases={'修改订阅', 'moddy'}, rule=to_me(), priority=5, - permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) +RSS_CHANGE = on_command('change', aliases={'修改订阅', 'moddy'}, rule=to_me(), priority=5, + permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) -@RssChange.handle() +@RSS_CHANGE.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() if args: - state["RssChange"] = unescape(args) # 如果用户发送了参数则直接赋值 + state["RSS_CHANGE"] = unescape(args) # 如果用户发送了参数则直接赋值 else: - await RssChange.send('请输入要修改的订阅' - '\n订阅名 属性=,值' - '\n如:' - '\ntest qq=,123,234 qun=-1' - '\n对应参数:' - '\n订阅链接-url QQ-qq 群-qun 更新频率-time' - '\n代理-proxy 翻译-tl 仅title-ot,仅图片-op' - '\n下载种子-downopen 白名单关键词-wkey 黑名单关键词-bkey 种子上传到群-upgroup' - '\n去重模式-mode' - '\n图片数量限制-img_num 最多一条消息只会发送指定数量的图片,防止刷屏' - '\n注:' - '\nproxy、tl、ot、op、downopen、upgroup 值为 1/0' - '\n去重模式分为按链接(link)、标题(title)、图片(image)判断' - '\n其中 image 模式,出于性能考虑以及避免误伤情况发生,生效对象限定为只带 1 张图片的消息,' - '\n此外,如果属性中带有 or 说明判断逻辑是任一匹配即去重,默认为全匹配' - '\n白名单关键词支持正则表达式,匹配时推送消息及下载,设为空(wkey=)时不生效' - '\n黑名单关键词同白名单一样,只是匹配时不推送,两者可以一起用' - '\nQQ、群号、去重模式前加英文逗号表示追加,-1设为空' - '\n各个属性空格分割' - '\n详细:https://oy.mk/ckL') + await RSS_CHANGE.send('请输入要修改的订阅' + '\n订阅名 属性=,值' + '\n如:' + '\ntest qq=,123,234 qun=-1' + '\n对应参数:' + '\n订阅链接-url QQ-qq 群-qun 更新频率-time' + '\n代理-proxy 翻译-tl 仅title-ot,仅图片-op' + '\n下载种子-downopen 白名单关键词-wkey 黑名单关键词-bkey 种子上传到群-upgroup' + '\n去重模式-mode' + '\n图片数量限制-img_num 最多一条消息只会发送指定数量的图片,防止刷屏' + '\n注:' + '\nproxy、tl、ot、op、downopen、upgroup 值为 1/0' + '\n去重模式分为按链接(link)、标题(title)、图片(image)判断' + '\n其中 image 模式,出于性能考虑以及避免误伤情况发生,生效对象限定为只带 1 张图片的消息,' + '\n此外,如果属性中带有 or 说明判断逻辑是任一匹配即去重,默认为全匹配' + '\n白名单关键词支持正则表达式,匹配时推送消息及下载,设为空(wkey=)时不生效' + '\n黑名单关键词同白名单一样,只是匹配时不推送,两者可以一起用' + '\nQQ、群号、去重模式前加英文逗号表示追加,-1设为空' + '\n各个属性空格分割' + '\n详细:https://oy.mk/ckL') # 处理带多个值的订阅参数 @@ -71,7 +71,7 @@ def handle_property(value: str, property_list: list) -> list: # 处理要修改的订阅参数 -def handle_change_list(rss: rss_class.rss, key_to_change: str, value_to_change: str, group_id: int): +def handle_change_list(rss: rss_class.Rss, key_to_change: str, value_to_change: str, group_id: int): # 暂时禁止群管理员修改 QQ / 群号,如要取消订阅可以使用 deldy 命令 if (key_to_change in ['qq', 'qun'] and not group_id) or key_to_change == 'mode': value_to_change = handle_property(value_to_change, getattr(rss, attribute_dict[key_to_change])) @@ -92,9 +92,9 @@ def handle_change_list(rss: rss_class.rss, key_to_change: str, value_to_change: setattr(rss, attribute_dict.get(key_to_change), value_to_change) -@RssChange.got("RssChange", prompt='') -async def handle_RssAdd(bot: Bot, event: Event, state: dict): - change_info = unescape(state["RssChange"]) +@RSS_CHANGE.got("RSS_CHANGE", prompt='') +async def handle_rss_change(bot: Bot, event: Event, state: dict): + change_info = unescape(state["RSS_CHANGE"]) group_id = None if event.message_type == 'group': group_id = event.group_id @@ -102,14 +102,14 @@ async def handle_RssAdd(bot: Bot, event: Event, state: dict): name = change_list[0] change_list.pop(0) - rss = rss_class.rss(name, '', '-1', '-1') - if not rss.findName(name=name): - await RssChange.send(f'❌ 订阅 {name} 不存在!') + rss = rss_class.Rss(name, '', '-1', '-1') + if not rss.find_name(name=name): + await RSS_CHANGE.send(f'❌ 订阅 {name} 不存在!') return - rss = rss.findName(name=name) + rss = rss.find_name(name=name) if group_id and str(group_id) not in rss.group_id: - await RssChange.send(f'❌ 修改失败,当前群组无权操作订阅:{rss.name}') + await RSS_CHANGE.send(f'❌ 修改失败,当前群组无权操作订阅:{rss.name}') return try: @@ -118,26 +118,27 @@ async def handle_RssAdd(bot: Bot, event: Event, state: dict): if key_to_change in attribute_dict.keys(): # 对用户输入的去重模式参数进行校验 mode_property_set = {'', '-1', 'link', 'title', 'image', 'or'} - if key_to_change == 'mode' and (set(value_to_change.split(',')) - mode_property_set or value_to_change == 'or'): - await RssChange.send(f'❌ 去重模式参数错误!\n{change_dict}') + if key_to_change == 'mode' and ( + set(value_to_change.split(',')) - mode_property_set or value_to_change == 'or'): + await RSS_CHANGE.send(f'❌ 去重模式参数错误!\n{change_dict}') return handle_change_list(rss, key_to_change, value_to_change, group_id) else: - await RssChange.send(f'❌ 参数错误或无权修改!\n{change_dict}') + await RSS_CHANGE.send(f'❌ 参数错误或无权修改!\n{change_dict}') return # 参数解析完毕,写入 - rss.writeRss() + rss.write_rss() # 加入定时任务 - await TR.addJob(rss) + await TR.add_job(rss) if group_id: # 隐私考虑,群组下不展示除当前群组外的群号和QQ # 奇怪的逻辑,群管理能修改订阅消息,这对其他订阅者不公平。 rss.group_id = [str(group_id), '*'] rss.user_id = ['*'] - await RssChange.send(f'👏 修改成功\n{rss}') + await RSS_CHANGE.send(f'👏 修改成功\n{rss}') logger.info(f'👏 修改成功\n{rss}') except Exception as e: - await RssChange.send(f'❌ 参数解析出现错误!\nE: {e}') + await RSS_CHANGE.send(f'❌ 参数解析出现错误!\nE: {e}') logger.error(f'❌ 参数解析出现错误!\nE: {e}') raise diff --git a/src/plugins/ELF_RSS2/config.py b/src/plugins/ELF_RSS2/config.py index 2cd44ec0..6fd6f043 100644 --- a/src/plugins/ELF_RSS2/config.py +++ b/src/plugins/ELF_RSS2/config.py @@ -14,7 +14,7 @@ class Config: rsshub: AnyHttpUrl = 'https://rsshub.app' rsshub_backup: List[AnyHttpUrl] = [] db_cache_expire = 30 - limt = 50 + limit = 50 zip_size: int = 2 * 1024 @@ -35,7 +35,7 @@ class Config: is_open_auto_down_torrent: bool = False qb_web_url: str = 'http://127.0.0.1:8081' qb_down_path: str = '' # qb的文件下载地址,这个地址必须是 go-cqhttp能访问到的 - down_status_msg_grou: List[int] = [] + down_status_msg_group: List[int] = [] down_status_msg_date: int = 10 max_length: int = 0 # 正文长度限制,防止消息太长刷屏 diff --git a/src/plugins/ELF_RSS2/del_dy.py b/src/plugins/ELF_RSS2/del_dy.py index 5e5a4b30..e5cb6089 100644 --- a/src/plugins/ELF_RSS2/del_dy.py +++ b/src/plugins/ELF_RSS2/del_dy.py @@ -10,42 +10,42 @@ from .RSS import rss_class from .RSS import my_trigger as TR -scheduler = require("nonebot_plugin_apscheduler").scheduler +SCHEDULER = require("nonebot_plugin_apscheduler").scheduler # 存储目录 -file_path = str(str(Path.cwd()) + os.sep+'data' + os.sep) +FILE_PATH = str(str(Path.cwd()) + os.sep + 'data' + os.sep) -Rssdel = on_command('deldy', aliases={'drop', '删除订阅'}, rule=to_me( +RSS_DELETE = on_command('deldy', aliases={'drop', '删除订阅'}, rule=to_me( ), priority=5, permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) -@Rssdel.handle() +@RSS_DELETE.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: - state["Rssdel"] = unescape(args) # 如果用户发送了参数则直接赋值 + state["RSS_DELETE"] = unescape(args) # 如果用户发送了参数则直接赋值 -@Rssdel.got("Rssdel", prompt="输入要删除的订阅名") -async def handle_RssAdd(bot: Bot, event: Event, state: dict): - rss_name = unescape(state["Rssdel"]) +@RSS_DELETE.got("RSS_DELETE", prompt="输入要删除的订阅名") +async def handle_rss_delete(bot: Bot, event: Event, state: dict): + rss_name = unescape(state["RSS_DELETE"]) group_id = None if event.message_type == 'group': group_id = event.group_id - rss = rss_class.rss('', '', '-1', '-1') - if rss.findName(name=rss_name): - rss = rss.findName(name=rss_name) + rss = rss_class.Rss('', '', '-1', '-1') + if rss.find_name(name=rss_name): + rss = rss.find_name(name=rss_name) else: - await Rssdel.send('❌ 删除失败!不存在该订阅!') + await RSS_DELETE.send('❌ 删除失败!不存在该订阅!') return if group_id: - if rss.delGroup(group=group_id): - await TR.addJob(rss) - await Rssdel.send('👏 当前群组取消订阅 {} 成功!'.format(rss.name)) + if rss.delete_group(group=group_id): + await TR.add_job(rss) + await RSS_DELETE.send('👏 当前群组取消订阅 {} 成功!'.format(rss.name)) else: - await Rssdel.send('❌ 当前群组没有订阅: {} !'.format(rss.name)) + await RSS_DELETE.send('❌ 当前群组没有订阅: {} !'.format(rss.name)) else: - rss.delRss(rss) - await TR.delJob(rss) - await Rssdel.send('👏 订阅 {} 删除成功!'.format(rss.name)) + rss.delete_rss(rss) + await TR.delete_job(rss) + await RSS_DELETE.send('👏 订阅 {} 删除成功!'.format(rss.name)) diff --git a/src/plugins/ELF_RSS2/show_all.py b/src/plugins/ELF_RSS2/show_all.py index 0371ce26..ca5b6d19 100644 --- a/src/plugins/ELF_RSS2/show_all.py +++ b/src/plugins/ELF_RSS2/show_all.py @@ -5,39 +5,38 @@ from .RSS import rss_class -RssShowAll = on_command('showall', aliases={'selectall', '所有订阅'}, rule=to_me(), priority=5, - permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) +RSS_SHOW_ALL = on_command('showall', aliases={'selectall', '所有订阅'}, rule=to_me(), priority=5, + permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) -@RssShowAll.handle() +@RSS_SHOW_ALL.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): - user_id = event.user_id group_id = None if event.message_type == 'group': group_id = event.group_id - rss = rss_class.rss('', '', '-1', '-1') + rss = rss_class.Rss('', '', '-1', '-1') if group_id: - rss_list = rss.findGroup(group=str(group_id)) + rss_list = rss.find_group(group=str(group_id)) if not rss_list: - await RssShowAll.send('当前群组没有任何订阅!') + await RSS_SHOW_ALL.send('当前群组没有任何订阅!') return else: - rss_list = rss.readRss() + rss_list = rss.read_rss() if rss_list: if len(rss_list) == 1: - await RssShowAll.send(str(rss_list[0])) + await RSS_SHOW_ALL.send(str(rss_list[0])) else: flag = 0 info = '' for rss_tmp in rss_list: if flag % 5 == 0 and flag != 0: - await RssShowAll.send(str(info[:-2])) + await RSS_SHOW_ALL.send(str(info[:-2])) info = '' info += 'Name:{}\nURL:{}\n\n'.format(rss_tmp.name, rss_tmp.url) flag += 1 - await RssShowAll.send(info+'共 {} 条订阅'.format(flag)) + await RSS_SHOW_ALL.send(info + '共 {} 条订阅'.format(flag)) else: - await RssShowAll.send('当前没有任何订阅!') + await RSS_SHOW_ALL.send('当前没有任何订阅!') return diff --git a/src/plugins/ELF_RSS2/show_dy.py b/src/plugins/ELF_RSS2/show_dy.py index d3289bcd..44cab39f 100644 --- a/src/plugins/ELF_RSS2/show_dy.py +++ b/src/plugins/ELF_RSS2/show_dy.py @@ -5,14 +5,14 @@ from .RSS import rss_class -RssShow = on_command('show', aliases={'查看订阅'}, rule=to_me( +RSS_SHOW = on_command('show', aliases={'查看订阅'}, rule=to_me( ), priority=5, permission=SUPERUSER.SUPERUSER | permission.GROUP_ADMIN | permission.GROUP_OWNER) # 不带订阅名称默认展示当前群组或账号的订阅 # 带订阅名称就显示该订阅的 -@RssShow.handle() +@RSS_SHOW.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: @@ -24,48 +24,44 @@ async def handle_first_receive(bot: Bot, event: Event, state: dict): if event.message_type == 'group': group_id = event.group_id - rss = rss_class.rss('', '', '-1', '-1') + rss = rss_class.Rss('', '', '-1', '-1') if rss_name: - rss = rss.findName(str(rss_name)) + rss = rss.find_name(str(rss_name)) if not rss: - await RssShow.send('❌ 订阅 {} 不存在!'.format(rss_name)) + await RSS_SHOW.send('❌ 订阅 {} 不存在!'.format(rss_name)) return if group_id: # 隐私考虑,群组下不展示除当前群组外的群号和QQ if not str(group_id) in rss.group_id: - await RssShow.send('❌ 当前群组未订阅 {} '.format(rss_name)) + await RSS_SHOW.send('❌ 当前群组未订阅 {} '.format(rss_name)) return rss.group_id = [str(group_id), '*'] rss.user_id = ['*'] - await RssShow.send(str(rss)) + await RSS_SHOW.send(str(rss)) return if group_id: - rss_list = rss.findGroup(group=str(group_id)) + rss_list = rss.find_group(group=str(group_id)) if not rss_list: - await RssShow.send('❌ 当前群组没有任何订阅!') + await RSS_SHOW.send('❌ 当前群组没有任何订阅!') return else: - rss_list = rss.findUser(user=str(user_id)) + rss_list = rss.find_user(user=str(user_id)) if rss_list: if len(rss_list) == 1: - await RssShow.send(str(rss_list[0])) + await RSS_SHOW.send(str(rss_list[0])) else: flag = 0 info = '' for rss_tmp in rss_list: if flag % 5 == 0 and flag != 0: - await RssShow.send(str(info)) + await RSS_SHOW.send(str(info)) info = '' info += 'Name:{}\nURL:{}\n\n'.format(rss_tmp.name, rss_tmp.url) flag += 1 - await RssShow.send(info+'共 {} 条订阅'.format(flag)) + await RSS_SHOW.send(info + '共 {} 条订阅'.format(flag)) else: - await RssShow.send('❌ 当前没有任何订阅!') + await RSS_SHOW.send('❌ 当前没有任何订阅!') return - -# @RssShow.got("RssShow", prompt="") -# async def handle_RssAdd(bot: Bot, event: Event, state: dict): -# rss_name = state["RssShow"] diff --git a/src/plugins/ELF_RSS2/start.py b/src/plugins/ELF_RSS2/start.py index 93cb629f..85839462 100644 --- a/src/plugins/ELF_RSS2/start.py +++ b/src/plugins/ELF_RSS2/start.py @@ -11,19 +11,20 @@ async def start(): bot, = nonebot.get_bots().values() try: - rss = rss_class.rss('', '', '-1', '-1') - rss_list = rss.readRss() # 读取list + rss = rss_class.Rss('', '', '-1', '-1') + rss_list = rss.read_rss() # 读取list if not rss_list: raise Exception('第一次启动,你还没有订阅,记得添加哟!') for rss_tmp in rss_list: - await RT.addJob(rss_tmp) # 创建检查更新任务 + await RT.add_job(rss_tmp) # 创建检查更新任务 await bot.send_msg(message_type='private', user_id=str(list(config.superusers)[0]), message='ELF_RSS 订阅器启动成功!\nVersion: {}\nAuthor:Quan666\nhttps://github.com/Quan666/ELF_RSS'.format( config.version)) logger.info('ELF_RSS 订阅器启动成功!') except Exception as e: await bot.send_msg(message_type='private', user_id=str(list(config.superusers)[0]), - message='第一次启动,你还没有订阅,记得添加哟!\nVersion: {}\nAuthor:Quan666\nhttps://github.com/Quan666/ELF_RSS'.format( + message='第一次启动,你还没有订阅,记得添加哟!\nVersion: {' + '}\nAuthor:Quan666\nhttps://github.com/Quan666/ELF_RSS'.format( config.version)) logger.info('第一次启动,你还没有订阅,记得添加哟!') logger.debug(e) diff --git a/src/plugins/ELF_RSS2/upload_group_file.py b/src/plugins/ELF_RSS2/upload_group_file.py index 5d69b981..bbcc1cec 100644 --- a/src/plugins/ELF_RSS2/upload_group_file.py +++ b/src/plugins/ELF_RSS2/upload_group_file.py @@ -1,3 +1,5 @@ +import re + import nonebot from nonebot import on_command, logger from nonebot.adapters.cqhttp import Bot, Event @@ -28,7 +30,7 @@ async def get_qb(): return qb -def getSize(size: int) -> str: +def get_size(size: int) -> str: kb = 1024 mb = kb * 1024 gb = mb * 1024 @@ -60,7 +62,8 @@ async def check_down_status(hash: str, group_id: int): path = config.qb_down_path + tmp['name'] else: path = info['save_path'] + tmp['name'] - await upload_group_file.send(str('{}\n大小:{}\nHash: {} \n开始上传'.format(tmp['name'], getSize(info['total_size']), hash))) + await upload_group_file.send( + str('{}\n大小:{}\nHash: {} \n开始上传'.format(tmp['name'], get_size(info['total_size']), hash))) await bot.call_api('upload_group_file', group_id=group_id, file=path, name=tmp['name']) except Exception: continue @@ -71,7 +74,9 @@ async def check_down_status(hash: str, group_id: int): @upload_group_file.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): - hash = str(event.message) + hash_str = re.search('[a-f0-9]{40}', str(event.message))[0] if event.message_type == 'private': - await upload_group_file.finish('请在群聊中使用哦') - await check_down_status(hash=hash, group_id=event.group_id) + group_id = re.search('[0-9]{6,12}', str(event.message).replace(hash_str, ''))[0] + else: + group_id = event.group_id + await check_down_status(hash=hash_str, group_id=group_id) diff --git a/src/plugins/ELF_URL/elf_url.py b/src/plugins/ELF_URL/elf_url.py index 1964cc47..946cb198 100644 --- a/src/plugins/ELF_URL/elf_url.py +++ b/src/plugins/ELF_URL/elf_url.py @@ -1,38 +1,36 @@ -import json from urllib.parse import quote import httpx -from httpx import AsyncClient from nonebot import on_command, logger from nonebot.adapters.cqhttp import Bot, Event from nonebot.rule import to_me -url = on_command("短链", rule=to_me(), priority=5) +URL = on_command("短链", rule=to_me(), priority=5) -@url.handle() +@URL.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): - args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 + args = str(event.message).strip() if args: - state["url"] = args # 如果用户发送了参数则直接赋值 + state["URL"] = args -@url.got("url", prompt="输入你想要缩短的链接") -async def handle_city(bot: Bot, event: Event, state: dict): - link = state["url"] +@URL.got("URL", prompt="输入你想要缩短的链接") +async def handle_url(bot: Bot, event: Event, state: dict): + link = state["URL"] uri = await get_uri_of_url(link) - await url.finish(uri) + await URL.finish(uri) async def get_uri_of_url(url: str) -> str: - www = 'https://oy.mk/api/insert' + api = 'https://oy.mk/api/insert' # data = {"url": url} # headers = {'Content-Type': 'application/json'} async with httpx.AsyncClient(proxies={}) as client: try: url = quote(url, 'utf-8') # res = await client.post(www, headers=headers, data=json.dumps(data)) - res = await client.get(f'{www}?url={url}') + res = await client.get(f'{api}?url={url}') res = res.json() if res['code'] != 200: raise Exception('获取短链错误') diff --git a/src/plugins/ELF_URL/miyu.py b/src/plugins/ELF_URL/miyu.py index efdd0ffe..9cb6322c 100644 --- a/src/plugins/ELF_URL/miyu.py +++ b/src/plugins/ELF_URL/miyu.py @@ -5,32 +5,32 @@ from nonebot.adapters.cqhttp import Bot, Event from nonebot.rule import to_me -miyu = on_command("密语", rule=to_me(), priority=5) +MIYU = on_command("密语", rule=to_me(), priority=5) -@miyu.handle() +@MIYU.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: - state["miyu"] = args # 如果用户发送了参数则直接赋值 + state["MIYU"] = args # 如果用户发送了参数则直接赋值 -@miyu.got("miyu", prompt="发送“密语”信息,及密码,空格分割") +@MIYU.got("MIYU", prompt="发送“密语”信息,及密码,空格分割") async def handle_city(bot: Bot, event: Event, state: dict): - txt = state["miyu"] + txt = state["MIYU"] miyu_list = txt.split(' ') if len(miyu_list) < 2: - await miyu.reject("发送“密语”信息,及密码,空格分割") + await MIYU.reject("发送“密语”信息,及密码,空格分割") re = await get_miyu(miyu_list[0], miyu_list[1]) - await miyu.finish(re) + await MIYU.finish(re) async def get_miyu(message: str, passwd: str) -> str: - www = 'https://ii1.fun/cipher/insert' + api = 'https://ii1.fun/cipher/insert' data = {"message": message, "passwd": passwd} headers = {'Content-Type': 'application/json'} async with AsyncClient(proxies={}, headers=headers) as client: - data_json = await client.post(www, data=json.dumps(data)) + data_json = await client.post(api, data=json.dumps(data)) data_json = data_json.json() return '' + data_json['data']['shortUrl'] diff --git a/src/plugins/ELF_URL/oneread.py b/src/plugins/ELF_URL/oneread.py index 8f3fc62c..56747e21 100644 --- a/src/plugins/ELF_URL/oneread.py +++ b/src/plugins/ELF_URL/oneread.py @@ -5,29 +5,29 @@ from nonebot.adapters.cqhttp import Bot, Event from nonebot.rule import to_me -oneread = on_command( +ONE_READ = on_command( '阅后即焚', aliases={'阅后即焚', 'yhjf'}, rule=to_me(), priority=5) -@oneread.handle() +@ONE_READ.handle() async def handle_first_receive(bot: Bot, event: Event, state: dict): args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海 if args: - state["oneread"] = args # 如果用户发送了参数则直接赋值 + state["ONE_READ"] = args # 如果用户发送了参数则直接赋值 -@oneread.got("oneread", prompt="发送“阅后即焚”信息") +@ONE_READ.got("ONE_READ", prompt="发送“阅后即焚”信息") async def handle_city(bot: Bot, event: Event, state: dict): - txt = state["oneread"] + txt = state["ONE_READ"] re = await get_oneread(txt) - await oneread.finish(re) + await ONE_READ.finish(re) async def get_oneread(oneread: str) -> str: - www = 'https://ii1.fun/oneread/insert' + api = 'https://ii1.fun/oneread/insert' data = {"message": oneread} headers = {'Content-Type': 'application/json'} async with AsyncClient(proxies={}, headers=headers) as client: - data_json = await client.post(www, headers=headers, data=json.dumps(data)) + data_json = await client.post(api, headers=headers, data=json.dumps(data)) data_json = data_json.json() return data_json['data']['shortUrl']