This repository was archived by the owner on Apr 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathread-rss.js
More file actions
98 lines (89 loc) · 3.16 KB
/
Copy pathread-rss.js
File metadata and controls
98 lines (89 loc) · 3.16 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
var FeedParser = require('feedparser');
var request = require('request'); // for fetching the feed
var Parse = require('parse/node');
var req = request('http://25livepub.collegenet.com/calendars/alleventsfrontpage.xml')
var feedparser = new FeedParser();
var events = [];
var EventObject = Parse.Object.extend("Event");
Parse.initialize("f8dd0d83bdc78b82378bb69e725d2f28");
Parse.serverURL = "http://localhost:8999/parse/";
/* converts Thursday, March 16, 2017, 11am – 12pm"
to "Thursday, March 16, 2017, 11:00 pm"
*/
function convertDate(date){
var split = date.split(";");
var endTime = split[split.length-1];
var startDate = date.split("&")[0];
split = startDate.split(",");
var startTime = split[split.length-1];
var day = split[0] + ","+ split[1] + "," + split[2]; //Thursday, March 16, 2015
startDate = day + " " + convertTime(startTime);
var endDate = day + " " + convertTime(endTime);
return [new Date(startDate),new Date(endDate)];
}
/* helper for convertDate */
function convertTime(time){
time = time.replace(" ",""); //removes leading space
if (time.length <= 4) {
var ar = time.split('');
ar.splice(ar.length - 2,0,":00");
time = ar.join('');
}
var ar = time.split('');
ar.splice(ar.length - 2,0," ");
return ar.join('');
}
/*searchs list of <br> items for <p> tag
returns null if not found
*/
function findDescription(description){
var pattern = /\<\/*p\>/ig
var d = description.filter((element) => pattern.test(element));
return d.length == 0 ? null : d[0].replace(pattern,"");
}
req.on('response', function (res) {
var stream = this; // `this` is `req`, which is a stream
if (res.statusCode !== 200) {
this.emit('error', new Error('Bad status code'));
}
else {
stream.pipe(feedparser);
}
});
feedparser.on('readable', function () {
// This is where the action is!
var stream = this; // `this` is `feedparser`, which is a stream
var meta = this.meta; // **NOTE** the "meta" is always available in the conteeventt of the feedparser instance
var item;
var description;
var evnt;
while (item = stream.read()) {
evnt = new EventObject();
description = item.description.split("<br/>"); //description is an html element from the RSS feed
times = convertDate(description[1]);
evnt.set("eventid", item.guid);
evnt.set("title", item.title);
evnt.set("description",findDescription(description));
evnt.set("location",description[0]); //if the RSS feed format changes, these will need to be changed
evnt.set("startTime",times[0]);
evnt.set("endTime",times[1]);
events.push(evnt);
}
});
feedparser.on('finish', function () {
//console.log("events updated " + updateCount);
//console.log("total events " + totalCount);
//console.log("new events " + (totalCount-updateCount));
//var query = new Parse.Query(EventObject).contains("eventid",item.guid);
//var log = query.find({success:function(results) {
//var d = test.description.split("<br/>");
//console.log(d);
//events[0].save();
//var query = new Parse.Query(Event);
//query.contains("eventid",
console.log("saving " + events.length + " events");
for (var e of events)
{
e.save();
}
});