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