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
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()