From 265447ed7616b95c83176e9f037418fcc20554c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Thu, 2 May 2019 15:53:49 +0200 Subject: [PATCH] add a /delete command --- delojza.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/delojza.py b/delojza.py index c482fc3..1055c96 100755 --- a/delojza.py +++ b/delojza.py @@ -62,11 +62,13 @@ class DelojzaBot: dp.add_handler(CommandHandler("stats", self.tg_stats)) dp.add_handler(CommandHandler("orphans", self.tg_orphan)) dp.add_handler(CommandHandler("orphans_full", self.tg_orphan_full)) + dp.add_handler(CommandHandler("delete", self.tg_delete)) dp.add_handler(CommandHandler("version", self.tg_version)) if tumblr_keys: self.client = pytumblr.TumblrRestClient(*tumblr_keys) + self.last_downloaded = [] self.last_hashtag = None @staticmethod @@ -110,6 +112,7 @@ class DelojzaBot: 'preferredcodec': 'mp3', 'preferredquality': '256' }] + filenames = [] with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(urls) out_dir = os.path.join(self.out_dir, subdir) @@ -134,8 +137,9 @@ class DelojzaBot: id3.add(mutagen.id3.TPE1(encoding=3, text=artist)) id3.save() self.logger.info("Moving %s to %s..." % (globbed, out_dir)) - shutil.move(globbed, out_dir) - return [] + dest = shutil.move(globbed, out_dir) + filenames.append(dest) + return filenames def download_raw(self, urls, subdir, date, _, extract=False, filename=None): filenames = [] @@ -218,6 +222,7 @@ class DelojzaBot: parse_mode=telegram.ParseMode.MARKDOWN) for filename in filenames: self.client.create_photo('kunsaxan', state="published", data=filename) + self.last_downloaded = filenames return filenames except: _, exc_value, __ = sys.exc_info() @@ -336,6 +341,20 @@ class DelojzaBot: if len(tmp_reply) > 0: update.message.reply_text(tmp_reply) + def tg_delete(self, _, update): + if len(self.last_downloaded) > 0: + for file in self.last_downloaded: + update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir):])) + os.remove(file) + file_parent_dir = os.path.dirname(file) + if len(os.listdir(file_parent_dir)) == 0: + hashtag = os.path.split(file_parent_dir)[1].upper() + update.message.reply_text("Removing tag \"{}\" as it's empty...".format(hashtag)) + os.rmdir(file_parent_dir) + self.last_downloaded.clear() + else: + update.message.reply_text("Nothing to remove!") + def tg_version(self, _, update): delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \ .strftime('%Y/%m/%d - %H:%M:%S')