Skip to content
This repository
file 83 lines (66 sloc) 2.185 kb
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 82
# 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, '&lt;']
    [/"/g, '&quot;']
    [/'/g, '&#039;']
  ]

  for r in replacements
    text = text.replace r[0], r[1]
  return text

Something went wrong with that request. Please try again.