spoike
2/27/2015 - 5:56 AM

"Is it friday?" hubot script that displays media from /r/holdmybeer if the current day is a friday

"Is it friday?" hubot script that displays media from /r/holdmybeer if the current day is a friday

# Description:
#   Hubot delivers a pic from Reddit's /r/holdmybeer frontpage if it is a friday
#   based on the aww script.
#
# Dependencies:
#   moment
#
# Configuration:
#   None
#
# Commands:
#   hubot is it friday - Display a picture from /r/holdmybeer if today is a friday
#
# Author:
#   spoike

url = require("url")
moment = require("moment")

module.exports = (robot) ->
  robot.respond /is it friday/i, (msg) ->
    weekday = moment().format("dddd")
    return msg.send "No, today is #{weekday}" if !~ weekday.indexOf "Friday"
    msg.http('http://www.reddit.com/r/holdmybeer.json')
      .get() (err, res, body) ->
        result = JSON.parse(body)

        data = [ ]
        for child in result.data.children
          if child.data.domain != "self.holdmybeer"
            if child.data.url
              data.push(child.data)

        if data.count <= 0
          msg.send "YES, TODAY IS FRIDAY!"
          return

        rnd = Math.floor(Math.random()*data.length)
        picked_data = data[rnd]
        picked_url = picked_data.url

        parsed_url = url.parse(picked_url)

        picked_title = picked_data.title.replace("HMB", "Hold my beer").replace(/\sbeer\s/gi, " :beer: ")

        msg.send "YES, TODAY IS FRIDAY!\nNow... #{picked_title} #{picked_url}"