打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Bilibili:修订间差异

来自电棍ottowiki
棍牧典
棍牧典留言 | 贡献 (创建页面,内容为“local bilibili = {} function bilibili.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 data = mw.http.get(url) if not data then return "获取信息失败" end local json = mw.text.jsonDecode(data.body) if json.code == 0 and jso…”)
 
 
(未显示同一用户的2个中间版本)
第1行: 第1行:
local bilibili = {}
local p = {}


function bilibili.getTitle(frame)
function p.getTitle(frame)
     local bvid = frame.args[1]
     local bvid = frame.args[1]
     if not bvid or bvid == '' then
     if not bvid or bvid == '' then
第9行: 第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 data = mw.http.get(url)
     local response = mw.http.get(url)
      
      
     if not data then
     if not response then
         return "获取信息失败"
         return "获取信息失败"
     end
     end
      
      
     local json = mw.text.jsonDecode(data.body)
    -- 检查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
第23行: 第33行:
end
end


return bilibili
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