mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-07 06:12:44 +08:00
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
from pydantic import AnyUrl, validator, BaseModel, field_validator
|
|
import re
|
|
from urllib.parse import urlparse
|
|
|
|
SUPPORTED_PLATFORMS = {
|
|
"bilibili": r"(https?://)?(www\.)?bilibili\.com/video/[a-zA-Z0-9]+",
|
|
"youtube": r"(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w\-]+",
|
|
"douyin": "douyin",
|
|
"kuaishou": "kuaishou"
|
|
}
|
|
|
|
|
|
def is_supported_video_url(url: str) -> bool:
|
|
parsed = urlparse(url)
|
|
|
|
# 检查是否为Bilibili的短链接
|
|
if parsed.netloc == "b23.tv":
|
|
return True
|
|
|
|
for name, pattern in SUPPORTED_PLATFORMS.items():
|
|
if pattern in ["douyin", "kuaishou"]:
|
|
if pattern in url:
|
|
return True
|
|
else:
|
|
if re.match(pattern, url):
|
|
return True
|
|
return False
|
|
|
|
|
|
class VideoRequest(BaseModel):
|
|
url: AnyUrl
|
|
platform: str
|
|
|
|
@field_validator("url")
|
|
def validate_video_url(cls, v):
|
|
if not is_supported_video_url(str(v)):
|
|
raise ValueError("暂不支持该视频平台或链接格式无效")
|
|
return v
|