模块:Bilibili

来自BinWiki
>琪露若2025年6月26日 (四) 09:55的版本

此模块的文档可以在模块:Bilibili/doc创建

local p = {}

local function validateBvid(bvid)
    return bvid and string.match(bvid, "^[Bb][Vv][0-9A-Za-z]+$") ~= nil
end

function p.getTitle(frame)
    local bvid = frame.args[1]
    if not validateBvid(bvid) then
        return "无效 BV 号"
    end
    
    local url = "https://api.bilibili.com/x/web-interface/view?bvid=" .. bvid
    local response = mw.ext.externalData.getWebData{
        url = url,
        format = "json"
    }
    
    if not response or response.code ~= 0 or not response.data then
        return "获取信息失败"
    end
    
    return response.data.title or "无标题"
end

-- 新增:生成 Bilibili 播放器 HTML(安全嵌入)
function p.getPlayer(frame)
    local bvid = frame.args[1]
    if not validateBvid(bvid) then
        return '<div class="error">错误:无效的 BV 号(示例:BV1GJ411x7h7)</div>'
    end

    -- 获取标题(复用你的 getTitle 方法)
    local title = p.getTitle(frame)
    
    -- 返回播放器 HTML
    return string.format([[
        <div class="bilibili-player" style="max-width:600px; margin:1em auto; border-radius:4px; overflow:hidden; box-shadow:0 2px 5px rgba(0,0,0,0.1);">
            <div style="background:#FB7299; color:white; padding:8px 12px; font-weight:bold;">
                <a href="https://www.bilibili.com/video/%s" target="_blank" style="color:white; text-decoration:none;">
                    %s
                </a>
            </div>
            <div style="position:relative; padding-bottom:56.25%%; height:0; background:#000;">
                <iframe
                    src="https://player.bilibili.com/player.html?bvid=%s&high_quality=1&danmaku=0"
                    style="position:absolute; top:0; left:0; width:100%%; height:100%%; border:none;"
                    allowfullscreen
                    scrolling="no"
                ></iframe>
            </div>
        </div>
    ]], bvid, title, bvid)
end

return p