File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 5
5
"author" : " Anchit Jain <anchitjain1234@gmail.com>" ,
6
6
"description" : " A simple helpful robot for your Company" ,
7
7
"dependencies" : {
8
+ "cheerio" : " ^0.19.0" ,
8
9
"hubot" : " ^2.12.0" ,
9
10
"hubot-9gag" : " ^0.2.0" ,
10
11
"hubot-codinglove" : " 0.2.5" ,
Original file line number Diff line number Diff line change
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' )
You can’t perform that action at this time.
0 commit comments