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

模块:SteamPrice:修订间差异

来自骷髅女孩Wiki
创建页面,内容为“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 = {}
local http = require("socket.http")
-- JSON字符串
local ltn12 = require("ltn12")
-- https://store.steampowered.com/api/appdetails?appids=245170&cc=cn&filters=price_overview
local json = require("dkjson")
 
local url = "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 response = {}
 
local res, code, response_headers, status = http.request{
local json_str = [[
     url = url,
{
    sink = ltn12.sink.table(response)
  "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


if code == 200 then
-- 获取价格信息(带默认值)
    local data = json.decode(table.concat(response))
local priceOverview = getPriceOverview()
    local priceOverview = data["245170"].data.price_overview
    print("货币单位: " .. priceOverview.currency)
    print("初始价格: " .. priceOverview.initial / 100 .. "元")
    print("最终价格: " .. priceOverview.final / 100 .. "元")
    print("折扣百分比: " .. priceOverview.discount_percent .. "%")
    print("格式化的最终价格: " .. priceOverview.final_formatted)
else
    print("请求失败,状态码: " .. code)
end


function p.discount()
function p.discount()
return string.format('%s', priceOverview.discount_percent)
    if not priceOverview then
        return "N/A"  -- 默认值或错误提示
    end
    return string.format('%d%%', priceOverview.discount_percent)
end
end


function p.price()
function p.price()
return string.format('%s', priceOverview.final)
    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