From cfa590aa5485ee4ba99fe97263861537f761cc71 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 8 Aug 2022 14:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Try=20to=20fix=20so=20much=20upd?= =?UTF-8?q?ate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ci.py | 1 - defs/format_time.py | 5 ++--- defs/msg.py | 9 ++++----- defs/source.py | 40 ++++++++++++++++++---------------------- defs/subs.py | 6 +----- defs/utils.py | 19 +++++++++---------- plugins/inline.py | 5 +---- plugins/ping.py | 2 +- plugins/track.py | 12 ++++++------ requirements.txt | 3 +-- 10 files changed, 43 insertions(+), 59 deletions(-) diff --git a/ci.py b/ci.py index 4458be2..3031f85 100644 --- a/ci.py +++ b/ci.py @@ -3,7 +3,6 @@ from configparser import RawConfigParser from os import sep, mkdir from os.path import exists -import pyromod.listen from pyrogram import Client from apscheduler.schedulers.asyncio import AsyncIOScheduler from httpx import AsyncClient, get diff --git a/defs/format_time.py b/defs/format_time.py index 7a0e1dc..da87acf 100644 --- a/defs/format_time.py +++ b/defs/format_time.py @@ -1,5 +1,4 @@ -from datetime import datetime, timedelta - +from datetime import datetime, timedelta, timezone date_format = "%Y-%m-%dT%H:%M:%SZ" @@ -16,7 +15,7 @@ def strf_time(data: str) -> str: def now_time() -> str: # UTC - ts = datetime.utcnow() + ts = datetime.now(timezone.utc) # UTC+8 ts = ts + timedelta(hours=8) return ts.strftime("%Y/%m/%d %H:%M:%S") diff --git a/defs/msg.py b/defs/msg.py index bca2d8b..c252e2e 100644 --- a/defs/msg.py +++ b/defs/msg.py @@ -58,10 +58,9 @@ def gen_update_msg(data: Module) -> TrackMessage: data.releases[0].description.replace(r"\r\n", "\n")[:250].strip()) url = None name = None - if data.releases: - if data.releases[0].releaseAssets: - url = data.releases[0].releaseAssets[0].url - mime = data.releases[0].releaseAssets[0].name.split(".")[-1:][0] - name = data.name.replace('.', '_') + "-" + data.latestRelease + "." + mime + if data.releases and data.releases[0].releaseAssets: + url = data.releases[0].releaseAssets[0].url + mime = data.releases[0].releaseAssets[0].name.split(".")[-1:][0] + name = data.name.replace('.', '_') + "-" + data.latestRelease + "." + mime button = gen_button(data) return TrackMessage(text, url, name, button) diff --git a/defs/source.py b/defs/source.py index 40483c3..efb34f0 100644 --- a/defs/source.py +++ b/defs/source.py @@ -15,13 +15,11 @@ old_modules_index: dict = {} if exists(f"data{sep}modules.json"): with open(f"data{sep}modules.json", "r", encoding="utf-8") as file: temp_data = load(file) - for temp in temp_data: - new_modules.append(Module(temp)) + new_modules.extend(Module(temp) for temp in temp_data) if exists(f"data{sep}old_modules.json"): with open(f"data{sep}old_modules.json", "r", encoding="utf-8") as file: temp_data = load(file) - for temp in temp_data: - old_modules.append(Module(temp)) + old_modules.extend(Module(temp) for temp in temp_data) new_modules_index = {i.name: i for i in new_modules} old_modules_index = {i.name: i.latestRelease for i in old_modules} @@ -35,20 +33,20 @@ async def update_data() -> None: f.write(data.text) data = data.json() old_modules = new_modules - new_modules = [] - for i in data: - new_modules.append(Module(i)) - new_modules_index = {i.name: i for i in new_modules} + new_modules = [Module(i) for i in data] + new_modules_index.clear() + for i in new_modules: + new_modules_index[i.name] = i sqlite["update_time"] = now_time() def compare() -> List[Module]: global old_modules_index - data = [] old_modules_index = {i.name: i.latestRelease for i in old_modules} - for i in new_modules: - if i.latestRelease != old_modules_index.get(i.name, ""): - data.append(i) + data = [i for i in new_modules if i.latestRelease != old_modules_index.get(i.name, "")] + if len(data) > 10: + await update_data() + data = [] return data @@ -58,17 +56,15 @@ async def download(url: str, name: str, pack_name: str) -> (str, str): content = (await client.get(f"https://modules.lsposed.org/module/{pack_name}.json")).json() data = Module(content) url = None - if data.releases: - if data.releases[0].releaseAssets: - url = data.releases[0].releaseAssets[0].url - mime = data.releases[0].releaseAssets[0].name.split(".")[-1:][0] - name = data.name.replace('.', '_') + "-" + data.latestRelease + "." + mime - if url: - content = await client.get(url) - if content.status_code == 404: - raise FileNotFoundError - else: + if data.releases and data.releases[0].releaseAssets: + url = data.releases[0].releaseAssets[0].url + mime = data.releases[0].releaseAssets[0].name.split(".")[-1:][0] + name = data.name.replace('.', '_') + "-" + data.latestRelease + "." + mime + if not url: raise FileNotFoundError + content = await client.get(url) + if content.status_code == 404: + raise FileNotFoundError content = content.content with open(f"data{sep}{name}", 'wb') as f: f.write(content) diff --git a/defs/subs.py b/defs/subs.py index a4e2f29..58fad61 100644 --- a/defs/subs.py +++ b/defs/subs.py @@ -42,11 +42,7 @@ def gen_subs_msg(cid: int) -> str: data = value.get("subscribes", []) if cid in data: data_.append(key) - if data_: - text = subs_list_msg.format(from_list_to_name(data_)) - else: - text = subs_list_no_msg - return text + return subs_list_msg.format(from_list_to_name(data_)) if data_ else subs_list_no_msg async def send_subs_msg(cid: int, data: Module, link: str): diff --git a/defs/utils.py b/defs/utils.py index 178f28d..bda342c 100644 --- a/defs/utils.py +++ b/defs/utils.py @@ -18,8 +18,7 @@ class Release: self.isPrerelease: bool = data["isPrerelease"] assets = [] if data["releaseAssets"]: - for i in data["releaseAssets"]: - assets.append(Assets(i)) + assets.extend(Assets(i) for i in data["releaseAssets"]) self.releaseAssets: List[Assets] = assets self.releaseAssetsLen = len(assets) @@ -27,10 +26,11 @@ class Release: class Module: def __init__(self, data: dict): self.name: str = data["name"] - self.description: str = data["description"] if data["description"] else "" - self.url: str = data["url"] if data["url"] else "" - self.homepageUrl: str = data["homepageUrl"] if data["homepageUrl"] else data["url"] - self.sourceUrl: str = data["sourceUrl"] if data["sourceUrl"] else "" + self.description: str = data["description"] or "" + self.url: str = data["url"] or "" + self.homepageUrl: str = data["homepageUrl"] or data["url"] + + self.sourceUrl: str = data["sourceUrl"] or "" self.hide: bool = data["hide"] self.createdAt: str = strf_time(data["createdAt"]) text = [] @@ -43,12 +43,11 @@ class Module: self.latestRelease: str = data["latestRelease"] releases = [] if data["releases"]: - for i in data["releases"]: - releases.append(Release(i)) + releases.extend(Release(i) for i in data["releases"]) self.releases: List[Release] = releases self.updatedAt: str = releases[0].publishedAt if releases else strf_time(data["updatedAt"]) - self.summary: str = data["summary"] if data["summary"] else "" - self.scope: List[str] = data["scope"] if data["scope"] else [] + self.summary: str = data["summary"] or "" + self.scope: List[str] = data["scope"] or [] class TrackMessage: diff --git a/plugins/inline.py b/plugins/inline.py index c060797..8c88bac 100644 --- a/plugins/inline.py +++ b/plugins/inline.py @@ -35,10 +35,7 @@ async def inline_process(_: Client, query: InlineQuery): if module.scope: for i in module.scope: name += i - skip = False - for i in text: - if i not in name: - skip = True + skip = any(i not in name for i in text) if not skip: data.append(InlineQueryResultArticle( (module.summary if module.summary else module.description) + " - " + module.name, diff --git a/plugins/ping.py b/plugins/ping.py index 4e49f15..bd762b7 100644 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -4,5 +4,5 @@ from pyrogram.types import Message @Client.on_message(filters.incoming & filters.private & filters.command(["ping", ])) -async def ping_check(bot: Client, message: Message): +async def ping_check(_: Client, message: Message): await message.reply("poi ~", quote=True) diff --git a/plugins/track.py b/plugins/track.py index 6a66249..ff50e76 100644 --- a/plugins/track.py +++ b/plugins/track.py @@ -1,3 +1,4 @@ +import contextlib import traceback from asyncio import sleep from os import remove @@ -51,18 +52,16 @@ async def run_every_30_minute(): parse_mode="html", ) except Exception as e: traceback.print_exc() - try: + with contextlib.suppress(FileNotFoundError): remove(file) - except FileNotFoundError: - pass except FileNotFoundError: track_msg.url = None if not track_msg.url: try: msg = await send_track_msg(None, track_msg) except FloodWait as e: - print(f"Send document flood - Sleep for {e.x} second(s)") - await sleep(e.x + uniform(0.5, 1.0)) + print(f"Send document flood - Sleep for {e.value} second(s)") + await sleep(e.value + uniform(0.5, 1.0)) msg = await send_track_msg(None, track_msg) except ButtonUrlInvalid: print(f"Send button error") @@ -82,5 +81,6 @@ async def run_every_30_minute(): @Client.on_message(filters.incoming & filters.private & filters.chat(admin_id) & filters.command(["force_update", ])) -async def force_update(bot: Client, message: Message): +async def force_update(_: Client, message: Message): await run_every_30_minute() + await message.reply("Force update complete OK!") diff --git a/requirements.txt b/requirements.txt index 563cb74..7288a92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ -Pyrogram>=1.4.8 +Pyrogram>=2.0.35 Tgcrypto>=1.2.3 -pyromod httpx>=0.22.0 apscheduler>=3.8.1 sqlitedict>=2.0.0