Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 89 lines (81 sloc) 2.347 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 83 84 85 86 87 88
# Description:
# Get a snow report from onthesnow.com
#
# Dependencies:
# "xml2js": "0.1.14"
#
# Configuration:
# None
#
# Commands:
# hubot snow in <two letter state name> - Displays resort info for a state, .e.g., snow in CO
# hubot snow at <resort>, <two letter state name> - Displays info for a single resort
#
# Author:
# rwc9u

xml2js = require('xml2js')

states =
  'ak': 'alaska'
  'az': 'arizona'
  'ca': 'california'
  'co': 'colorado'
  'ct': 'connecticut'
  'id': 'idaho'
  'il': 'illinois'
  'in': 'indiana'
  'ia': 'iowa'
  'me': 'maine'
  'md': 'maryland'
  'ma': 'massachusetts'
  'mi': 'michigan'
  'mn': 'minnesota'
  'mo': 'missouri'
  'mt': 'montana'
  'nv': 'nevada'
  'nh': 'new-hampshire'
  'nj': 'new-jersey'
  'nm': 'new-mexico'
  'ny': 'new-york'
  'nc': 'north-carolina'
  'oh': 'ohio'
  'or': 'oregon'
  'pa': 'pennsylvania'
  'sd': 'south-dakota'
  'tn': 'tennessee'
  'ut': 'utah'
  'vt': 'vermont'
  'va': 'virginia'
  'wa': 'washington'
  'wv': 'west-virginia'
  'wi': 'wisconsin'
  'wy': 'wyoming'

module.exports = (robot) ->
  robot.respond /snow in (\w+)$/i, (msg) ->
    snow_report(msg, msg.match[1], null)
  robot.respond /snow at (.+), (\w+)$/i, (msg) ->
    snow_report(msg, msg.match[2], msg.match[1])


snow_report = (msg, state, resort) ->
  state_full = states[state.toLowerCase()]
  if !state_full
    msg.send "Sorry bro, #{state} isn't a legit state or there are no resorts there!"
    return
  
  get_snow_report(msg,"http://www.onthesnow.com/#{state_full}/snow-rss.html",resort)

get_snow_report = (msg, url, resort) ->
  msg.http(url)
    .get() (err, res, body) ->
      if res.statusCode is 301
        get_snow_report msg, res.headers.location, resort
      else if res.statusCode is 404
         msg.send "Couldn't find that location."
      else
        parser = new xml2js.Parser()
        parser.parseString body, (err, result) ->
          if err
            msg.send "Got: #{err}"
          output = ""
          for area in result.channel.item
            if resort? and resort.toLowerCase() == area.title.toLowerCase()
              output = "#{area.title} - #{area.description}"
            else
              output += "#{area.title} - #{area.description}\n" unless resort?
          msg.send output
Something went wrong with that request. Please try again.