This repository was archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocialfeed.js
More file actions
209 lines (183 loc) · 8.04 KB
/
Copy pathsocialfeed.js
File metadata and controls
209 lines (183 loc) · 8.04 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var config = require('./config.js');
var SOCIALFEEDRSSINFO = config.SOCIALFEEDRSSINFO;
var SITES = config.SITES;
// Array of tweets/statuses to be published excluding location data.
var SINGLECITYPHRASES = [
"Book your next hotel room {deal} with us.",
"Planning a vacay or know someone who is? Stay {deal}. Make it happen.",
"Start booking your next vacation here {deal}.",
"Check our current hotel deals {deal}.",
"Find hotels {deal}. Come visit.",
"Relax here {deal}.",
"Looking for somewhere to stay during your visit? Book a hotel room {deal}.",
"Treat yourself. Stay in one of our hotels {deal}.",
"Visit and relax with hotel rooms {deal}.",
"Don't go broke just for a vacation. Book with us {deal}.",
"Take a vacation. Get a room {deal}.",
"[You] + [a one of our hotels] = [downtime]. Re-energize {deal}. You deserve it.",
"Clear your mind. Book a room from {deal}. Escape. Refresh. Relax.",
"Live in the city? Why not have a staycation for once? Book a room {deal}.",
"Take a breather from your everyday routine. We have hotel rooms {deal}.",
"Be a tourist. Book a room {deal} with us but don't forget the fanny pack!",
"Closed blinds. Room service. Serenity. Click for our hotels {deal}.",
"No alarms. No email. Sleep. We're offering you hotel rooms {deal}. Book yours!",
"Did you know that you could get room with us {deal}? Check us out!",
"Have a look at which hotel rooms you can book {deal}.",
"Party, sleep or maybe party in your sleep while staying at one of our hotels {deal}.",
"We could put something witty here about another one of our hotel room deals but here's a link to book one instead. You're welcome!",
"Feeling lucky? Here are surprize hotel room options for you.",
'Always been told "Get a room"? You can do that {deal} with us.'
];
// Used with sites like Cheap where it publishes deals for multiple locations/cities.
var MULTICITYPHRASES = [
"Going to {location}? Book a hotel room {deal}.",
"Planning a vacay for {location}? Stay {deal}. Make it happen.",
"Vacation in {location} {deal}.",
"Run away to {location} hotels {deal}.",
"Check our {location} hotel deals {deal}.",
"Find hotels {deal}. Come visit.",
"Explore {location} with ease. Book a room {deal}.",
"{location} has hotel rooms {deal}.",
"Relax in a {location} hotel {deal}.",
"Stay in {location} hotels {deal}.",
"Looking for somewhere to stay in {location}? Book a hotel room {deal}.",
"Treat yourself. Stay in a {location} hotel {deal}.",
"Visit {location} and relax with hotel rooms {deal}.",
"Don't go broke just for a vacation. Stay in {location} {deal}.",
"Take a vacation in {location}. Get a room {deal}.",
"[You] + [a {location} hotel] = [downtime]. Re-energize {deal}. You deserve it.",
"Clear your mind. Book a room in {location} {deal}. Escape. Refresh. Relax.",
"Live in {location}? Why not have a staycation for once? Book a room {deal}.",
"Take a breather from your everyday routine. {location} has hotel rooms {deal}.",
"Be a tourist in {location}. Book a room {deal} but don't forget the fanny pack.",
"Closed blinds. Room service. Serenity. {location} hotels {deal}.",
"No alarms. No email. Sleep. {location} hotel rooms {deal}. Book yours!",
"Thought about staying in a {location} hotel room? Do it {deal}.",
"Party, sleep or maybe party in your sleep while staying at a {location} hotel {deal}.",
"We could put something witty here about our {location} hotel room deals but here's the link to book one instead.",
"Feeling lucky? Here are surprize hotel room options for you."
];
// Phrases to be used as a fallback if EAN doesn't give us supply a timely feed.
var STANDARDPHRASE = [
"Check us out.",
"Browse our hotel selection for your next trip.",
"Did you know about our best price guarantee?",
"Tons of hotels to choose from.",
"Book your next hotel through us."
];
var FeedParser = require('feedparser');
var fs = require('fs'); // Module for writing to files. *--- To be removed ---*
var RSS = require('rss'); // Module used to create RSS output
// Fields to be replaced in final SINGLECITYPHRASES instances.
var PHRASE_FIELDS = {
location:"{location}",
deal:"{deal}",
currency:"USD ",
duration:"1 night(s)"
};
var feed = new RSS(config.SOCIALFEEDRSSINFO);
//console.log("Feed object: ", feed);
// Returns processed RSS.
function done_parsing(site, error, meta, articles) {
// In the event of an error in getting the EAN feed, output STANDARDPHRASE otherwise get to humanizing below!
if (error) {
//console.log("----> Error event handling. Now replacing strings with fallback phrases.", error);
var text = STANDARDPHRASE[randomIndex(STANDARDPHRASE)];
var options = {description:text, url:site.author, author:site.author};
feed.item({
title: text,
description: "",
url: site.author,
author: site.author
});
//console.log("feed.xml()", xml);
// *--- To be removed ---*
//fs.writeFile('myfeed.xml', xml);
} else {
for (var idx in articles) {
humanize_article(site, articles[idx])
}
}
var xml = feed.xml();
if (site.results_callback)
site.results_callback(xml);
else
site.results_callback('Unable to retrieve site RSS data.')
// Re-instantiates object after xml update. Eliminates persisting values and duplicate listings when reloading
// the page while the app runs.
feed = new RSS(config.SOCIALFEEDRSSINFO);
}
function randomIndex(phraseBank){
return Math.floor((Math.random()*phraseBank.length));
}
// Processes raw EAN RSS items and populates huamnized 'feed' RSS object.
function humanize_article(site, article){
//Random number used to index into SINGLECITYPHRASES
//var randomNum = Math.floor((Math.random()*SINGLECITYPHRASES.length));
// Select which phrase bank to use based on whether or not we will be outputting the location in the finished status.
// Eg. Miami only refers to Miami, therefore it's redundant to mention Miami in any status updates.
if (!site.multiCity){
var phrases = SINGLECITYPHRASES;
}
else
{
var phrases = MULTICITYPHRASES;
}
var randomNum = randomIndex(phrases);
var text = phrases[randomNum];
// Values to replace {deal} {location fields} in raw SINGLECITYPHRASES for an RSS item.
var phraseValues = {
location:article.title,
deal:article.description,
currency:"US$",
duration: "a night"
};
// Replaces {deal} {location} fields in SINGLECITYPHRASES. Polishes it to 'human' form.
for (var idx in PHRASE_FIELDS)
text = text.replace(PHRASE_FIELDS[idx], phraseValues[idx]);
// Appends to processed RSS.
feed.item({
title: text,
//title: article.title,
description: "",
url: article.link,
author: site.author
})
}
/*
function list_sites(){
var names = new Array();
for (var idx in config.SITES)
names = config.SITES[idx]
//return names[];
}
*/
// Extract (raw) EAN RSS items.
function parse_feed(site) {
// Pass in EAN's RSS feed to be parsed. Wait for {timeout period} before spewing an error.
options = {uri:site.eanRssFeed, timeout:5000};
var parseF = function(error, meta, articles) {
done_parsing(site, error, meta, articles);
}
var parser = new FeedParser();
parser.parseUrl(options, parseF);
parser.on('error', function(error) {
if(error.code === 'ETIMEDOUT'){
console.log("-----> Error expected and handled: ", error);
parseF(error, null, null)
}
else{
console.log("-----> Error that wasn't accounted for: ", error.code)
}
});
}
function process_site(site_name, results_callback) {
var site = config.SITES[site_name];
//console.log('Site object: ', site)
//if (!site) return;
site.results_callback = results_callback;
parse_feed(site);
}
exports.process = process_site;
//
//process_site('miami');