require"idownx"

function issupport(url)
	if url:match("http://y2002.com/Songs/") then return true end
	return false
end

function analyze(url)
	local page = idown.requesturl(url)
	local title = page:match('songname = "(.-)"')
	local realurl = page:match('url = "(.-)"')

	local page2 = idown.requesturl('http://y2002.com/JS/fd.js')
	local domain = page2:match('AuthUrl%s*=%s*"(.-)"')
	local url2 = string.format('http://%s/getuuauthcode/', domain)
	
print(url2)
	local req = {}
	req.headers = { Cookie = 'uuauth=ok', Referer = url, ['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
	local page2,head2 = idown.requesturl(url2, '', idown.encode(req))
	print(page2)
	
	local tflag = page2:match('tflag=%d+')
	local pin = page2:match('pin=%w+')

	print(tflag, pin)
	local result = {}
	result.title = title
	result.realname = title..'.mp3'
	result.realurl = realurl..'?'..tflag
	result.headers = {
		Cookie = string.format('uuauth=ok; mtype=mp; %s; %s', tflag, pin),
		Referer = url,
		['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
		Range = '',
		Connection = "keep-alive",
		['Accept-Encoding'] = 'gzip,deflate,sdch'
	}
	result.debug = 1
	result.encode = 65001

	print(result.realurl)

	return 1, idown.encode(result)
end