require('idownx')

local function sup_show_page(url)
    if url:match("^http://www.youku.com/show_page/id_") then return true end
    return false
end

local function ana_show_page(url)
    local page = idown.requesturl(url)
    local epdiv = page:match('<div id="episode">(.-)</div>')
    for u in epdiv:gmatch('href="(http://.-)"') do
        idown.addtask(u)
    end
    return 200
end

local function sup_playlist_show(url)
    if url:match("^http://www.youku.com/playlist_show/id_") then return true end
    return false
end

local function ana_playlist_show(url)
    local page = idown.requesturl(url)
    for vtag in page:gmatch('<ul class="v">(.-)</ul>') do
        local u = vtag:match('<li class="v_link">.- href="(.-)" .-</li>')
	print(u)
        idown.addtask(u)
    end
    return 200
end

local dispatcher = {
    --function issupport, analyze
    {sup_show_page,     ana_show_page},
    {sup_playlist_show, ana_playlist_show},
}

function issupport(url)
    for i =1,#dispatcher do
        if dispatcher[i][1](url) then return true end
    end
    return false
end

function analyze(url)
    print('analyze by youku list')
    for i =1,#dispatcher do
        if dispatcher[i][1](url) then return dispatcher[i][2](url) end
    end
    return 
end