更多操作
小无编辑摘要 |
小无编辑摘要 |
||
(未显示同一用户的2个中间版本) | |||
第3行: | 第3行: | ||
-- https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview | -- https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview | ||
data = mw.loadJsonData("https://store.steampowered.com/api/appdetails?appids=245170&cc=cn.json") | --data = mw.loadJsonData("https://store.steampowered.com/api/appdetails?appids=245170&cc=cn.json") | ||
local json_str = [[ | |||
{ | { | ||
"245170": { | "245170": { | ||
第29行: | 第22行: | ||
} | } | ||
]] | ]] | ||
local data = mw.text.jsonDecode(json_str) | local data = mw.text.jsonDecode(json_str) | ||
local appid = "245170" | local appid = "245170" | ||
local | -- 安全提取价格信息的工具函数 | ||
local function getPriceOverview() | |||
-- 逐层检查字段是否存在 | |||
if type(data) ~= "table" then | |||
return nil, "Invalid JSON data" | |||
end | |||
local appData = data[appid] | |||
if not appData or not appData.success then | |||
return nil, "Failed to fetch app data" | |||
end | |||
local gameData = appData.data | |||
if not gameData then | |||
return nil, "No game data available" | |||
end | |||
return gameData.price_overview -- 可能为nil | |||
end | |||
-- 获取价格信息(带默认值) | |||
local priceOverview = getPriceOverview() | |||
function p.discount() | function p.discount() | ||
if not priceOverview then | |||
return "N/A" -- 默认值或错误提示 | |||
end | |||
return string.format('%d%%', priceOverview.discount_percent) | return string.format('%d%%', priceOverview.discount_percent) | ||
end | end | ||
function p.price() | function p.price() | ||
if not priceOverview or not priceOverview.final_formatted then | |||
return "价格信息不可用" -- 默认值 | |||
end | |||
return priceOverview.final_formatted | return priceOverview.final_formatted | ||
end | end | ||
return p | return p |
2025年2月21日 (五) 00:30的最新版本
此模块的文档可以在模块:SteamPrice/doc创建
local p = {}
-- JSON字符串
-- https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview
--data = mw.loadJsonData("https://store.steampowered.com/api/appdetails?appids=245170&cc=cn.json")
local json_str = [[
{
"245170": {
"success": true,
"data": {
"price_overview": {
"currency": "CNY",
"initial": 8000,
"final": 8000,
"discount_percent": 0,
"initial_formatted": "",
"final_formatted": "¥ 80.00"
}
}
}
}
]]
local data = mw.text.jsonDecode(json_str)
local appid = "245170"
-- 安全提取价格信息的工具函数
local function getPriceOverview()
-- 逐层检查字段是否存在
if type(data) ~= "table" then
return nil, "Invalid JSON data"
end
local appData = data[appid]
if not appData or not appData.success then
return nil, "Failed to fetch app data"
end
local gameData = appData.data
if not gameData then
return nil, "No game data available"
end
return gameData.price_overview -- 可能为nil
end
-- 获取价格信息(带默认值)
local priceOverview = getPriceOverview()
function p.discount()
if not priceOverview then
return "N/A" -- 默认值或错误提示
end
return string.format('%d%%', priceOverview.discount_percent)
end
function p.price()
if not priceOverview or not priceOverview.final_formatted then
return "价格信息不可用" -- 默认值
end
return priceOverview.final_formatted
end
return p