From 848ba3166839609b9514667d92ce8d99a1bcf670 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Sat, 12 Mar 2022 15:04:41 +0800 Subject: [PATCH] delete bad users for subscription --- ytdlbot/db.py | 3 ++- ytdlbot/limit.py | 8 ++++++-- ytdlbot/ytdl_bot.py | 14 +++++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ytdlbot/db.py b/ytdlbot/db.py index a398d0e..7d6f791 100644 --- a/ytdlbot/db.py +++ b/ytdlbot/db.py @@ -195,7 +195,8 @@ class MySQL: create table if not exists subscribe ( user_id bigint null, - channel_id varchar(256) null + channel_id varchar(256) null, + is_valid boolean default 1 null ) CHARSET=utf8mb4; """ diff --git a/ytdlbot/limit.py b/ytdlbot/limit.py index d9dbf47..dbf0095 100644 --- a/ytdlbot/limit.py +++ b/ytdlbot/limit.py @@ -187,14 +187,18 @@ class VIP(Redis, MySQL): def group_subscriber(self): # {"channel_id": [user_id, user_id, ...]} - self.cur.execute("select * from subscribe") + self.cur.execute("select * from subscribe where is_valid=1") data = self.cur.fetchall() group = {} for item in data: group.setdefault(item[1], []).append(item[0]) - logging.info("Checking peroidic subscriber...") + logging.info("Checking periodic subscriber...") return group + def deactivate_user_subscription(self, user_id: "int"): + self.cur.execute("UPDATE subscribe set is_valid=0 WHERE user_id=%s", (user_id,)) + self.con.commit() + def sub_count(self): sql = """ select user_id, channel.title, channel.link diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index 76a7019..e8d4802 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -334,14 +334,22 @@ def owner_local_callback(client: "Client", callback_query: types.CallbackQuery): def periodic_sub_check(): vip = VIP() + exceptions = pyrogram.errors.exceptions for cid, uids in vip.group_subscriber().items(): video_url = vip.has_newer_update(cid) if video_url: logging.info(f"periodic update:{video_url} - {uids}") for uid in uids: - bot_msg = app.send_message(uid, f"{video_url} is downloading...", disable_web_page_preview=True) - ytdl_download_entrance(bot_msg, app, video_url) - time.sleep(random.random()) + try: + bot_msg = app.send_message(uid, f"{video_url} is downloading...", disable_web_page_preview=True) + ytdl_download_entrance(bot_msg, app, video_url) + except(exceptions.bad_request_400.PeerIdInvalid, exceptions.bad_request_400.UserIsBlocked) as e: + logging.warning("User is blocked or deleted. %s", e) + vip.deactivate_user_subscription(uid) + except Exception as e: + logging.error("Unknown error when sending message to user. %s", e) + finally: + time.sleep(random.random() * 3) if __name__ == '__main__':