Files
2026-05-27 17:29:46 +08:00

88 lines
1.7 KiB
Python

from __future__ import annotations
from pathlib import PurePosixPath
from urllib.parse import unquote, urlsplit
IMAGE_EXTENSIONS = {
".png",
".jpg",
".jpeg",
".webp",
".bmp",
".svg",
}
GIF_EXTENSIONS = {".gif"}
AUDIO_EXTENSIONS = {
".mp3",
".wav",
".ogg",
".m4a",
".flac",
}
VIDEO_EXTENSIONS = {
".mp4",
".webm",
".mov",
".avi",
".mkv",
".wmv",
}
QT_VIDEO_PLAYER_EXTENSIONS = {
".mp4",
".mov",
".mkv",
".avi",
".wmv",
}
MARKDOWN_EXTENSIONS = {".md", ".markdown"}
WEBPAGE_EXTENSIONS = {".html", ".htm", ".xhtml"}
MEDIA_MIME_TYPES = {
".mp3": "audio/mpeg",
".wav": "audio/wav",
".ogg": "audio/ogg",
".m4a": "audio/mp4",
".flac": "audio/flac",
".mp4": "video/mp4",
".webm": "video/webm",
".mov": "video/quicktime",
".avi": "video/x-msvideo",
".mkv": "video/x-matroska",
".wmv": "video/x-ms-wmv",
}
def extension_from_url(url: str) -> str:
path = unquote(urlsplit(url).path)
return PurePosixPath(path).suffix.lower()
def media_kind(url: str) -> str | None:
ext = extension_from_url(url)
if ext in GIF_EXTENSIONS:
return "gif"
if ext in IMAGE_EXTENSIONS:
return "image"
if ext in AUDIO_EXTENSIONS:
return "audio"
if ext in VIDEO_EXTENSIONS:
return "video"
return None
def is_markdown_url(url: str) -> bool:
return extension_from_url(url) in MARKDOWN_EXTENSIONS
def is_webpage_url(url: str) -> bool:
return extension_from_url(url) in WEBPAGE_EXTENSIONS
def is_qt_video_player_url(url: str) -> bool:
return extension_from_url(url) in QT_VIDEO_PLAYER_EXTENSIONS
def media_mime_type(url: str) -> str:
return MEDIA_MIME_TYPES.get(extension_from_url(url), "")