From ddf7f57ca508ab866da1799c34f6d3b94ae1735c Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sat, 27 Feb 2021 10:52:23 +0100 Subject: [PATCH] Refactor media type detection and log when we miss it --- LogBot/logbot.py | 51 +++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/LogBot/logbot.py b/LogBot/logbot.py index be125e6..d1ba95f 100644 --- a/LogBot/logbot.py +++ b/LogBot/logbot.py @@ -7,6 +7,28 @@ from urllib.parse import urlparse import jinja2 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): 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): # define media_type - if message.url.lower().endswith( - (".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps") - ): + if message.url.lower().endswith(IMAGE_TYPES): media_type = "images" - elif message.url.lower().endswith(".pdf"): + elif message.url.lower().endswith(FILE_TYPES): media_type = "pdf" - elif message.url.lower().endswith( - (".mp3", ".ogg", ".oga", ".mogg", ".wav", ".m4a", ".webm") - ): + elif message.url.lower().endswith(AUDIO_TYPES): media_type = "audio" - elif message.url.lower().endswith( - ( - ".mp4", - ".webm", - ".flv", - ".vob", - ".avi", - ".mov", - ".qt", - ".mpg", - ".mpeg", - ".mp4", - ".m2v", - ".mpe", - ".3gp", - ) - ): + elif message.url.lower().endswith(VIDEO_TYPES): media_type = "video" else: media_type = None + logger.info(f"Unable to determine media type of {message.url.lower()}") # download file data = urllib.request.urlopen(message.url).read()