-- Radom
-- Script author: Piotr Beling <qwak82@gmail.com>

local base_url = "http://www.brydzradom.yoyo.pl/";

local clock = os.clock
function sleep(n)  -- seconds
	local t0 = clock()
	while clock() - t0 <= n do end
end

local success = false
local tries = 5	--we want do 5 tries
for try = 1, tries do
	local content = url.download(base_url)
	if content ~= nil then	--if download something
		for u, name in string.gmatch(content, 'href="([^"]-%.pbn[^"]-)".->(.-)</a>') do
			u = string.gsub(u, '&amp;', '&')	-- &amp; -> &
			--insert deal url to results table, at first position we want to have the newest results			
			table.insert(results, 1, {["url"]=base_url..u, ["name"]=name})
			success = true
		end
	end
	if success then
		break
	else
		if try ~= tries then sleep(0.2) end
	end
end

