giphy sends original if downsized doesn't exists

This commit is contained in:
yago 2015-02-28 12:30:58 +01:00
parent 2e6c0f823d
commit 13a787e577

View File

@ -6,28 +6,40 @@ do
local BASE_URL = 'http://api.giphy.com/v1' local BASE_URL = 'http://api.giphy.com/v1'
local API_KEY = 'dc6zaTOxFJmzC' -- public beta key local API_KEY = 'dc6zaTOxFJmzC' -- public beta key
function get_random_top() function get_image(response)
local res, code = http.request(BASE_URL.."/gifs/trending?api_key="..API_KEY) local images = json:decode(response).data
if code ~= 200 then return nil end if #images == 0 then return nil end -- No images
local images = json:decode(res).data
local i = math.random(0,#images) local i = math.random(0,#images)
return images[i].images.downsized.url local image = images[i] -- A random one
if image.images.downsized then
return image.images.downsized.url
end
if image.images.original then
return image.original.url
end
return nil
end
function get_random_top()
local url = BASE_URL.."/gifs/trending?api_key="..API_KEY
local response, code = http.request(url)
if code ~= 200 then return nil end
return get_image(response)
end end
function search(text) function search(text)
text = URL.escape(text) text = URL.escape(text)
local url = BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY local url = BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY
print(url) local response, code = http.request(url)
local res, code = http.request(url)
if code ~= 200 then return nil end if code ~= 200 then return nil end
local images = json:decode(res).data return get_image(response)
if #images == 0 then return nil end -- No images
local i = math.random(0,#images)
return images[i].images.downsized.url
end end
function run(msg, matches) function run(msg, matches)
local gif_url = '' local gif_url = nil
-- If no search data, a random trending GIF will be sended -- 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
@ -60,4 +72,4 @@ return {
run = run run = run
} }
end end