-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9gag.coffee
executable file
·82 lines (66 loc) · 2.13 KB
/
9gag.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Description:
# None
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect": "0.2.0"
#
# Configuration:
# HUBOT_9GAG_NO_GIFS (optional, skips GIFs if defined; default is undefined)
#
# Commands:
# hubot 9gag me - Returns a random meme image
#
# Author:
# EnriqueVidal
#
# Contributors:
# dedeibel (gif support)
Select = require( "soupselect" ).select
HTMLParser = require "htmlparser"
module.exports = (robot)->
robot.respond /9gag( me)?/i, (message)->
send_meme message, false, (title, src)->
message.send title, src
send_meme = (message, location, response_handler)->
meme_domain = "http://9gag.com"
location ||= "/random"
if location.substr(0, 4) != "http"
url = meme_domain + location
else
url = location
message.http( url ).get() (error, response, body)->
return response_handler "Sorry, something went wrong" if error
if response.statusCode == 302
location = response.headers['location']
return send_meme( message, location, response_handler )
selectors = ["a img.badge-item-img"]
if ! process.env.HUBOT_9GAG_NO_GIFS?
selectors.unshift("div.badge-animated-container-animated img")
img_src = get_meme_image( body, selectors )
if img_src.substr(0, 4) != "http"
img_src = "http:#{img_src}"
img_title = escape_html_characters( get_meme_title( body, [".badge-item-title"] ) )
response_handler img_title, img_src
select_element = (body, selectors)->
html_handler = new HTMLParser.DefaultHandler((()->), ignoreWhitespace: true )
html_parser = new HTMLParser.Parser html_handler
html_parser.parseComplete body
for selector in selectors
img_container = Select( html_handler.dom, selector )
if img_container && img_container[0]
return img_container[0]
get_meme_image = ( body, selectors )->
select_element(body, selectors).attribs.src
get_meme_title = ( body, selectors )->
select_element(body, selectors).children[0].raw
escape_html_characters = (text)->
replacements = [
[/&/g, '&']
[/</g, '<']
[/"/g, '"']
[/'/g, ''']
]
for r in replacements
text = text.replace r[0], r[1]
return text