-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (138 loc) · 3.95 KB
/
app.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var sys = require('util'),
rest = require('restler'),
async = require('async'),
events = require("events");
var ical = require('./my_node_modules/node-ical/ical');
var cf = require("./my_node_modules/cloudfoundry");
// MongoDB
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
// Express
var express = require("express");
var app = express.createServer();
app.configure(function() {
app.set('running in cloud', cf.isRunningInCloud());
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
console.log('running in cloud ' + cf.isRunningInCloud());
if(!cf.isRunningInCloud()) {
// Only use this in public for samples or development
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}
});
var EventCal = new Schema({
list: { type: String },
timestamp: { type: Date },
ical: { type: String }
});
mongoose.model('EventCal', EventCal);
var mongoConfig = cf.getServiceConfig("eventcal");
console.log(mongoConfig);
var db = mongoose.createConnection("mongo://" + mongoConfig.username + ":" + mongoConfig.password + "@" + mongoConfig.hostname + ":" + mongoConfig.port + "/" + mongoConfig.db);
function getFromMongo(req, res) {
function getCalUrl(item, callback) {
var res = (item['screen_name'] ? item['screen_name'] : null);
callback(null, res);
}
function getCalData(item, callback) {
var url = 'http://lanyrd.com/profile/' + item + '/' + item + '.ics';
rest.get(url).on('complete', function(data) {
callback(null, data);
});
}
function parseCal(item, callback) {
events = ical.parseICS(item);
res = [];
for (uid in events) {
res.push(events[uid]['data']);
}
callback(null, res);
}
function allICalData(err, icals) {
cal = [];
for (e in icals) {
cal.push(icals[e]);
}
updateMongo(cal.join(''));
}
function allCalData(err, cals) {
async.concat(cals, parseCal, allICalData);
}
function getCals(err, cals) {
// cals = ['chanezon', 'mewzherder'];
async.map(cals, getCalData, allCalData);
}
function getList(tList) {
console.log('refreshing the list');
rest.get('https://api.twitter.com/1/lists/members.json?slug=' + tList.list + '&owner_screen_name=' + tList.owner).on('complete', function(data) {
async.map(data['users'], getCalUrl, getCals);
});
}
function updateMongo(s) {
var EventCal = db.model("EventCal", EventCal);
EventCal.findOne({list: tList.toString()}, function (err, data) {
if (err) {
throw(err);
}
var evCal;
if (data) {
evCal = data;
} else {
evCal = new EventCal();
evCal.list = tList.toString();
}
evCal.ical = s;
evCal.timestamp = new Date();
evCal.save(function (err) {
if (err) {
throw(err);
}
console.log('new eventcal saved at ' + evCal.timestamp);
});
});
}
function getCurrentUrl(req) {
//todo: enhance to handle https
return 'http://'+ req.headers.host + req.url;
}
function getTwitterListUrl(tList) {
return 'https://twitter.com/#!/' + tList.owner + '/' + tList.list;
}
var tList = { owner: req.params.owner,
list: req.params.list};
tList.toString = function() { return this.owner + '/' + this.list };
console.log(tList);
var EventCal = db.model("EventCal", EventCal);
console.log('in getFromMongo');
EventCal.findOne({list: tList.toString()}, function (err, data) {
if (err) {
console.log('error accessing the db ' + err);
throw(err);
}
var ical;
if (data) {
now = new Date();
if (now.getTime() > data.timestamp.getTime() + 5000) {
console.log('bust cache');
getList(tList);
}
console.log('found events');
ical = data.ical;
} else {
getList(tList);
//send back empty cal for now
ical = null;
console.log('sent empty cal');
}
res.contentType('ics');
res.render('ical', {
url: getCurrentUrl(req),
title: 'Aggregated Lanyrd Calendar for Twitter list ' + getTwitterListUrl(tList),
cal: ical
});
});
}
app.get('/cal/:owner/:list', getFromMongo);
app.listen(cf.getAppPort());