Lua腳本獲取喜馬拉雅MP3音頻地址

字號(hào):


    下面介紹了Lua腳本獲取喜馬拉雅MP3音頻地址,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    在Linux下可以直接運(yùn)行
    #!/usr/bin/lua5.1
    --需要luacurl
    --luajson
    require("luacurl")
    require('json')
    function get_html(url, c)
    local result = { }
    if c == nil then
    c = curl.new()
    end
    c:setopt(curl.OPT_URL, url)
    c:setopt(curl.OPT_WRITEDATA, result)
    c:setopt(curl.OPT_WRITEFUNCTION, function(tab, buffer) --call back函數(shù),必須有
    table.insert(tab, buffer) --tab參數(shù)即為result,參考
    return #buffer
    end)
    local ok = c:perform()
    return ok, table.concat(result) --此table非上一個(gè)table,作用域不同
    end
    function downMp3(id)
    local url=""
    local mp3 = ""
    local ok,html = get_html(url)
    if ok then
    local result = json.decode(html)
    print(mp3..result.play_path_64)
    else
    print("error")
    end
    end
    if arg[1] and tonumber(arg[1]) then
    downMp3(arg[1])
    else
    print("請(qǐng)輸入編號(hào)")
    end