模块:Bilibili:修订间差异
来自电棍ottowiki
更多操作
(未显示同一用户的1个中间版本) | |||
第1行: | 第1行: | ||
local p = {} | local p = {} | ||
第10行: | 第9行: | ||
-- 调用B站API获取视频信息 | -- 调用B站API获取视频信息 | ||
local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid | local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid | ||
local | local response = mw.http.get(url) | ||
if not | if not response then | ||
return "获取信息失败" | return "获取信息失败" | ||
end | end | ||
local json = mw.text.jsonDecode | -- 检查HTTP状态码 | ||
if json.code == 0 and json.data then | if response.status ~= 200 then | ||
return "API请求失败 (HTTP " .. response.status .. ")" | |||
end | |||
-- 安全解析JSON | |||
local success, json = pcall(mw.text.jsonDecode, response.body) | |||
if not success or not json then | |||
return "解析数据失败" | |||
end | |||
if json.code == 0 and json.data and json.data.title then | |||
return json.data.title -- 返回视频标题 | return json.data.title -- 返回视频标题 | ||
else | else | ||
return " | return "视频不存在或获取失败" | ||
end | end | ||
end | end | ||
return p | return p |
2025年6月15日 (日) 09:44的最新版本
此模块的文档可以在模块:Bilibili/doc创建
local p = {}
function p.getTitle(frame)
local bvid = frame.args[1]
if not bvid or bvid == '' then
return "无效BV号"
end
-- 调用B站API获取视频信息
local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid
local response = mw.http.get(url)
if not response then
return "获取信息失败"
end
-- 检查HTTP状态码
if response.status ~= 200 then
return "API请求失败 (HTTP " .. response.status .. ")"
end
-- 安全解析JSON
local success, json = pcall(mw.text.jsonDecode, response.body)
if not success or not json then
return "解析数据失败"
end
if json.code == 0 and json.data and json.data.title then
return json.data.title -- 返回视频标题
else
return "视频不存在或获取失败"
end
end
return p