Skip to content

Commit 9d11b11

Browse files
Cyanide and Happiness comic strips can also be retrieved now.
1 parent 23c1bc6 commit 9d11b11

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Anchit Jain <anchitjain1234@gmail.com>",
66
"description": "A simple helpful robot for your Company",
77
"dependencies": {
8+
"cheerio": "^0.19.0",
89
"hubot": "^2.12.0",
910
"hubot-9gag": "^0.2.0",
1011
"hubot-codinglove": "0.2.5",

scripts/explosm.coffee

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Description:
2+
# Returns a random comic strip from Cyanide and Happiness website.
3+
#
4+
# Dependencies:
5+
# "cheerio": "0.7.0"
6+
#
7+
#
8+
#
9+
# Configuration:
10+
# None
11+
#
12+
# Commands:
13+
# hubot c&h - Returns random comic strip from Cyanide and Happiness.
14+
#
15+
# Author:
16+
# Anchit Jain
17+
18+
19+
cheerio = require('cheerio')
20+
util = require "util"
21+
22+
23+
module.exports = (robot)->
24+
robot.respond /c&h( me)?/i, (message)->
25+
send_meme message, 'http://explosm.net/comics/random', (text)->
26+
message.send text
27+
28+
send_meme = (message, location, response_handler)->
29+
url = location
30+
31+
message.http(url).get() (error, response, body)->
32+
return response_handler "Sorry, something went wrong" if error
33+
34+
if response.statusCode == 302 || response.statusCode == 301
35+
location = response.headers['location']
36+
location='http://explosm.net' + location
37+
return send_meme(message, location, response_handler)
38+
img_src = get_meme_image(body, "#comic-container img")
39+
40+
response_handler "http:#{img_src}"
41+
42+
get_meme_image = (body, selector)->
43+
$ = cheerio.load(body)
44+
$(selector).first().attr('src')

0 commit comments

Comments
 (0)