-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.coffee
More file actions
114 lines (89 loc) · 3.33 KB
/
data.coffee
File metadata and controls
114 lines (89 loc) · 3.33 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
parser = require 'xml2json'
rest = require 'restler'
date = require 'date-utils'
moment = require 'moment'
ical = require 'ical'
parseFeed = (feed) ->
JSON.parse(parser.toJson(feed)).feed.entry
messageFeed = 'http://groups.google.com/group/kc-nodejs/feed/atom_v1_0_topics.xml'
eventFeed = 'http://www.google.com/calendar/ical/nodekc.org_e8lg6hesldeld1utui23ebpg7k%40group.calendar.google.com/public/basic.ics'
twitterFeed = 'http://search.twitter.com/search.json?q=%40nodekc'
lastMessageFetchResult = {}
lastEventFetchResult = {}
lastTwitterFetchResult = {}
determineDate = (start, end) ->
start = moment(start)
end = moment(end)
date = start.format('ddd, MMM D')
return date if start.diff(end, 'days') == -1
date + start.add('hours', -6).format(' h:mma CST') + " for " + start.from(end, true)
createEventUrl = (id) ->
id = id.split('@')[0]
id = new Buffer(id + ' e8lg6hesldeld1utui23ebpg7k@google.com').toString('base64').replace('==', '')
"https://www.google.com/calendar/b/0/render?eid=#{id}&ctz=America/Chicago&pli=1&sf=true&output=xml"
striphtml = (value) ->
value.replace(/<(?:.|\n)*?>/gm, ' ')
timeAgo = (date) ->
moment(new Date(date)).fromNow()
formatContent = (content) ->
content = striphtml(content).trim()
content += '\u2026' if /[\w]$/i.test content
content
fetchMessages = (cb) ->
if lastMessageFetchResult.on? and lastMessageFetchResult.on > (new Date).addMinutes(-1)
cb lastMessageFetchResult.value
return
rest.get(messageFeed).on('complete', (data) ->
data or= ''
messages = for x in parseFeed data
{ subject: x.title.$t, body: formatContent(x.summary.$t), author: x.author.name, timeago: timeAgo(x.updated), url: x.link.href }
lastMessageFetchResult.value = messages
lastMessageFetchResult.on = new Date
cb messages
)
fetchTweets = (cb) ->
if lastTwitterFetchResult.on? and lastTwitterFetchResult.on > (new Date).addMinutes(-1)
cb lastTwitterFetchResult.value
return
rest.get(twitterFeed).on('complete', (data) ->
data or= {}
data.results or= []
tweets = for x in data.results
{ timeago: timeAgo(x.created_at), created_at: x.created_at, created_by: x.from_user, tweet: x.text }
lastTwitterFetchResult.value = tweets
lastTwitterFetchResult.on = new Date
cb tweets
)
fetchEvents = (cb) ->
if lastEventFetchResult.on? and lastEventFetchResult.on > (new Date).addMinutes(-1)
cb lastEventFetchResult.value
return
ical.fromURL eventFeed, {}, (err, calendar) ->
calendar or= {}
events = for k,v of calendar
{title: v.summary, location: v.location, details: v.description, when: determineDate(v.start, v.end), url: createEventUrl v.uid }
lastEventFetchResult.value = events
lastEventFetchResult.on = new Date
cb events
module.exports = {
load: (keys...) ->
return (req, res, next) =>
finished = []
res.data or= {}
keys.forEach (key) =>
this[key] res.data, (k) ->
finished.push k
next() if finished.length == keys.length
messages: (data, cb) ->
fetchMessages (messages) ->
data.messages = messages
cb 'messages'
tweets: (data, cb) ->
fetchTweets (tweets) ->
data.tweets = tweets
cb 'tweets'
events: (data, cb) ->
fetchEvents (events) ->
data.events = events
cb 'events'
}