All pastes #907557 Raw Edit

Anonymous

public lua v1 · immutable
·published 2008-02-17 18:39 UTC
rendered paste body
#!/usr/bin/lua--4chan automagical imagegrabberrequire'lfs'require'socket.http'http = socket.httpset = {}set.imgq={}-------------DEFINES-------------WAIT_TIME=60MAX_NO_UPDATES=10 --if we get the page more than 10 times with no new images, stop (10 minutes with default settings)DEFAULT_DIR="/home/jlulian38/store/lol_anime/tsukihime/Type name of new folder/lol_wut/idiot_folder/dump"set.dir="" --current dirset.run=trueset.nimgcnt=0function explode(div,str)	if (div=='') then return false end	local pos,arr = 0,{}	-- for each divider found	for st,sp in function() return string.find(str,div,pos,true) end do		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider		pos = sp + 1 -- Jump past current divider	end	table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider	return arrendfunction getName(url)	t = explode("/",url)	return t[#t-2] , t[#t]	endfunction setupscrape(url)	a,b = getName(url) --get purtier name		print(a,b) -- quick test	set.name = a.."-"..b:gsub(".html","")	set.url = url	set.time=0	set.imgqp = 1 --image queue positionendfunction checkForDup(t,v)	for _,v2 in pairs(t) do		if v2==v then			return true		end	end	return falseendfunction pLog(s)	print(os.date(),s)endfunction addImage(p,v)	imgu = v:sub(p,v:find("\"",p)-1)	if not checkForDup(set.imgq,imgu) then		pLog("Adding "..imgu.." to image queue")		table.insert(set.imgq,imgu)		set.nimgcnt = 0	endendfunction scanpage()	local url=set.url	set.nimgcnt=set.nimgcnt+1	pLog("Getting page: "..url.." count"..set.nimgcnt)	b,c,h = http.request(url)	pLog("Got page: "..url.." Status: "..c)	if c ~= 200 then		set.run=false	end	links = {}	for w in string.gmatch(b,"%b<>") do		if w:find("<a") then 			table.insert(links,w) 		end	end	for _,v in pairs(links) do		img = v:find("http:\/\/.*\/src\/%d")		if img then			addImage(img,v)		end	endendfunction getimages()	if set.imgq[set.imgqp] then		local img = set.imgq[set.imgqp]		set.imgqp=set.imgqp+1		pLog("Getting "..img)		local file=set.dir.."/"..set.name.."|"..img:sub(img:gsub("4chan",""):find("%d"),#img):gsub("/src/","")		if not lfs.attributes(file) then			b,c,h=http.request(img)		else			c=403		end		if c~=200 then			pLog("Download of "..img.." failed!")		else			f=io.open(file,"w")			f:write(b)			f:close()			pLog(img.." saved as "..file)		end	endendfunction doTask()	if set.time + WAIT_TIME <= os.time() then		scanpage()		set.time=os.time()	else		getimages()	end	if set.nimgcnt >= MAX_NO_UPDATES then		set.run=false	endendfunction runscrape(url)	setupscrape(url)	while set.run do		doTask()	endendfunction checkDir()	if not lfs.attributes(set.dir) then		if not lfs.mkdir(set.dir) then			self.run=false --FAILURE		end	endendif arg[1] then	set.dir = DEFAULT_DIR .. "/" .. (arg[2] or "")	checkDir()	runscrape(arg[1])else	print("Lol need an URL")end