From 1dfb1f67ee4c3a4de5f719e87a63e5f188d18fc6 Mon Sep 17 00:00:00 2001 From: NekoAria <990879119@qq.com> Date: Thu, 8 Jul 2021 07:28:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Optimization=20-=20=E5=B0=86=20`dict=5Fhash?= =?UTF-8?q?()`=20=E7=9A=84=E5=AE=9E=E7=8E=B0=E4=BB=8E=E9=BB=91=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E6=94=B9=E4=B8=BA=E7=99=BD=E5=90=8D=E5=8D=95=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E4=BF=AE=E6=AD=A3=20`check=5Fupdate()`=20?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=B9=B6=E5=9C=A8=20`start?= =?UTF-8?q?.py`=20=E4=B8=AD=E5=8A=A0=E5=85=A5=E6=B8=85=E9=99=A4=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=96=87=E4=BB=B6=E4=B8=AD=E5=8E=9F=E6=9C=89=20`hash`?= =?UTF-8?q?=20=E5=AD=97=E6=AE=B5=E7=9A=84=E9=80=BB=E8=BE=91=20-=20?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20`=E4=BB=8E=E6=B6=88=E6=81=AF=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6=E9=97=B4`=20=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/ELF_RSS2/RSS/rss_parsing.py | 29 +++++++++++-------------- src/plugins/ELF_RSS2/start.py | 21 ++++++++++++++++++ 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/plugins/ELF_RSS2/RSS/rss_parsing.py b/src/plugins/ELF_RSS2/RSS/rss_parsing.py index 4ed2dcaa..8579d700 100644 --- a/src/plugins/ELF_RSS2/RSS/rss_parsing.py +++ b/src/plugins/ELF_RSS2/RSS/rss_parsing.py @@ -182,7 +182,10 @@ async def start(rss: rss_class.Rss) -> None: item_msg += await handle_source(source=item["link"]) # 处理时间 - item_msg += await handle_date(date=item.get("published_parsed")) + date = item.get("published_parsed") + if not date: + date = item.get("updated_parsed") + item_msg += await handle_date(date=date) # 处理种子 try: @@ -802,15 +805,8 @@ async def handle_translation(content: str) -> str: # 将 dict 对象转换为 json 字符串后,计算哈希值,供后续比较 def dict_hash(dictionary: Dict[str, Any]) -> str: - dictionary_temp = dictionary.copy() - # 避免部分缺失 published_parsed 的消息导致检查更新出问题,进行过滤 - if dictionary.get("published_parsed"): - dictionary_temp.pop("published_parsed") - # 某些情况下,如微博带视频的消息,正文可能不一样,先过滤 - if dictionary.get("summary"): - dictionary_temp.pop("summary") - if dictionary.get("summary_detail"): - dictionary_temp.pop("summary_detail") + keys = ["id", "link", "published", "updated", "title"] + dictionary_temp = {k: dictionary[k] for k in keys if k in dictionary} d_hash = hashlib.md5() encoded = json.dumps(dictionary_temp, sort_keys=True).encode() d_hash.update(encoded) @@ -828,14 +824,15 @@ async def check_update(new: list, old: list) -> list: i["hash"] = hash_temp temp.append(i) # 将结果进行去重,避免消息重复发送 - temp = [value for index, value in enumerate(temp) if value not in temp[index + 1 :]] - # 因为最新的消息会在最上面,所以要反转处理(主要是为了那些缺失 published_parsed 的消息) - result = [] - for t in temp: - result.insert(0, t) + result = [ + value for index, value in enumerate(temp) if value not in temp[index + 1 :] + ] # 对结果按照发布时间排序 result_with_date = [ - (await handle_date(i.get("published_parsed")), i) for i in result + (await handle_date(i.get("updated_parsed")), i) + if i.get("updated_parsed") + else (await handle_date(i.get("published_parsed")), i) + for i in result ] result_with_date.sort(key=lambda tup: tup[0]) result = [i for key, i in result_with_date] diff --git a/src/plugins/ELF_RSS2/start.py b/src/plugins/ELF_RSS2/start.py index 2a2d4a8c..e72e2fe5 100644 --- a/src/plugins/ELF_RSS2/start.py +++ b/src/plugins/ELF_RSS2/start.py @@ -1,11 +1,31 @@ +import codecs import nonebot +import os +import re from nonebot import logger, on_metaevent from nonebot.adapters.cqhttp import Bot, Event, LifecycleMetaEvent +from pathlib import Path from .config import config from .RSS import rss_class from .RSS import my_trigger as rt +FILE_PATH = str(str(Path.cwd()) + os.sep + "data" + os.sep) + + +def hash_clear(): + json_paths = list(Path(FILE_PATH).glob("*.json")) + + for j in [str(i) for i in json_paths if i != "rss.json"]: + + with codecs.open(j, "r", "utf-8") as f: + lines = f.readlines() + + with codecs.open(j, "w", "utf-8") as f: + for line in lines: + if not re.search(r'"hash": "[0-9a-zA-Z]{32}",', line): + f.write(line) + async def start(): (bot,) = nonebot.get_bots().values() @@ -29,6 +49,7 @@ async def start(): ), ) logger.info("ELF_RSS 订阅器启动成功!") + hash_clear() except Exception as e: await bot.send_msg( message_type="private", From 9d9733af6332a792d70370e7885438668a8152d5 Mon Sep 17 00:00:00 2001 From: NekoAria <990879119@qq.com> Date: Thu, 8 Jul 2021 07:31:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=96=EF=B8=8F=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 9ae69f7f..acf7e6a2 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ ENVIRONMENT=prod -VERSION='v2.2.6' +VERSION='v2.2.7'