require('idownx')
function issupport(url)
    if url:match('^http://bilibili..-/video/') then return true end
    if url:match('^http://www.bilibili..-/video/') then return true end
    return false
end
function GetFinalUrl(url)
    local json = require('json')
   	local result = {}
   	result['autoredir'] = false
    result['headers'] = {}
    result['headers']['Download-Type'] = '0'
    result['headers']['Transfer-Mode'] = '1'
    local page,head = idown.requesturl(url, '', idown.encode(result))
    local real = getlocation(head)
    return real
end
function analyze(url)
    print('-----------------------enter analyze-----------------------')
    print('url:'..url)
    
    local json = require('json')
    require('common')
    --´ò¿ªÍøÕ¾
    page1 = idown.requesturl(url) 
    id = page1:match('cid=(%d+)')
    url2 = 'http://interface.bilibili.tv/playurl?cid='..id
    page2 = idown.requesturl(url2)
    local final = ''
    for u in page2:gmatch('<url><!%[CDATA%[(.-)]]>') do
	if u:match('f.youku.com') then u = GetFinalUrl(u) end
	print(u)
        final = final..u..'\n'
    end
    
    final = final:gsub('\n$','')
    print(final)
    
    result={}
    result.realurl = final
    result.encode = 65001
    result.redown = true
    result.title = page1:match('<h2>(.-)</h2>')..'.flv'
    result.realname = result.title
    return 1,json:encode(result)
end