Usando la super api de yago de 9gag

This commit is contained in:
yago 2014-08-06 01:15:54 +02:00
parent 0264ac2935
commit 6fa1952dd0

View File

@ -45,12 +45,13 @@ function do_action(msg)
send_msg(receiver, text) send_msg(receiver, text)
end end
if string.starts(msg.text, '9gag') then if string.starts(msg.text, '9gag') then
url = get_infiniGAG() url, title = get_9GAG()
file_name = url:match("([^/]+)$") file_name = url:match("([^/]+)$")
file = "/tmp/"..file_name file = "/tmp/"..file_name
sh = "curl -o '"..file.."' "..url sh = "curl -o '"..file.."' "..url
run_bash(sh) run_bash(sh)
send_photo(receiver, file) send_photo(receiver, file)
send_msg(receiver, title)
end end
if string.starts(msg.text, 'fortune') then if string.starts(msg.text, 'fortune') then
text = run_bash('fortune') text = run_bash('fortune')
@ -230,14 +231,18 @@ function get_fortunes_uc3m()
return b return b
end end
function get_infiniGAG() function get_9GAG()
b, c, h = http.request("http://infinigag-us.aws.af.cm") b = http.request("http://api-9gag.herokuapp.com/")
local gag = json:decode(b) local gag = json:decode(b)
math.randomseed(os.time()) math.randomseed(os.time())
i = math.random(#gag.data) -- random max json table size (# is an operator o.O) i = math.random(#gag) -- random max json table size (# is an operator o.O)
local link_image = gag.data[i].images.normal local link_image = gag[i].src
local title = gag[i].title
if link_image:sub(0,2) == '//' then
link_image = msg.text:sub(3,-1)
end
print("9gag image"..link_image) print("9gag image"..link_image)
return link_image return link_image, title
end end
function get_weather(location) function get_weather(location)