function issupport(url)
    if url:match("^http://www.xiami.com/song/%d+") then return true end
    if url:match("^http://www.xiami.com/widget/isingle%?sid=%d+") then return true end
    return false   
end

function decodeurl(encode)
--encode = '5h3%.i%31%F%5%E1E5ptA2x.2215%2E51_7E3t%FinF%4E551E6259p2fae225%E27%3%9.%F1mt1F422%7545%m'

	totallines = tonumber(string.sub(encode, 1, 1))
	matrix = string.sub(encode, 2)
	length = math.floor(string.len(matrix) / totallines)
	print('length:'..length)
	fulllines = string.len(matrix) % totallines
	print('fulllines:'..fulllines)
	if fulllines == 0 then
		fulllines = totallines
	else
		length = length + 1
	end

	array = {}
	line = 0
	--full lines
	while line < fulllines do
		local s = 1 + line * length
		local j = s + length - 1
		array[line] = string.sub(matrix, s, j)
		print(array[line])
		line = line + 1
	end
	--short lines
	while line < totallines do
		local s = 1 + fulllines * length + (line - fulllines) * (length -1)
		local j = s + length - 2
		array[line] = string.sub(matrix, s, j)
		print(array[line])
		line = line + 1
	end

	pos = 0
	realurl = ''
	while pos < string.len(matrix) do
		line = pos % totallines
		row = math.floor(pos / totallines)
		--print(string.format("pos:%d,%d", line, row))
		realurl = realurl .. string.sub(array[line], row + 1, row + 1)
		pos = pos + 1
	end
	realurl = unescape(realurl)

	realurl = string.gsub(realurl, '%^', '0')
	realurl = string.gsub(realurl, '%+', ' ')

	print(realurl)
	return realurl
end

function analyze(url)
	print('-----------------------Enter analyze-----------------------')
	print('url:'..url)
	require('xml')
    local json = require('json')
    local common = require('common')
    
	--获得音乐文件id
	local id = url:match('song/(%d+)') or url:match('sid=(%d+)')
	print('id:'..id)
	
	--请求歌曲信息页面
	playerurl = 'http://data.xiami.com/widget/xml-single/uid/0/sid/'..id
	print('playerurl:'..playerurl)
	page1 = idown.requesturl(playerurl)
	print('page:\n'..page1)

    --获得文件标题和下载地址
	local x = xml.eval(page1)
	local songname = x:find('song_name')
	local artist = x:find('artist_name')
	local location = x:find('location')

	local title = songname[1]..'-'..artist[1]
	local realurl = decodeurl(location[1])
	local realfilename = title..getsuffix(realurl)
	
	local result = {}
	result['realurl'] = realurl
	result['title'] = title
	result['realname'] = realfilename:gsub('/','-')
	result['encode'] = 65001

	retstr = json:encode(result)
	print('ret:'..retstr)
	
	return 1, retstr	
end