#!/usr/bin/env lua5.1 LOTTERY_URL = 'http://lua-users.sourceforge.net/wiki/LuaUsersLottery' SEQUENCE_URL = 'http://www.random.org/sequences/?min=1&max=%d&format=plain&rnd=date.%s' SEQUENCE_FILE = 'sequence.txt' INFO_FILE = 'lottery_info.txt' N_WINNERS = 2 function command(...) return io.popen(string.format(...)):read() end print("== Lua User's Lottery ==") print(os.date('!%c')..' UTC') command("lynx -dump -nolist -width=160 '%s' > %s", LOTTERY_URL, INFO_FILE) info_text = io.open(INFO_FILE):read('*a') participants = string.gsub(info_text, '.-HERE >>>.-\n(.*\n).-<<<.*', '%1') participant_list = { } -- NOTE: assuming list entries are not multiple lines string.gsub(participants, '%s*%*%s*(.-)\n', function (entry) table.insert(participant_list, entry) end) table.sort(participant_list) print('participants:') n = #participant_list for i = 1, n do print(' '..i, participant_list[i]) end utc_date = os.date('!%Y-%m-%d') seq_url = string.format(SEQUENCE_URL, n, utc_date) command("wget -q -O %s '%s'", SEQUENCE_FILE, seq_url) print('random sequence URL:') print(' '..seq_url) print('and the '..(N_WINNERS > 1 and 'winners are' or 'winner is')..'...') count = 0 for line in io.open(SEQUENCE_FILE):lines() do local n = tonumber(line) print(' '..n, participant_list[n]) count = count + 1 if count == N_WINNERS then break end end