-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
158 lines (132 loc) · 6.14 KB
/
server.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
157
158
const express = require('express');
const app = express();
const http = require('http');
const https = require('https');
const path = require('path');
const assert = require('assert');
const bodyParser = require('body-parser');
const fs = require('fs');
const readline = require('readline');
const rp = require('request-promise');
const cheerio = require('cheerio');
const admin = require('firebase-admin');
const dotenv = require('dotenv');
const { error } = dotenv.config();
if (error) {
throw error;
}
admin.initializeApp({
credential: admin.credential.cert({
"type": process.env.FIREBASETYPE,
"project_id": process.env.FIREBASEPROJECTID,
"private_key_id": process.env.FIREBASEPRIVATEKEYID,
"private_key": process.env.FIREBASEPRIVATEKEY.replace(/\\n/g, '\n'),
"client_email": process.env.FIREBASECLIENTEMAIL,
"client_id": process.env.FIREBASECLIENTID,
"auth_uri": process.env.FIREBASEAUTHURI,
"token_uri": process.env.FIREBASETOKENURI,
"auth_provider_x509_cert_url": process.env.FIREBASE509CERTPROVIDER,
"client_x509_cert_url": process.env.FIREBASE509CERTURL
}),
databaseURL: process.env.FIREBASEDATABASEURL
});
// Get a database reference to our blog
let db = admin.database();
let eventsRef = db.ref('events');
const xmlFeedUrl = 'https://www.himalayaninstitute.org/calendar/feed?action=tribe_photo&tribe_paged=1&tribe_event_display=photo&tribe_event_type%5B%5D=PureRejuv+Program&tribe_venues%5B%5D=32593&tribe_event_type%5B%5D=PureRejuv+Program&tribe_venues%5B%5D=32593';
let Parser = require('rss-parser');
let parser = new Parser();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', express.static(path.join(__dirname + '/')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/parsefeed', (req, res) => {
// https://www.npmjs.com/package/rss-parser
/*
{
"creator": "Rafe Colton",
"title": "Finding Center: An Ayurvedic Retreat to Nourish and Balance",
"link": "https:\/\/www.himalayaninstitute.org\/event\/finding-center-ayurvedic-retreat-nourish-balance-4\/",
"pubDate": "Fri, 14 Sep 2018 23:00:00 +0000",
"content:encoded": "some content about the thing",
"dc:creator": "Rafe Colton",
"comments": "https:\/\/www.himalayaninstitute.org\/event\/finding-center-ayurvedic-retreat-nourish-balance-4\/#respond",
"content": "<p> […]<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.himalayaninstitute.org\/event\/finding-center-ayurvedic-retreat-nourish-balance-4\/\">Finding Center: An Ayurvedic Retreat to Nourish and Balance<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.himalayaninstitute.org\">Himalayan Institute<\/a>.<\/p>\n",
"contentSnippet": "[\u2026]\nThe post Finding Center: An Ayurvedic Retreat to Nourish and Balance appeared first on Himalayan Institute.",
"guid": "https:\/\/www.himalayaninstitute.org\/?post_type=tribe_events&p=47940",
"isoDate": "2018-09-14T23:00:00.000Z"
},
*/
(async () => {
try {
let feed = await parser.parseURL(xmlFeedUrl);
let allData = [];
let index = 0;
feed.items.forEach((item) => {
//console.log(item.title + ':' + item.link)
const options = {
uri: item.link,
transform: (body) => {
return cheerio.load(body);
}
};
rp(options)
.then(($) => {
let data = {
startDate : $('.tribe-event-date-start').text(),
endDate : $('.tribe-event-date-end').text(),
cost : $('.tribe-events-cost').text(),
title : item.title || $('#tribe-events-content h1').text(),
subtitle : $('.tribe-events-page-subtitle').text(),
contentSnippet : item.contentSnippet,
contentEncoded : item['content:encoded'],
featuredImage : $('.tribe-events-event-image img').attr('src'),
soldout : $('.event-sold-out').length > 0,
presenters : []
};
$('.presenters .presenter').each(() => {
let presenter = {
image: $(this).find('img').attr('src') || '',
name: $(this).find('.organizer-desc h5 a').text(),
description: $(this).find('.organizer-desc p').text(),
};
data.presenters.push(presenter);
});
eventsRef
.orderByChild('title')
.equalTo(data.title).on('value', (snapshot) => {
if (snapshot.exists()) {
data.existed = true;
} else {
eventsRef.push(data);
data.existed = false;
}
});
allData.push(data);
})
.then(() => {
++index;
if(index === feed.items.length - 1) {
return res.status(200).json(allData);
}
})
.catch((err) => {
console.log(err);
});
});
//return res.status(200).json(allData);
}
catch(error) {
return res.status(500).json({
success: false,
message: 'Unable to parse RSS feed',
error: error
});
}
})();
});
let server = app.listen(3000, () => {
console.log('started on port 3000');
});