|
| 1 | +local FILE_NAME = 'dailyprogrammer.txt' |
| 2 | +local T_MODES = {"easy","intermediate","difficult","hard"} |
| 3 | +local t_links = {} |
| 4 | + |
| 5 | +function download() |
| 6 | + io.write('download ... ') |
| 7 | + io.flush() |
| 8 | + local http = require('socket.http') |
| 9 | + local ltn12 = require('ltn12') |
| 10 | + |
| 11 | + local pat_entry = '<a class="title " href="([^"]+)"' |
| 12 | + local pat_next = '<a href="([^"]+)"[^>]+>next' |
| 13 | + |
| 14 | + local next_link = 'http://www.reddit.com/r/dailyprogrammer' |
| 15 | + repeat |
| 16 | + local html, code, header, status = http.request(next_link) |
| 17 | + assert(code==200) |
| 18 | + |
| 19 | + for x in html:gmatch(pat_entry) do |
| 20 | + --if x:match("_challenge_") then |
| 21 | + table.insert(t_links, x) |
| 22 | + --end |
| 23 | + --print(x) |
| 24 | + end |
| 25 | + |
| 26 | + next_link = html:match(pat_next) |
| 27 | + until not next_link |
| 28 | + |
| 29 | + -- write to file |
| 30 | + local file = assert(io.open(FILE_NAME, 'w')) |
| 31 | + for _,x in pairs(t_links) do |
| 32 | + file:write(x..'\n') |
| 33 | + end |
| 34 | + file:close() |
| 35 | + print('done.') |
| 36 | +end |
| 37 | + |
| 38 | +------- |
| 39 | +-- main |
| 40 | +------- |
| 41 | +local update = (arg[1] == 'update') |
| 42 | + |
| 43 | +local file = io.open(FILE_NAME,'r') |
| 44 | +if not file or update then download() |
| 45 | +else |
| 46 | + print('using local copy.') |
| 47 | + for link in file:lines() do |
| 48 | + table.insert(t_links, link) |
| 49 | + end |
| 50 | + file:close() |
| 51 | +end |
| 52 | + |
| 53 | +local t_mode = {} |
| 54 | +for _,x in pairs(t_links) do |
| 55 | + local mode = x:match("(difficult)") or x:match("(intermediate)") or x:match("(easy)") or x:match("(hard)") |
| 56 | + |
| 57 | + if mode then |
| 58 | + if not t_mode[mode] then t_mode[mode] = {} end |
| 59 | + table.insert(t_mode[mode], x) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +for _,mode in pairs(T_MODES) do |
| 64 | + for _,link in pairs(t_mode[mode]) do |
| 65 | + print(mode..string.rep(' ',12-#mode),link) |
| 66 | + end |
| 67 | +end |
| 68 | + |
0 commit comments