add a /delete command

This commit is contained in:
Tomáš Mládek 2019-05-02 15:53:49 +02:00
parent f93bc1a0eb
commit 265447ed76

View file

@ -62,11 +62,13 @@ class DelojzaBot:
dp.add_handler(CommandHandler("stats", self.tg_stats)) dp.add_handler(CommandHandler("stats", self.tg_stats))
dp.add_handler(CommandHandler("orphans", self.tg_orphan)) dp.add_handler(CommandHandler("orphans", self.tg_orphan))
dp.add_handler(CommandHandler("orphans_full", self.tg_orphan_full)) 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)) dp.add_handler(CommandHandler("version", self.tg_version))
if tumblr_keys: if tumblr_keys:
self.client = pytumblr.TumblrRestClient(*tumblr_keys) self.client = pytumblr.TumblrRestClient(*tumblr_keys)
self.last_downloaded = []
self.last_hashtag = None self.last_hashtag = None
@staticmethod @staticmethod
@ -110,6 +112,7 @@ class DelojzaBot:
'preferredcodec': 'mp3', 'preferredcodec': 'mp3',
'preferredquality': '256' 'preferredquality': '256'
}] }]
filenames = []
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(urls) ydl.download(urls)
out_dir = os.path.join(self.out_dir, subdir) 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.add(mutagen.id3.TPE1(encoding=3, text=artist))
id3.save() id3.save()
self.logger.info("Moving %s to %s..." % (globbed, out_dir)) self.logger.info("Moving %s to %s..." % (globbed, out_dir))
shutil.move(globbed, out_dir) dest = shutil.move(globbed, out_dir)
return [] filenames.append(dest)
return filenames
def download_raw(self, urls, subdir, date, _, extract=False, filename=None): def download_raw(self, urls, subdir, date, _, extract=False, filename=None):
filenames = [] filenames = []
@ -218,6 +222,7 @@ class DelojzaBot:
parse_mode=telegram.ParseMode.MARKDOWN) parse_mode=telegram.ParseMode.MARKDOWN)
for filename in filenames: for filename in filenames:
self.client.create_photo('kunsaxan', state="published", data=filename) self.client.create_photo('kunsaxan', state="published", data=filename)
self.last_downloaded = filenames
return filenames return filenames
except: except:
_, exc_value, __ = sys.exc_info() _, exc_value, __ = sys.exc_info()
@ -336,6 +341,20 @@ class DelojzaBot:
if len(tmp_reply) > 0: if len(tmp_reply) > 0:
update.message.reply_text(tmp_reply) 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): def tg_version(self, _, update):
delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \ delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \
.strftime('%Y/%m/%d - %H:%M:%S') .strftime('%Y/%m/%d - %H:%M:%S')