Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 49 lines (37 sloc) 1.345 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
# Description:
# None
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect: "0.2.0"
#
# Configuration:
# None
#
# Commands:
# hubot reddit (me) <reddit> [limit] - Lookup reddit topic
#
# Author:
# EnriqueVidal

Select = require( "soupselect" ).select
HTMLParser = require "htmlparser"

lookup_site = "http://www.reddit.com/"

module.exports = (robot)->
  robot.respond /reddit( me)? ([a-z0-9\-_\.]+\/?[a-z0-9\-_\.]+)( [0-9]+)?/i, (message)->
    lookup_reddit message, (text)->
      message.send text

  lookup_reddit = (message, response_handler)->
    top = parseInt message.match[3]
    reddit = "r/" + message.match[2] + ".json"

    location = lookup_site + reddit

    message.http( location ).get() (error, response, body)->
      return response_handler "Sorry, something went wrong" if error
      return response_handler "Reddit doesn't know what you're talking about" if response.statusCode == 404
      return response_handler "Reddit doesn't want anyone to go there any more." if response.statusCode == 403

      list = JSON.parse( body ).data.children
      count = 0

      for item in list
        count++

        text = ( item.data.title || item.data.link_title ) + " - " + ( item.data.url || item.data.body )
        response_handler text

        break if count == top

Something went wrong with that request. Please try again.