From fea447d1d3227ccd855f66bd684440432ed8b466 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Sat, 12 Mar 2022 13:32:05 +0800 Subject: [PATCH] uncache --- README.md | 1 + ytdlbot/db.py | 2 +- ytdlbot/limit.py | 9 +++++++++ ytdlbot/ytdl_bot.py | 9 +++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2ebe4bb..1054cc1 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,7 @@ direct - Download file directly sub - Subscribe to YouTube Channel unsub - Unsubscribe from YouTube Channel sub_count - Check subscription status, owner only. +uncache - Delete cache for this link ``` # Test data diff --git a/ytdlbot/db.py b/ytdlbot/db.py index 26d60fe..a398d0e 100644 --- a/ytdlbot/db.py +++ b/ytdlbot/db.py @@ -147,7 +147,7 @@ class Redis: return self.r.hget("cache", unique) def del_send_cache(self, unique): - self.r.hdel("cache", unique) + return self.r.hdel("cache", unique) class MySQL: diff --git a/ytdlbot/limit.py b/ytdlbot/limit.py index 76cd288..d9dbf47 100644 --- a/ytdlbot/limit.py +++ b/ytdlbot/limit.py @@ -207,6 +207,15 @@ class VIP(Redis, MySQL): text += "{} ==> [{}]({})\n".format(*item) return text + def del_cache(self, user_link: "str"): + unique = self.extract_canonical_link(user_link) + caches = self.r.hgetall("cache") + count = 0 + for key in caches: + if key.startswith(unique): + count += self.del_send_cache(key) + return count + class BuyMeACoffee: def __init__(self): diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index ef4e523..76a7019 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -143,6 +143,15 @@ def patch_handler(client: "Client", message: "types.Message"): hot_patch() +@app.on_message(filters.command(["uncache"])) +def patch_handler(client: "Client", message: "types.Message"): + username = message.from_user.username + link = message.text.split()[1] + if username == OWNER: + count = VIP().del_cache(link) + message.reply_text(f"{count} cache(s) deleted.", quote=True) + + @app.on_message(filters.command(["ping"])) def ping_handler(client: "Client", message: "types.Message"): chat_id = message.chat.id