Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit e5b1f1b

Browse files
Merge pull request #1468 from altschuler/patch-1
Update replygif script to use the new API
2 parents 1b0caee + 74867b7 commit e5b1f1b

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

src/scripts/replygif.coffee

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,47 @@
11
# Description:
2-
# Makes ReplyGif easier to use. See http://replygif.net.
2+
# Show ReplyGifs based on tags. See http://replygif.net.
33
#
44
# Dependencies:
5-
# "cheerio": ">= 0.9.2"
5+
# None
66
#
77
# Configuration:
8-
# None
8+
# HUBOT_REPLYGIF_API_KEY: the api key for replygif.net, defaults to public key "39YAprx5Yi"
99
#
1010
# Commands:
11-
# http://replygif.net/<id> - Embeds image from ReplyGif with that id.
12-
# hubot replygif <keyword> - Embeds random ReplyGif with the keyword.
13-
# hubot replygif me <keyword> - Same as `hubot replygif <keyword>`.
11+
# hubot replygif <tag> - Embed a random ReplyGif with the given tag.
12+
# hubot replygif me <tag> - Same as `hubot replygif <tag>`.
13+
# hubot replygif id <id> - Embed the ReplyGif with the given id
14+
# hubot replygif me id <id> - Same as `hubot replygif id <id>`.
1415
#
1516
# Notes:
16-
# None
17+
# Use 'rg' as shorthand for the 'replygif' command
1718
#
1819
# Author:
19-
# sumeetjain, meatballhat
20+
# altschuler (previous non-api version by sumeetjain, meatballhat)
2021

21-
cheerio = require 'cheerio'
22+
apiKey = process.env.HUBOT_REPLYGIF_API_KEY or "39YAprx5Yi"
2223

23-
module.exports = (robot) ->
24-
# Listen for someone to link to a ReplyGif and reply with the image.
25-
robot.hear /.*replygif\.net\/(i\/)?(\d+).*/i, (msg) ->
26-
id = msg.match[2]
27-
msg.send "http://replygif.net/i/#{id}#.gif"
28-
29-
# Listen for a command to look up a ReplyGif by ID.
30-
robot.respond /replygif( me)? (\d+)/i, (msg) ->
31-
id = msg.match[2]
32-
msg.send "http://replygif.net/i/#{id}#.gif"
24+
apiUrl = "http://replygif.net/api/gifs?api-key=#{apiKey}"
3325

34-
# Listen for a command to look up a ReplyGif by tag.
35-
robot.respond /replygif( me)? (\D+)/i, (msg) ->
36-
replyGifByTag(msg, msg.match[2])
26+
module.exports = (robot) ->
27+
apiCall = (msg, failMsg, query) ->
28+
robot.http(apiUrl + query).get() (err, res, body) ->
29+
try
30+
gifs = JSON.parse body
31+
if not gifs? or not gifs.length
32+
msg.send failMsg
33+
else
34+
msg.send (msg.random gifs).file
3735

38-
replyGifByTag = (msg, tag) ->
39-
msg
40-
.http("http://replygif.net/t/#{tagify(tag)}")
41-
.header('User-Agent: ReplyGIF for Hubot (+https://github.com/github/hubot-scripts)')
42-
.get() (err, res, body) ->
43-
if not err and res.statusCode is 200
44-
msg.send msg.random getGifs(body)
45-
else
46-
msg.send 'No GIF for you, human.'
36+
robot.hear /.*replygif\.net\/(i\/)?(\d+)(?!.*\.gif).*/i, (msg) ->
37+
id = msg.match[2]
38+
msg.send "http://replygif.net/i/#{id}.gif"
4739

48-
getGifs = (body) ->
49-
$ = cheerio.load(body)
50-
$('img.gif[src]').map (i, elem) ->
51-
elem.attribs.src.replace(/thumbnail/, 'i')
40+
robot.respond /(replygif|rg)( me)? ([\w|\ ]+)/i, (msg) ->
41+
tag = msg.match[3]
42+
if tag is "id" then return # hubot's looking for an id
43+
apiCall msg, "I don't know that reaction", "&tag=#{tag}"
5244

53-
tagify = (s) ->
54-
s.toLowerCase().replace(/\s+/g, '-').replace(/[^-a-z]/g, '')
45+
robot.respond /(replygif|rg)( me)? id (\d+)/i, (msg) ->
46+
id = msg.match[3]
47+
apiCall msg, "I don't any gifs with that id", "&id=#{id}"

0 commit comments

Comments
 (0)