filetype detection

feature-unify_handlers
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
youtube-dl
requests
requests
filetype

View File

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

View File

@ -1,10 +1,12 @@
#!/usr/bin/env python3
import logging
import os
import re
import shutil
import sys
from glob import glob
import filetype
import requests
import youtube_dl
from telegram import MessageEntity
@ -50,18 +52,19 @@ def download_ydl(urls, date):
def download_raw(url, date):
if 'api.telegram.org' in url \
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
local_filename = OUT_DIR + '/' + datestr(date) + '__' + url.split('/')[-1]
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if 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