forked from rweichler/rjailbreak_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
72 lines (60 loc) · 1.78 KB
/
main.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
65
66
67
68
69
70
71
72
local json = require 'dkjson'
local output_filename = '/home/freebsd/www/rjailbreak.html'
local existing_contents
function main()
existing_contents = readAll(output_filename)
local url = "'https://www.reddit.com/r/jailbreak/new.json'"
local jsonstr = os.capture('curl '..url..' -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36"')
local listing, pos, err = json.decode(jsonstr, 1)
if err then
print(err)
return
end
local posts = listing.data.children
for i,v in ipairs(posts) do
process_post(v.data.title, 'http://reddit.com'..v.data.permalink, v.data.created)
end
local f = io.open(output_filename, 'w')
f:write(existing_contents)
f:close()
end
local good_words = {
'%[release%]',
'%[upcoming%]',
'%[news%]',
'%[update%]',
'%[beta%]',
}
function process_post(title, url, time)
local lower = string.lower(title)
local found = false
for i,v in ipairs(good_words) do
if string.find(lower, v) then
found = true
break
end
end
if not found then
print('BAD '..title)
elseif string.find(existing_contents, url) then
print('IGNORING '..title)
else
print('APPENDING '..title)
local date = os.date('%B %d, %Y (%I:%M %p)', time)
existing_contents = date..' <a href="'..url..'">'..title..'</a><br/>\n'..existing_contents
end
end
function os.capture(cmd)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
return s
end
function readAll(file)
local f = io.open(file, "rb")
if not f then return '' end
local content = f:read("*all")
f:close()
return content
end
main()