forked from malimishdan/reserve-america-scraper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraper.js
153 lines (140 loc) · 6.04 KB
/
scraper.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
var http = require('http');
(function () {
"use strict";
var jsdom = require('jsdom');
var window = jsdom.jsdom().createWindow();
var $ = require('jquery/dist/jquery')(window), //require('jquery'),
request = require('request'),
querystring = require('querystring'),
growler = require('growler'),
fs = require('fs'),
argv = require('optimist').boolean('notify_growl').boolean('notify_boxcar').boolean('notify_pushover')
.boolean('waterfront').default('interval', 0).default('false_positives_max', 100).argv,
boxcar = require('boxcar'),
push = require('pushover-notifications'),
nconf = require('nconf'),
path = require('path'),
shorturl = require('shorturl');
var config_file = path.join(__dirname, argv.config_path, 'config.json');
nconf.argv().env().file({ file: config_file });
var campground_enum = nconf.get('campground_enum'),
campsites_ignore = nconf.get('campsites_ignore') || [],
campgrounds = argv.campgrounds.split(','),
electric_enum = { "15": 3002, "20": 3003, "30":3004, "50":3005 },
interval = argv.interval * 1000 * 60;
var scraper = function() {
campgrounds.forEach(function(campground) {
if (!campground) {
throw new Error('Campground not found!');
return;
}
if (!campground_enum[campground]) {
throw new Error(campground + ' not found!');
return;
}
var campground_fullname = campground.replace('_Sp','').replace('_', ' '),
state = argv.state,
parkId = campground_enum[campground],
length = argv.length || null,
electric = electric_enum[argv.electric] || null,
waterfront = (argv.waterfront) ? '3011' : '',
arrival = argv.arrival,
departure = argv.departure,
range = (departure) ? '2' : '',
nights = argv.nights,
false_positives_max = argv.false_positives_max,
notify_growl = argv.notify_growl,
notify_boxcar = argv.notify_boxcar,
notify_pushover = argv.notify_pushover;
var query = querystring.stringify({ parkId:parkId, siteType:'2001', expfits:true, eqplen:length, hookup:electric,
waterfront:waterfront, range:range,
calarvdate:arrival, lengthOfStay:nights, page:'calendar',
contractCode:state, siteTypeFilter:'RV or Tent' }),
baseurl = 'http://www.reserveamerica.com',
action = '/camping/' + campground + '/r/campsiteCalendar.do?',
url = baseurl + action + query;
request(url + '&submitSiteForm=true', function(error, response, body) {
request(url, function(error, response, body) {
// console.log(body);
console.log(url);
// console.log(campground_fullname);
var found = 0,
text = "Found: ";
$(body).find('table#calendar tbody tr:not(:has(td.sn a.unavail))').each(function(index) {
var possible_consecutive_nights_found = $(this).find('td.status.a+td.status.a').length + 1;
if (possible_consecutive_nights_found >= nights) {
var that = $(this).find('td.sn a:not(.sitemarker)'),
campsite = parseInt(that.text(),10),
campsite_link = that.attr('href');
if ($.inArray(campsite,campsites_ignore[campground]) === -1) {
text += '#' + campsite + ' ';
console.log(campsite + '*');
found++;
if (found > false_positives_max) console.log(that);
} else {
console.log(campsite);
}
}
});
if (found > 0) {
if (found < false_positives_max) {
shorturl(url, function(shorturl_result) {
text += shorturl_result;
if (notify_growl) {
var growl_app = new growler.GrowlApplication(nconf.get('growl:growl_app')),
icon_file = path.join(__dirname, nconf.get('growl:icon')),
icon = (fs.existsSync(icon_file)) ? fs.readFileSync(icon_file) : '';
growl_app.setNotifications({
'Status': {}
});
growl_app.register();
growl_app.sendNotification('Status', {
title: campground_fullname,
text: text,
icon: icon
});
}
if (notify_boxcar) {
var user = new boxcar.User(nconf.get('boxcar:username'), nconf.get('boxcar:password'));
user.notify(text, campground_fullname, null, null, nconf.get('boxcar:iconUrl'));
}
if (notify_pushover) {
var p = new push( {
user: nconf.get('pushover:user'),
token: nconf.get('pushover:token')
});
var msg = {
message: text,
title: campground_fullname
};
p.send(msg, function(err,result) {
if (err) {
throw err;
}
console.log(result);
});
}
});
}
else {
console.log('Possibly too many false positives!');
console.log(url);
console.log(body);
}
}
else {
var date = (departure) ? ' from ' + arrival + ' to ' + departure : ' on ' + arrival;
console.log('Sorry, no preferred campsites found at ' + campground_fullname + date + ' for ' + nights + ' night' + ((nights>1) ? 's' : '') + '.');
}
});
});
});
};
scraper();
if (interval > 0) setInterval(scraper, interval);
}) ();
var port = Number(process.env.PORT || 5000);
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello User \n');
}).listen(port);