--http://vv.video.qq.com/geturl?otype=xml&platform=1&vid=X0090pTFE16&format=2
--http://v.qq.com/video/play.html?vid=X0090pTFE16
function issupport(url)
    if url:match("^http://v.qq.com/") then return true end
    return false   
end

function analyze(url)
    require ('common')
    local json = require('json')
	print('-----------------------Enter analyze-----------------------')
	print('url:'..url)
	
	local id = url:match('vid=([%d%w]+)') 
	if not id then
	    id = GetIdAndTitle(url)
	end
	print('id:'..id)
	
	local realurl=''
	
	local ids = split(id, '|')
	for i=1,#ids do
		--组合播放页面地址
		local playerurl = string.format('http://vv.video.qq.com/geturl?otype=xml&platform=1&vid=%s&format=2', id)
		local playerpage = idown.requesturl(playerurl)
		print(playerpage)
		local url = playerpage:match("<url>(.-)</url>")
		print('realurl:'..url)
		realurl = realurl..url
		if not i==#ids then realurl = realurl..'\n' end
	end
		
	--获取文件后缀
	id = ids[1]
	filename = getfilename(realurl)
	title = GetTitleById(id)..getsuffix(filename)
	realfilename = title
	print('realfilename:'..realfilename)
	print('-----------------------Leave analyze-----------------------')
	result = {}
	result['realurl'] = realurl
	result['title'] = title
	result['realname'] = realfilename
	result['encode'] = 65001
	
	retstr = json:encode(result)
	return 1, retstr
end

function GetIdAndTitle(url)
    id = url:match('page/.-/.-/.-/(.-).html')
    if id then return id end
	
    page = idown.requesturl(url,'','')
    for match in string.gmatch(page, 'vid:"(.-)"') do
        id = match
        break
    end
    print('id:'..id)
    return id
end

function GetTitleById(id)
    url=string.format('http://sns.video.qq.com/tvideo/fcgi-bin/video?otype=json&vid=%s',id)
    print(url)
    page = idown.requesturl(url,'','')
    print(page)
    title=""
    for match in page:gmatch( '"title":"(.-)"') do
        title = match
    end
    if title:len() == 0 then
        title = id
    end
    print('title:'..title)
    return title
end