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

模块:SteamPrice:修订间差异

来自骷髅女孩Wiki
无编辑摘要
无编辑摘要
 
(未显示同一用户的6个中间版本)
第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.ext.externaldata.getWebData {
--data = mw.loadJsonData("https://store.steampowered.com/api/appdetails?appids=245170&cc=cn.json")
    url = "https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview"
  , data = "json=__json"
}


 
local json_str = [[
--[[
local json_str =  
{
{
   "245170": {
   "245170": {
第27行: 第22行:
}
}
]]
]]
--[[ 解析JSON字符串
local data = mw.text.jsonDecode(json_str)
local data = mw.text.jsonDecode(json_str)
]]
-- 提取价格信息
local appid = "245170"
local appid = "245170"
local priceOverview = data[appid].data.price_overview
-- 安全提取价格信息的工具函数
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