模块:CardData:修订间差异
来自夜幕之下
更多操作
无编辑摘要 标签:已被回退 |
无编辑摘要 标签:已被回退 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
local | local API = "https://data.saltedkiss.org/items/cards" | ||
-- 解析JSON | |||
local function parseJson(str) | local function parseJson(str) | ||
local ok, data = pcall(mw.text.jsonDecode, str) | |||
local ok, | if ok then | ||
if ok then return | return data | ||
end | |||
return nil | |||
end | end | ||
local function | -- HTTP请求 | ||
local | local function request(url) | ||
local res = mw.http.fetch(url) | |||
if res.status == 200 then | |||
return res.body | |||
if | end | ||
return nil | |||
return | |||
end | end | ||
function p.card(frame) | |||
if | local name = frame.args[1] or frame.args.name | ||
if not name then | |||
return "未提供角色名" | |||
end | |||
local fields = table.concat({ | |||
"stylename", | |||
"stories.*", | |||
"stories.story.*" | |||
}, ",") | |||
local url = API .. | |||
"?fields=" .. mw.uri.encode(fields) .. | |||
"&filter[stylename][_eq]=" .. mw.uri.encode(name) | |||
local raw = request(url) | |||
local | if not raw then | ||
if | return "API请求失败" | ||
end | end | ||
local | local json = parseJson(raw) | ||
if not json or not json.data or not json.data[1] then | |||
if not | return "未获取到数据" | ||
end | end | ||
local card = json.data[1] | |||
local | local html = {} | ||
table.insert(html, '<div class="card-story">') | |||
table.insert(html, '<h2>小传</h2>') | |||
if card.stories then | |||
for _,storyGroup in ipairs(card.stories) do | |||
if storyGroup.story then | |||
for _,story in ipairs(storyGroup.story) do | |||
if story.text then | |||
local text = mw.text.nowiki(story.text) | |||
text = text:gsub("\n","<br>") | |||
table.insert(html, | |||
'<p class="story-text">' .. text .. '</p>' | |||
) | |||
end | |||
end | end | ||
| 第135行: | 第80行: | ||
end | end | ||
else | |||
table.insert(html, "<p>未获取到数据</p>") | |||
end | end | ||
table.insert(html, '</div>') | |||
return table.concat(html,"\n") | |||
end | end | ||
return p | return p | ||
2026年3月11日 (三) 21:17的版本
此模块的文档可以在模块:CardData/doc创建
local p = {}
local API = "https://data.saltedkiss.org/items/cards"
-- 解析JSON
local function parseJson(str)
local ok, data = pcall(mw.text.jsonDecode, str)
if ok then
return data
end
return nil
end
-- HTTP请求
local function request(url)
local res = mw.http.fetch(url)
if res.status == 200 then
return res.body
end
return nil
end
function p.card(frame)
local name = frame.args[1] or frame.args.name
if not name then
return "未提供角色名"
end
local fields = table.concat({
"stylename",
"stories.*",
"stories.story.*"
}, ",")
local url = API ..
"?fields=" .. mw.uri.encode(fields) ..
"&filter[stylename][_eq]=" .. mw.uri.encode(name)
local raw = request(url)
if not raw then
return "API请求失败"
end
local json = parseJson(raw)
if not json or not json.data or not json.data[1] then
return "未获取到数据"
end
local card = json.data[1]
local html = {}
table.insert(html, '<div class="card-story">')
table.insert(html, '<h2>小传</h2>')
if card.stories then
for _,storyGroup in ipairs(card.stories) do
if storyGroup.story then
for _,story in ipairs(storyGroup.story) do
if story.text then
local text = mw.text.nowiki(story.text)
text = text:gsub("\n","<br>")
table.insert(html,
'<p class="story-text">' .. text .. '</p>'
)
end
end
end
end
else
table.insert(html, "<p>未获取到数据</p>")
end
table.insert(html, '</div>')
return table.concat(html,"\n")
end
return p