Files
JieXi/parsers/bilibili.py
2025-11-30 19:49:25 +08:00

124 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from parsers.base import BaseParser
from typing import Dict
from urllib.parse import urlencode
class BilibiliMirParser(BaseParser):
"""哔哩哔哩解析器 - 米人API"""
def parse(self, video_url: str) -> Dict:
"""解析哔哩哔哩视频"""
try:
# 手动构建URL避免双重编码
url = f"{self.api_url}/api/bzjiexi?{urlencode({'url': video_url})}"
response = self._make_request(url)
data = response.json()
return self._extract_data(data)
except Exception as e:
raise Exception(f"哔哩哔哩解析失败(米人API): {str(e)}")
def _extract_data(self, data: Dict) -> Dict:
"""提取并标准化数据"""
try:
if data.get("code") == 200 or data.get("status") == "success":
video_data = data.get("data", {})
# 如果data是列表取第一个元素
if isinstance(video_data, list):
video_data = video_data[0] if video_data else {}
cover = video_data.get("cover", "") or video_data.get("pic", "")
video_url = video_data.get("url", "") or video_data.get("video_url", "")
title = video_data.get("title", "")
description = video_data.get("desc", "") or video_data.get("description", "")
author = video_data.get("author", "") or video_data.get("owner", {}).get("name", "")
return self._normalize_response(cover, video_url, title, description, author)
else:
raise Exception(f"解析失败: {data.get('msg', '未知错误')}")
except Exception as e:
raise Exception(f"数据提取失败: {str(e)}")
class BilibiliBugPKParser(BaseParser):
"""哔哩哔哩解析器 - BugPK API"""
def parse(self, video_url: str) -> Dict:
"""解析哔哩哔哩视频"""
try:
# 手动构建URL避免双重编码
url = f"{self.api_url}/api/bilibili?{urlencode({'url': video_url})}"
response = self._make_request(url)
data = response.json()
return self._extract_data(data)
except Exception as e:
raise Exception(f"哔哩哔哩解析失败(BugPK API): {str(e)}")
def _extract_data(self, data: Dict) -> Dict:
"""提取并标准化数据"""
try:
if data.get("code") == 200 or data.get("status") == "success":
video_data = data.get("data", {})
# 如果data是列表取第一个元素
if isinstance(video_data, list):
video_data = video_data[0] if video_data else {}
cover = video_data.get("cover", "") or video_data.get("pic", "")
video_url = video_data.get("url", "") or video_data.get("video_url", "")
title = video_data.get("title", "")
description = video_data.get("desc", "") or video_data.get("description", "")
author = video_data.get("author", "") or video_data.get("owner", {}).get("name", "")
return self._normalize_response(cover, video_url, title, description, author)
else:
raise Exception(f"解析失败: {data.get('msg', '未知错误')}")
except Exception as e:
raise Exception(f"数据提取失败: {str(e)}")
class BilibiliYaohuParser(BaseParser):
"""哔哩哔哩解析器 - 妖狐API"""
def parse(self, video_url: str) -> Dict:
"""解析哔哩哔哩视频"""
try:
# 手动构建URL避免双重编码
url = f"{self.api_url}/api/v6/video/bili?{urlencode({'key': self.api_key, 'url': video_url})}"
response = self._make_request(url, verify=False)
data = response.json()
return self._extract_data(data)
except Exception as e:
raise Exception(f"哔哩哔哩解析失败(妖狐API): {str(e)}")
def _extract_data(self, data: Dict) -> Dict:
"""提取并标准化数据"""
try:
if data.get("parse_type") == "video":
video_data = data.get("data", {})
basic = video_data.get("basic", {})
# 提取基本信息
cover = basic.get("cover", "")
title = basic.get("title", "")
description = basic.get("description", "")
author = basic.get("author", "") or basic.get("owner", {}).get("name", "")
# 提取视频URL - 优先使用data.video_url其次使用videos[0].url
video_url = video_data.get("video_url", "")
if not video_url:
videos = video_data.get("videos", [])
if isinstance(videos, list) and videos:
video_url = videos[0].get("url", "")
return self._normalize_response(cover, video_url, title, description, author)
else:
raise Exception(f"解析失败: 不支持的类型 {data.get('parse_type')}")
except Exception as e:
raise Exception(f"数据提取失败: {str(e)}")