# Linux
luarocks install luacurl
luarocks install luajson # 或 dkjson
https://www.ximalaya.com/tracks/{id}.json。play_path_64(64kbps MP3 相對路徑)。https://fdfs.xmcdn.com/ 得到完整 MP3 連結。ximalaya.lua#!/usr/bin/env lua5.1
-- 需求:luacurl, luajson
require("luacurl")
require("json")
local function fetch(url, curl_obj)
local buffer = {}
local c = curl_obj or require("luacurl").new()
c:setopt(curl.OPT_URL, url)
c:setopt(curl.OPT_WRITEFUNCTION, function(buf, data)
table.insert(buf, data)
return #data
end, buffer)
local ok = c:perform()
return ok, table.concat(buffer)
end
local function get_mp3_url(track_id)
local api = "https://www.ximalaya.com/tracks/" .. track_id .. ".json"
local ok, body = fetch(api)
if not ok then return nil, "HTTP 請求失敗" end
local data = json.decode(body)
if not data or not data.play_path_64 then
return nil, "JSON 解析失敗或欄位缺失"
end
return "https://fdfs.xmcdn.com/" .. data.play_path_64
end
-- CLI 入口
local id = tonumber(arg[1])
if not id then
print("用法: lua ximalaya.lua ")
os.exit(1)
end
local url, err = get_mp3_url(id)
if url then
print(url)
else
print("錯誤: " .. err)
end
lua ximalaya.lua 12345678
# 輸出: https://fdfs.xmcdn.com/xxx/xxx.mp3
robots.txt 與服務條款。curl -O 或 aria2c 多線程下載。短短幾十行 Lua,串接 HTTP + JSON 解析,即可獲取音頻真實連結。展示了 Lua 輕量腳本語言在「膠水腳本」場景的極致效率。