filetype detection

This commit is contained in:
Tomáš Mládek 2018-01-31 14:34:59 +01:00
parent 0cbf5fa135
commit 81a8dcca22
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