Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support to display titles, since they often help to get the han…

…g of the joke.
  • Loading branch information...
commit 7c91c3aa5bab9194e4c1b83cc33335a4bed61c9c 1 parent 77f3e0f
@dedeibel authored
Showing with 26 additions and 6 deletions.
  1. +26 −6 src/scripts/9gag.coffee
View
32 src/scripts/9gag.coffee
@@ -22,8 +22,8 @@ HTMLParser = require "htmlparser"
module.exports = (robot)->
robot.respond /9gag( me)?/i, (message)->
- send_meme message, false, (text)->
- message.send text
+ send_meme message, false, (title, src)->
+ message.send title, src
send_meme = (message, location, response_handler)->
meme_domain = "http://9gag.com"
@@ -45,13 +45,14 @@ send_meme = (message, location, response_handler)->
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}"
- response_handler img_src
+ img_title = escape_html_characters( get_meme_title( body, [".badge-item-title"] ) )
+
+ response_handler img_title, img_src
-get_meme_image = (body, selectors)->
+select_element = (body, selectors)->
html_handler = new HTMLParser.DefaultHandler((()->), ignoreWhitespace: true )
html_parser = new HTMLParser.Parser html_handler
@@ -59,4 +60,23 @@ get_meme_image = (body, selectors)->
for selector in selectors
img_container = Select( html_handler.dom, selector )
if img_container && img_container[0]
- return img_container[0].attribs.src
+ 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, '&lt;']
+ [/"/g, '&quot;']
+ [/'/g, '&#039;']
+ ]
+
+ for r in replacements
+ text = text.replace r[0], r[1]
+ return text
+

0 comments on commit 7c91c3a

Please sign in to comment.
Something went wrong with that request. Please try again.