Permalink
Browse files

Merge pull request #1304 from dedeibel/master

Added gif support for 9gag.
  • Loading branch information...
2 parents 3a2a070 + 77f3e0f commit 068ec13183e31df75b2d544107719d46fc0262b5 @technicalpickles technicalpickles committed Feb 10, 2014
Showing with 14 additions and 4 deletions.
  1. +14 −4 src/scripts/9gag.coffee
@@ -6,13 +6,16 @@
# "soupselect": "0.2.0"
#
# Configuration:
-# None
+# 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"
@@ -37,16 +40,23 @@ send_meme = (message, location, response_handler)->
location = response.headers['location']
return send_meme( message, location, response_handler )
- img_src = get_meme_image( body, ".badge-item-img" )
+ 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}"
response_handler img_src
-get_meme_image = (body, selector)->
+get_meme_image = (body, selectors)->
html_handler = new HTMLParser.DefaultHandler((()->), ignoreWhitespace: true )
html_parser = new HTMLParser.Parser html_handler
html_parser.parseComplete body
- Select( html_handler.dom, selector )[0].attribs.src
+ for selector in selectors
+ img_container = Select( html_handler.dom, selector )
+ if img_container && img_container[0]
+ return img_container[0].attribs.src

0 comments on commit 068ec13

Please sign in to comment.