Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Created August 27, 2015 15:51
Show Gist options
  • Select an option

  • Save jmatsu/5324ad5ff963bccf85ce to your computer and use it in GitHub Desktop.

Select an option

Save jmatsu/5324ad5ff963bccf85ce to your computer and use it in GitHub Desktop.
One of my lab's Hubot script
# Description:
# Retrieve the plans which are on weekdays from specified Google Calendar.
#
# Commands:
# hubot #{Words to run this}
#
# Notes:
# Displayed plan is found first.
# PLEASE FIX ME!!!
module.exports = (robot) ->
robot.respond /#{Words to run this}/i, (msg) ->
request = require('request')
xml2js = require('xml2js')
now = new Date
# hard coding...
request.get {
uri:'[Google Calendar URL (XML) is here]'
json: false
headers: {
"accept-language": "ja,en;q=0.8"
}
}, (err, r, body) ->
new xml2js.Parser().parseString(body, (err, result) ->
plans = []
trim = (str) ->
str = str.replace(" ", "").replace("JST", "").replace("\n", "")
str = str.replace("月", "Mon").replace("火", "Tue").replace("水", "Wed")
return str.replace("木", "Thu").replace("金", "Fri")
# the order of entries is created-date.
result["feed"]["entry"].forEach((s) ->
title = s.title[0]["_"]
m = /(\d+\/\d+\/\d+[^<]*)<br/.exec(s.summary[0]["_"])
if m?
date_str = trim(m[1])
m = /(\d+\/\d+\/\d+)/.exec(date_str)
if m?
date = new Date(m[1])
plans.push(date_str) if /{Regexp is here}/i.test(title) && date > now # get a plan by regexp and push it
)
# order by date asc
comp = (a, b) ->
m = /(\d+\/\d+\/\d+)/.exec(a)
lhs = new Date(m[0])
m = /(\d+\/\d+\/\d+)/.exec(b)
rhs = new Date(m[0])
return lhs - rhs
plans.sort(comp)
msg.send ""+
"Next will be held : "+plans[0]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment