33 lines
728 B
Python
33 lines
728 B
Python
import re
|
|
from typing import Optional
|
|
|
|
from folkugat_web.model import temes as model
|
|
|
|
IMAGE_FORMATS_RE = "|".join([
|
|
'jpg', 'jpeg', 'png'
|
|
])
|
|
|
|
LINK_RES = {
|
|
model.LinkType.IMAGE: [
|
|
re.compile(rf"^.*\.({IMAGE_FORMATS_RE})$")
|
|
],
|
|
model.LinkType.PDF: [
|
|
re.compile(r"^.*\.pdf$")
|
|
],
|
|
model.LinkType.SPOTIFY: [
|
|
re.compile(r"^.*spotify\.com.*$")
|
|
],
|
|
model.LinkType.YOUTUBE: [
|
|
re.compile(r"^.*youtube\.com.*$"),
|
|
re.compile(r"^.*youtu\.be.*$"),
|
|
]
|
|
}
|
|
|
|
|
|
def guess_link_type(url: str) -> model.LinkType | None:
|
|
for link_type, regexes in LINK_RES.items():
|
|
for regex in regexes:
|
|
if regex.match(url):
|
|
return link_type
|
|
return None
|