Some changes on giphy plugin

This commit is contained in:
yago 2015-02-23 00:11:31 +01:00
parent f39e51c0f8
commit 3b59be34c5

View File

@ -1,27 +1,31 @@
-- Idea by https://github.com/asdofindia/telegram-bot/ -- Idea by https://github.com/asdofindia/telegram-bot/
-- See http://api.giphy.com/ -- See http://api.giphy.com/
do
local BASE_URL = 'http://api.giphy.com/v1'
local API_KEY = 'dc6zaTOxFJmzC' -- public beta key
function get_random_top() function get_random_top()
local api_key = "dc6zaTOxFJmzC" -- public beta key local res, code = http.request(BASE_URL.."/gifs/trending?api_key="..API_KEY)
local b = http.request("http://api.giphy.com/v1/gifs/trending?api_key="..api_key) if code ~= 200 then return nil end
local images = json:decode(b).data local images = json:decode(res).data
local i = math.random(0,#images) local i = math.random(0,#images)
return images[i].images.downsized.url return images[i].images.downsized.url
end end
function search(text) function search(text)
local api_key = "dc6zaTOxFJmzC" -- public beta key local res, code = http.request(BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY)
b = http.request("http://api.giphy.com/v1/gifs/search?q="..text.."&api_key="..api_key) local images = json:decode(res).data
local images = json:decode(b).data
if #images == 0 then return nil end -- No images if #images == 0 then return nil end -- No images
math.randomseed(os.time())
local i = math.random(0,#images) local i = math.random(0,#images)
return images[i].images.downsized.url return images[i].images.downsized.url
end end
function run(msg, matches) function run(msg, matches)
-- If no search data, a cat gif will be sended local gif_url = ''
-- Because everyone loves pussies
-- If no search data, a random trending GIF will be sended
if matches[1] == "!gif" or matches[1] == "!giphy" then if matches[1] == "!gif" or matches[1] == "!giphy" then
gif_url = get_random_top() gif_url = get_random_top()
else else
@ -50,4 +54,6 @@ return {
"^!giphy$" "^!giphy$"
}, },
run = run run = run
} }
end