formate file by date of message

This commit is contained in:
Tomáš Mládek 2018-01-31 13:28:54 +01:00 committed by Tomáš Mládek
parent 2d1b968780
commit 4798507629

View file

@ -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))