更多操作
创建页面,内容为“local p = {} local http = require("socket.http") local ltn12 = require("ltn12") local json = require("dkjson") local url = "https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview" local response = {} local res, code, response_headers, status = http.request{ url = url, sink = ltn12.sink.table(response) } if code == 200 then local data = json.decode(table.concat(response)) local priceOverview = data["245170"].dat…” |
小无编辑摘要 |
||
| (未显示同一用户的14个中间版本) | |||
| 第1行: | 第1行: | ||
local p = {} | 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 | |||
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() | function p.discount() | ||
if not priceOverview then | |||
return "N/A" -- 默认值或错误提示 | |||
end | |||
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 | |||
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