From 4798507629899ebd74a5876a23eec66fecc681fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 31 Jan 2018 13:28:54 +0100 Subject: [PATCH] formate file by date of message --- robot.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/robot.py b/robot.py index 1c87fd6..df4007c 100755 --- a/robot.py +++ b/robot.py @@ -2,7 +2,6 @@ import logging import os import sys -from datetime import datetime import requests import youtube_dl @@ -27,25 +26,25 @@ def ytdl_has(url): return False -def date(): - return datetime.now().strftime("%Y-%m-%d@%H%M") +def datestr(date): + return date.strftime("%Y-%m-%d@%H%M") -def download_ydl(urls): +def download_ydl(urls, date): ydl_opts = { 'noplaylist': True, 'download_archive': DIR + '/downloaded.lst', - 'outtmpl': OUT_DIR + '/' + date() + '__%(title)s__%(id)s.%(ext)s' + 'outtmpl': OUT_DIR + '/' + datestr(date) + '__%(title)s__%(id)s.%(ext)s' } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(urls) -def download_raw(url): +def download_raw(url, date): ext = '' if 'api.telegram.org' in url and 'animation' in url and "mp4" not in url: ext = '.mp4' - local_filename = OUT_DIR + '/' + date() + '__' + url.split('/')[-1] + ext + local_filename = OUT_DIR + '/' + datestr(date) + '__' + url.split('/')[-1] + ext r = requests.get(url, stream=True) with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): @@ -63,7 +62,7 @@ def handle_url(bot, update): try: logger.info("Downloading %s" % ytdl_urls) update.message.reply_text('Downloading now...') - download_ydl(ytdl_urls) + download_ydl(ytdl_urls, update.message.date) except: type, value, _ = sys.exc_info() update.message.reply_text("Something is FUCKED: %s, %s" % (type, value)) @@ -86,7 +85,7 @@ def handle_rest(bot, update): url = bot.getFile(file).file_path update.message.reply_text('Downloading now...') logger.info("Downloading '%s'" % url) - download_raw(url) + download_raw(url, update.message.date) except: type, value, _ = sys.exc_info() update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))