filetype detection

This commit is contained in:
Tomáš Mládek 2018-01-31 14:34:59 +01:00 committed by Tomáš Mládek
parent 9abf01b02a
commit 47eebac4d8
3 changed files with 13 additions and 8 deletions

View file

@ -1,3 +1,4 @@
python-telegram-bot python-telegram-bot
youtube-dl youtube-dl
requests requests
filetype

View file

@ -6,6 +6,7 @@
# #
certifi==2018.1.18 # via python-telegram-bot, requests certifi==2018.1.18 # via python-telegram-bot, requests
chardet==3.0.4 # via requests chardet==3.0.4 # via requests
filetype==1.0.0
future==0.16.0 # via python-telegram-bot future==0.16.0 # via python-telegram-bot
idna==2.6 # via requests idna==2.6 # via requests
python-telegram-bot==9.0.0 python-telegram-bot==9.0.0

View file

@ -1,10 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging import logging
import os import os
import re
import shutil import shutil
import sys import sys
from glob import glob from glob import glob
import filetype
import requests import requests
import youtube_dl import youtube_dl
from telegram import MessageEntity from telegram import MessageEntity
@ -50,18 +52,19 @@ def download_ydl(urls, date):
def download_raw(url, date): def download_raw(url, date):
if 'api.telegram.org' in url \ local_filename = OUT_DIR + '/' + datestr(date) + '__' + url.split('/')[-1]
and ('animation' in url or 'video' in url) \
and "mp4" not in url:
ext = '.mp4'
else:
ext = ''
local_filename = OUT_DIR + '/' + datestr(date) + '__' + url.split('/')[-1] + ext
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):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
if not re.match(r'.*\..{3,5}$', os.path.split(local_filename)[-1]):
kind = filetype.guess(local_filename)
if kind is None:
logger.error("File has no extension and could not be determined!")
else:
logger.info('Moving file without extension... %s?' % kind.extension)
shutil.move(local_filename, local_filename + '.' + kind.extension)
# noinspection PyBroadException # noinspection PyBroadException