require('idownx')

function issupport(url)
    if url:match('^http://www.kuwo.cn/yinyue/%d+/') then return true end
    return false
end

function analyze(url)
    local id = url:match('yinyue/(%d+)/')
    --http://player.kuwo.cn/webmusic/st/getNewMuiseByRid?rid=MUSIC%5F1689355
    local url1 = 'http://player.kuwo.cn/webmusic/st/getNewMuiseByRid?rid=MUSIC%5F'..id
    local page1 = idown.requesturl(url1)

    --http://player.kuwo.cn/webmusic/getfdl?purl=a2%2F21%2F84%2F598368406%2Eaac
    local mp3path = page1:match('<path>(.-)</path>')
    local url2 = 'http://player.kuwo.cn/webmusic/getfdl?purl='..urlencode(mp3path)
    local page2 = idown.requesturl(url2)
    print(page2)
    
    local realurl = 'http://'..page1:match('<wmadl>(.-)</wmadl>')..'/'..page2
    print(realurl)
    
    local title = page1:match('<name>(.-)</name>')..' - '..page1:match('<singer>(.-)</singer>')
    local realname = title..getsuffix(realurl)
    
    local result = {
        title = title,
        realurl = realurl,
        realname = realname,
        encode = 65001,
    }
    return 201,idown.encode(result)
end