init
This commit is contained in:
54
parsers/base.py
Normal file
54
parsers/base.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from abc import ABC, abstractmethod
|
||||
import requests
|
||||
from typing import Dict, Optional
|
||||
|
||||
class BaseParser(ABC):
|
||||
"""解析器基类"""
|
||||
|
||||
def __init__(self, api_url: str, api_key: Optional[str] = None, timeout: int = 30):
|
||||
self.api_url = api_url
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
|
||||
@abstractmethod
|
||||
def parse(self, video_url: str) -> Dict:
|
||||
"""
|
||||
解析视频链接
|
||||
返回统一格式:
|
||||
{
|
||||
"cover": "封面URL",
|
||||
"video_url": "视频URL",
|
||||
"title": "标题",
|
||||
"description": "简介"
|
||||
}
|
||||
"""
|
||||
pass
|
||||
|
||||
def _make_request(self, url: str, params: Dict = None, headers: Dict = None, verify: bool = True) -> requests.Response:
|
||||
"""发送HTTP请求"""
|
||||
try:
|
||||
# 设置默认请求头
|
||||
default_headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
}
|
||||
|
||||
# 合并自定义请求头
|
||||
if headers:
|
||||
default_headers.update(headers)
|
||||
|
||||
response = requests.get(url, params=params, headers=default_headers, timeout=self.timeout, verify=verify)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
except requests.RequestException as e:
|
||||
raise Exception(f"请求失败: {str(e)}")
|
||||
|
||||
def _normalize_response(self, cover: str, video_url: str, title: str, description: str) -> Dict:
|
||||
"""标准化返回数据"""
|
||||
return {
|
||||
"cover": cover or "",
|
||||
"video_url": video_url or "",
|
||||
"title": title or "",
|
||||
"description": description or ""
|
||||
}
|
||||
Reference in New Issue
Block a user