grande refactor
This commit is contained in:
parent
efda13b9f0
commit
bf1f60eca5
1 changed files with 48 additions and 54 deletions
74
delojza.py
74
delojza.py
|
@ -46,7 +46,7 @@ def ytdl_has(url):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def download_ydl(urls, subdir, date, extract=False):
|
def download_ydl(urls, subdir, date, extract=False, filename=None):
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
'noplaylist': True,
|
'noplaylist': True,
|
||||||
'restrictfilenames': True,
|
'restrictfilenames': True,
|
||||||
|
@ -69,9 +69,9 @@ def download_ydl(urls, subdir, date, extract=False):
|
||||||
shutil.move(globbed, out_dir)
|
shutil.move(globbed, out_dir)
|
||||||
|
|
||||||
|
|
||||||
def download_raw(url, filename, subdir, date):
|
def download_raw(urls, subdir, date, extract=False, filename=None):
|
||||||
|
for url in urls:
|
||||||
local_filename = f"{OUT_DIR}/{subdir}/" + "%s__%s" % (datestr(date), filename or url.split('/')[-1])
|
local_filename = f"{OUT_DIR}/{subdir}/" + "%s__%s" % (datestr(date), filename or url.split('/')[-1])
|
||||||
# local_filename = OUT_DIR + '/' + ("%s/" % subdir) if subdir else '' + datestr(date) + '__' + url.split('/')[-1]
|
|
||||||
r = requests.get(url, stream=True)
|
r = requests.get(url, stream=True)
|
||||||
with open(local_filename, 'wb') as f:
|
with open(local_filename, 'wb') as f:
|
||||||
for chunk in r.iter_content(chunk_size=1024):
|
for chunk in r.iter_content(chunk_size=1024):
|
||||||
|
@ -100,18 +100,15 @@ def get_first_hashtag(message):
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
def handle_url(bot, update):
|
def handle(urls, message, download, filename=None):
|
||||||
ytdl_urls = list(filter(ytdl_has,
|
tries = 0
|
||||||
map(lambda e: update.message.parse_entity(e),
|
while tries < 3:
|
||||||
filter(lambda e: e.type == 'url',
|
|
||||||
update.message.entities))))
|
|
||||||
if len(ytdl_urls) > 0:
|
|
||||||
try:
|
try:
|
||||||
logger.info("Downloading %s" % ytdl_urls)
|
logger.info("Downloading %s" % urls)
|
||||||
hashtag = get_first_hashtag(update.message)
|
hashtag = get_first_hashtag(message)
|
||||||
|
|
||||||
if hashtag == 'IGNORE':
|
if hashtag == 'IGNORE':
|
||||||
update.message.reply_text('Ignoring...')
|
message.reply_text('Ignoring...')
|
||||||
return
|
return
|
||||||
|
|
||||||
reply = 'Downloading'
|
reply = 'Downloading'
|
||||||
|
@ -120,13 +117,32 @@ def handle_url(bot, update):
|
||||||
reply += f' to "{hashtag}"'
|
reply += f' to "{hashtag}"'
|
||||||
reply += '...'
|
reply += '...'
|
||||||
|
|
||||||
if hashtag == 'AUDIO':
|
if hashtag == 'AUDIO' and download != download_raw:
|
||||||
reply += ' (And also guessing you want to extract the audio)'
|
reply += ' (And also guessing you want to extract the audio)'
|
||||||
update.message.reply_text(reply)
|
message.reply_text(reply)
|
||||||
download_ydl(ytdl_urls, hashtag or '.', update.message.date, extract=hashtag == 'AUDIO')
|
download(urls,
|
||||||
|
hashtag or '.', message.date,
|
||||||
|
extract=(hashtag == 'AUDIO'),
|
||||||
|
filename=filename)
|
||||||
|
break
|
||||||
except:
|
except:
|
||||||
type, value, _ = sys.exc_info()
|
_, exc_value, __ = sys.exc_info()
|
||||||
update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))
|
if "Timed out" not in str(exc_value):
|
||||||
|
message.reply_text("Something is FUCKED: %s" % exc_value)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
tries += 1
|
||||||
|
if tries == 3:
|
||||||
|
message.reply_text("Something is FUCKED, tried thrice and it's all wrong")
|
||||||
|
|
||||||
|
|
||||||
|
def handle_url(bot, update):
|
||||||
|
ytdl_urls = list(filter(ytdl_has,
|
||||||
|
map(lambda e: update.message.parse_entity(e),
|
||||||
|
filter(lambda e: e.type == 'url',
|
||||||
|
update.message.entities))))
|
||||||
|
if len(ytdl_urls) > 0:
|
||||||
|
handle(ytdl_urls, update.message, download_ydl)
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
|
@ -146,30 +162,8 @@ def handle_rest(bot, update):
|
||||||
elif update.message.voice is not None:
|
elif update.message.voice is not None:
|
||||||
file = update.message.voice.file_id
|
file = update.message.voice.file_id
|
||||||
if file is not None:
|
if file is not None:
|
||||||
try:
|
|
||||||
url = bot.getFile(file).file_path
|
url = bot.getFile(file).file_path
|
||||||
logger.info("Downloading %s" % url)
|
handle([url], update.message, download_raw, filename=filename)
|
||||||
hashtag = get_first_hashtag(update.message)
|
|
||||||
if hashtag == 'IGNORE':
|
|
||||||
update.message.reply_text('Ignoring...')
|
|
||||||
return
|
|
||||||
|
|
||||||
if not hashtag and update.message.audio is not None:
|
|
||||||
hashtag = 'AUDIO'
|
|
||||||
|
|
||||||
reply = 'Downloading'
|
|
||||||
if hashtag:
|
|
||||||
mkdir_p(f'{OUT_DIR}/{hashtag}')
|
|
||||||
reply += f' to "{hashtag}"'
|
|
||||||
reply += '...'
|
|
||||||
update.message.reply_text(reply)
|
|
||||||
|
|
||||||
if hashtag:
|
|
||||||
mkdir_p(f'{OUT_DIR}/{hashtag}')
|
|
||||||
download_raw(url, filename, hashtag or '.', update.message.date)
|
|
||||||
except:
|
|
||||||
type, value, _ = sys.exc_info()
|
|
||||||
update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))
|
|
||||||
|
|
||||||
|
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
|
|
Loading…
Reference in a new issue