Refactor media type detection and log when we miss it

This commit is contained in:
Luke Murphy 2021-02-27 10:52:23 +01:00
parent f6c7520f42
commit ddf7f57ca5
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

View File

@ -7,6 +7,28 @@ from urllib.parse import urlparse
import jinja2 import jinja2
from xbotlib import Bot from xbotlib import Bot
IMAGE_TYPES = (".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
AUDIO_TYPES = (".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
FILE_TYPES = ".pdf"
VIDEO_TYPES = (
".mp4",
".webm",
".flv",
".vob",
".avi",
".mov",
".qt",
".mpg",
".mpeg",
".mp4",
".m2v",
".mpe",
".3gp",
)
def add_to_db(self, message, media_post=None): def add_to_db(self, message, media_post=None):
keys = [x for x in self.db[message.room]["messages"].keys()] keys = [x for x in self.db[message.room]["messages"].keys()]
@ -39,36 +61,17 @@ def write_log(self, message, logger):
def download(self, message, logger): def download(self, message, logger):
# define media_type # define media_type
if message.url.lower().endswith( if message.url.lower().endswith(IMAGE_TYPES):
(".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
):
media_type = "images" media_type = "images"
elif message.url.lower().endswith(".pdf"): elif message.url.lower().endswith(FILE_TYPES):
media_type = "pdf" media_type = "pdf"
elif message.url.lower().endswith( elif message.url.lower().endswith(AUDIO_TYPES):
(".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm")
):
media_type = "audio" media_type = "audio"
elif message.url.lower().endswith( elif message.url.lower().endswith(VIDEO_TYPES):
(
".mp4",
".webm",
".flv",
".vob",
".avi",
".mov",
".qt",
".mpg",
".mpeg",
".mp4",
".m2v",
".mpe",
".3gp",
)
):
media_type = "video" media_type = "video"
else: else:
media_type = None media_type = None
logger.info(f"Unable to determine media type of {message.url.lower()}")
# download file # download file
data = urllib.request.urlopen(message.url).read() data = urllib.request.urlopen(message.url).read()