   
   
--require "star"


--config
name_en = 'alibabaAlbum'
name_cn = 'ͰͼƬ'
prefix = 'ALBB'
ver = '3.0'
firstpagename = 'Ͱ'
firstpageurl = 'http://china.alibaba.com'
mainwidth = 940
mainheight = 700
imagewidth = 120
imageheight = 92
maxalbumsperpage = 20
maxphotosperpage = 24


--cʼ
msgprint = msgprint or print
SetDirectryName = SetDirectryName or print
AddOneAlbum = AddOneAlbum or print
AddOnePhoto = AddOnePhoto or print
InputUrl = InputUrl or print
StartGetAlbum = StartGetAlbum or print
StartDownAlbum = StartDownAlbum or print
SelectDownType = SelectDownType or print
star.DecodeEscapeUsequence = star.DecodeEscapeUsequence or function(x) return x end

---------------------------------------------------------------------------
--1.СλͼƬ
--photoһʹõֶΣid,name,preview,orign,url,
PHOTO = {mt={}}

--һͼƬ,ΪԪ
function PHOTO.new()
     local photo = {}
     setmetatable(photo,PHOTO.mt)
     return photo
end
---------------------------------------------------------------------------

---------------------------------------------------------------------------
--2.СλͼƬ
PHOTOS = {mt={}}

--ͼƬĳͼƬʱ֮
PHOTOS.mt.__index = function(t,index)
          local photo = PHOTO.new()
          t[index] = photo
          return photo;
end

--һͼƬ,ΪԪ
function PHOTOS.new()
     local photos = {}
     setmetatable(photos,PHOTOS.mt)
     return photos
end

---------------------------------------------------------------------------
--3.ᣬһһڲһͼƬ飺PHOTOS
--ʹֶΣid,name,url,preview,photos
ALBUM = {mt={}}

--һ,ΪԪ
function ALBUM.new()
     local album = {}
     album.photos = PHOTOS.new()
     setmetatable(album,ALBUM.mt)
     return album
end

---------------------------------------------------------------------------
--4.飺ALBUMS
ALBUMS = {mt={}}

--ĳʱ֮
ALBUMS.mt.__index = function(t,index)
     local album = ALBUM.new()
     album.photos = PHOTOS.new()
     --print('')
     t[index] = album
     return album;
end

--һ,ΪԪ
function ALBUMS.new()
     local albums = {}
     setmetatable(albums,ALBUMS.mt)
     return albums
end

---------------------------------------------------------------------------
--your codes
---------------------------------------------------------------------------

--var
username = '';
account = '';
member_id = ''
ndowntype = 0;
albums = ALBUMS.new() 
---------------------------------------------------------------------------

function GetOneItemAlbum(url)
	local albumcount = 0
	local html = nil
	
	html = star.gethtml(url)
	if html==nil or html=='' then
		return
	end
		
	--get id
	_,_,id = string.find(html,'offerId.-=(%d*)')

	--get name	
	_,_,name = string.find(html,[[mod%-detail%-hd.-<h1.->(.-)</h1>]])

	if id==nil or name==nil then
		msgbox('뵥Ʒַ!')
		return
	end
	
	--get preview
	_,_,src = string.find(html,[[vertical%-img.-src.-(http.-.jpg)]])
	
	albumcount = albumcount + 1
	albums[albumcount].id = id;
	albums[albumcount].url = url;
	albums[albumcount].preview = src;
	albums[albumcount].total = -1;
	albums[albumcount].name = name;
	
	name = string.gsub(name,'|','')
	SetDirectryName(name)
end

function trim(s)
	local from = '';
	if s==nil then
		return '';
	end
	from = s:match"^%s*()"
	return from > #s and "" or s:match(".*%S", from)
end

--ȡƷͼƬ
function GetCatAlbum(url)
	local albumcount = 0
	local html = nil
	local nextpageurl = url
	local catname = nil
	
	repeat
		html = star.gethtml(nextpageurl)
		if html==nil or html=='' then
			break
		end
		
		if catname==nil then
			--ȡԼ÷µĲƷ
			--_,_,catname,count = string.find(html,[[selected.-name">([^"^'^/]+)</span>.-num(%d*)]])
			_,_,catname = string.find(html,[[<title>.-keywords.-content="(.-)"]])
			catname = trim(catname)
			catname = string.gsub(catname, "|", "")
			SetDirectryName(catname);
		end
		
		_,_,html = string.find(html,[[<label>(.-)m%-footer]])
		
		bfound = false
		for href,name,preview in string.gfind(html, [["image">.-href="(.-)".-title="(.-)".-src.-(http[^"^'^%s]-.jpg)]]) do
			_,_,id = string.find(href,'/(%d+)')
			bfound = true
			albumcount = albumcount + 1
			albums[albumcount].id = id;
			albums[albumcount].url = href;
			albums[albumcount].preview = preview;
			albums[albumcount].total = -1;
			albums[albumcount].name = name;
		end
		
		if bfound==false then
			break
		end
	
		--Ƿһҳ
		p1,p2 = string.find(html,'next-disabled');
		if p1~=nil then
		  break
		end
		
		pos1,pos2,nextpageurl = string.find(html,[[class="next".-href.-(http[^"^'^%s]*)]]);    
		if nextpageurl==nil or nextpageurl=='' then
		   break
		end
		nextpageurl = string.gsub(nextpageurl,"#anchor","");
	until (false)
		
end

function GetProductAlbum(url)
	local bhascover = false
	local albumcount = 0
	local html = nil	
	local ALBUMCOUNTPERPAGE = 20
	local albumstart = 1
	local albumend = ALBUMCOUNTPERPAGE
	local feedbackUid = ''
	member_id = ''
	
	--ȡmember_id
	url = string.lower(url)
	_,_,account = string.find(url,'//(.-.com)')
	if account==nil then
		_,_,account = string.find(url,'(.-.com)')
	end
	if account~=nil then
		url = 'http://' .. account
	end
	html = star.gethtml(url)
	if html==nil or html=='' then
		return
	end


	_,_,feedbackUid = string.find(html,[[feedbackUid.-value.-"(.-)"]])
	_,_,member_id = string.find(html,[[member_id:.-([^'^"]+)]])
	if member_id==nil then
		return
	end
	if member_id:find('.uid')~=nil then
		member_id = feedbackUid
	end
	SetDirectryName(member_id)
	
	repeat 
		--url = 'http://picman.china.alibaba.com/open/ajax/RecommendAlbumDetailList.json?memberId=' .. feedbackUid .. '&hasAlbums=true&start=' .. albumstart .. '&end=' .. albumend
		url = 'http://picman.1688.com/open/ajax/RecommendAlbumDetailList.json?memberId='..feedbackUid..'&jsResponseDataName=albumList'..star.getcurrenttime(2)..'&hasAlbums=true&start='..albumstart..'&end='..albumend
		html = star.gethtml(url)
		if html==nil or html=='' then
			break
		end
		--msgbox(url)
		bfound = false
		
		--Щûзͼ
		for oneline in string.gfind(html,[[({"id".-})]]) do
			--ûз
			bhascover = false
			for id,preview,name,count in string.gfind(oneline, [["id":(%d+).-"cover".-(http.-.jpg).-"title":"(.-)".-"count":(%d+)]]) do
				bhascover = true
				preview = star.DecodeEscapeUsequence(preview)
				name = star.DecodeEscapeUsequence(name)
				bfound = true
				albumcount = albumcount + 1
				albums[albumcount].id = id;
				albums[albumcount].url = 'http://' .. member_id .. '.cn.alibaba.com/page/albumdetail_' .. id .. '.htm';
				albums[albumcount].preview = preview;
				albums[albumcount].total = tonumber(count);
				albums[albumcount].name = name;
			end
			
			if bhascover==false then
				for id,name,count in string.gfind(oneline, [["id":(%d+).-"title":"(.-)".-"count":(%d+)]]) do
					preview = star.DecodeEscapeUsequence(preview)
					name = star.DecodeEscapeUsequence(name)
					bfound = true
					albumcount = albumcount + 1
					albums[albumcount].id = id;
					albums[albumcount].url = 'http://' .. member_id .. '.cn.alibaba.com/page/albumdetail_' .. id .. '.htm';
					albums[albumcount].preview = 'http://img.china.alibaba.com/images/app/platform/winport/mod/albums/no-cover-150.gif';
					albums[albumcount].total = tonumber(count);
					albums[albumcount].name = name;
				end
			end
		end

		
		if bfound==false then
			break
		end
		
		_,_,count = string.find(html,[["total":(%d+)]])
		count = tonumber(count)
		if albumcount>=count then
			break
		end
		
		albumstart = albumstart + ALBUMCOUNTPERPAGE
		albumend = albumend + ALBUMCOUNTPERPAGE
	until (false)

end

function GetAlbums(ntype,url)
	albums = ALBUMS.new()
	ndowntype = ntype;
	if ntype==0 then
		GetOneItemAlbum(url)
	elseif ntype==1 then
		GetCatAlbum(url)
	elseif ntype==2 then
		GetProductAlbum(url)
	end
	
	for i = 1,#albums do 
		albums[i].total = tonumber(albums[i].total)
		AddOneAlbum(albums[i].id,albums[i].name,albums[i].url,albums[i].preview,albums[i].total);
	end
end

function randfloatnum()
	s = '0.'
	math.randomseed(os.time())
	for i = 1,16 do
		s = s .. math.random(0,9);
	end
	
	return s;
end

function GetOneItemPhotos(album)
	local url = nil
	local photocount = 0;
	local photos = PHOTOS.new()
	
	html = star.gethtml(album.url)
	if html==nil or html=='' then
		return
	end

	--Ԥͼ
	bfound = false
	for src in string.gfind(html, [[original.-(http[^"^'^%s]+)]]) do
		bfound = true
		photocount = photocount + 1;
		photos[photocount].preview = src
		photos[photocount].orign = src
	end
	
	--ûлȡԤͼȡͼ
	if bfound==false then
		--get cover
		_,_,previewsrc = string.find(html,[[vertical%-img.-src.-(http.-.jpg)]])
		orignsrc = string.gsub(previewsrc,'(%.%d+x%d+).jpg','.jpg')
		
		photocount = photocount + 1
		photos[photocount].preview = previewsrc
		photos[photocount].orign = orignsrc;
	end
	
	--ȡеͼƬ
	_,_,url = string.find(html,[[data%-tfs%-url.-(http[^"^'^%s]*)]])
	html = star.gethtml(url)
	if html==nil or html=='' then
		return
	end
	
	for src in string.gfind(html, [[(http[^"^']-.jpg)]]) do
		photocount = photocount + 1;
		photos[photocount].preview = src
		photos[photocount].orign = src;
	end
	for src in string.gfind(html, [[(http[^"^']-.JPG)]]) do
		photocount = photocount + 1;
		photos[photocount].preview = src
		photos[photocount].orign = src;
	end
	
	return photos;
end

function GetProductPhotos(album)
	local photocount = 0;
	local photos = PHOTOS.new()
	
	pagecount,_ =math.modf(album.total/8) 
	rest =math.mod(album.total,8)
	pagecount = pagecount + (rest~=0 and 1 or 0)
	
	for ipage = 1,pagecount do
		url = string.format('http://%s/event/app/albumGuide/imageList.htm?albumId=%s&pageNum=%d',
			account,album.id,ipage)
		html = star.gethtml(url)
		if html==nil or html=='' then
			break
		end
		
		for src in string.gfind(html, [[(http[^"^'^%s]-.jpg)]]) do
			--ͼ
			src = string.gsub(src,'(%.%d+x%d+).jpg','.jpg')
			photocount = photocount + 1;
			photos[photocount].preview = src
			photos[photocount].orign = src;
		end

	end
	
	return photos;
end

function GetPhotos(index)
	local photos = PHOTOS.new()
	
	if albums[index].name~=nil then
		msgprint('ڻȡͼƬϢ '..albums[index].name);
	end
	if ndowntype==0 then
		photos = GetOneItemPhotos(albums[index])
	elseif ndowntype==1 then
		photos = GetOneItemPhotos(albums[index])
	elseif ndowntype==2 then
		photos = GetProductPhotos(albums[index])
	else
	end
	
	for i = 1,table.getn(photos) do
		ok = AddOnePhoto(photos[i].id,photos[i].name,photos[i].preview,photos[i].orign);
		if ok==0 then
			break
		end
	end
end


function test()
	--GetAlbums(0,'http://detail.china.alibaba.com/buyer/offerdetail/574616943.html')
end

--test();