-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
208 lines (159 loc) · 6.3 KB
/
app.js
File metadata and controls
208 lines (159 loc) · 6.3 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
var os = require('os');
var mubsub = require('mubsub');
var datejs = require('datejs');
var isdebug = process.argv[2] == 'debug';
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;
var path = require('path');
var childProcess = require('child_process');
var urlPrefix = 'http://www.skyscanner.de/dataservices/routedate/v2.0/';
var request = require('request');
var cpus = os.cpus();
console.log("IsDebug: " + isdebug);
console.log("Number of CPUs: " + cpus.length);
var importantCities = ['BKK','PMI','IST','NYC','BCN','MIA','AYT','LIS','LON','LAX','DPS','AGP','MAD','HKT','SKG','HAV','HER','LPA','ADB','ATH','SFO','VIE','IBZ','CPT','MUC','DXB','FRA','PAR','CMB','MNL','ROM','AMS','BER','FAO','LAS','KUL','DUB','JFK','SYD','SJO','SIN','FUE','ALC','MEX','HAM','CUN','SCL','SGN','TFS','CPH','HRG','SAW','BUD','TCI','BOG','USM','LIM','OPO','LHR','DEL','SPU','CTA','ZRH','DUS','HKG','VCE','AKL','BUE','NAP','JNB','STO','TLV','RAK','NCE','CDG','BOM','EDI','RIO','MOW','OLB','YVR','CFU','ACE','TYO','CAI','VLC','HNL','MLA','OSL','BEY','MEL','PRG','FCO','STR','FLL','SVO','RHO','WAW','HEL','MAN']
var count = 0;
var processcount = 0;
var executeSearch = function(data) {
data.forEach(function(item) {
dataItems.push(item);
if(dataItems.length > 20)
dataItems.shift();
});
}
var dataItems = [];
var processcount = 0;
setInterval(function() {
console.log(processcount + " processes active.");
var loadavg = os.loadavg()[0];
console.log("Loadaverage: " + loadavg);
if(processcount < 10)
{
StartNewWork();
StartNewWork();
StartNewWork();
}
}, 3000);
function StartNewWork() {
var data = dataItems.pop();
if(data==undefined)
{
setTimeout(StartNewWork, 1000);
return;
}
var ssUrl = 'http://www.skyscanner.de/transport/fluge/{0}/{1}/{2}/{3}/?usrplace=DE';
var departureDate = data.in_DepartureDate.toString("yyMMdd")
var returnDate = data.in_ReturnDate.toString("yyMMdd");
ssUrl = ssUrl.format(data.in_FromCity, data.in_ToCity, departureDate, returnDate);
if(departureDate.length != 6)
return undefined;
if(returnDate.length != 6)
return undefined;
/*
if(!importantCities.some(function(importantIata) {
return importantIata == data.in_ToCity;
}))
return;
*/
processcount++;
console.log("Processing " + ssUrl.split('http://www.skyscanner.de/transport/fluge/')[1]);
var process = childProcess.execFile(binPath, [path.join(__dirname, 'ssScraper.js'), ssUrl], function(err, stdout, stderr) {
//console.log(stdout);
try {
parseAndSendSSHTML(stdout);
}
catch (e)
{
console.log(e);
console.log(stdout);
}
StartNewWork();
processcount--;
});
}
var client = mubsub('mongodb://swalo:84kAanan@ds051658.mongolab.com:51658/swalo');
var searchChannel = client.channel('searches');
var searchdataChannel = client.channel('searchdata');
client.on('error', console.error);
searchChannel.on('error', console.error);
searchChannel.subscribe('NewSearch', function (message) {
if(!isdebug || count == 0)
{
executeSearch(message);
}
if(count == 0) {
StartNewWork();
StartNewWork();
StartNewWork();
StartNewWork();
}
count = count + 1;
});
var parsePriceHTML = function($element) {
var text = $element.text().trim();
if(text.indexOf('€') == -1)
return undefined;
var splits = text.split(' ');
if(splits.length > 3) //Aegan Airlines 54 €
return {
Price : splits[0] + ' ' + splits[1],
OTA: splits[2]
}
return {
Price : splits[1],
OTA: splits[0]
}
}
var parseAndSendSSHTML = function(html) {
var env = require('jsdom').env;
// first argument can be html string, filename, or url
var res = {};
env(html, function (errors, window) {
var $ = require('jquery')(window);
res.BestPrice = $('.header-info-bestprice .price').text();
console.log(res.BestPrice);
console.log($('.day-list-item').length);
res.Itineraries = $('.day-list-item').map(function(item) {
$this = $(this);
var res = {};
res.CarrierIds = $('.carrier-wrapper img, .carrier-wrapper span', $this).attr('data-carrier-ids');
res.OutboundLegDepartureDateTime = $("div.day-options-item.depart > div > div.day-options-navigation > span").attr('data-date') + ' ' + $('.leg.depart.flight .leg-flight-depart .leg-flight-time', $this).text();
res.InboundLegDepartureDateTime = $("div.day-options-item.return > div > div.day-options-navigation > span").attr('data-date') + ' ' + $('.leg.return.flight .leg-flight-depart .leg-flight-time', $this).text();
res.Origin = $('.leg.depart.flight .leg-flight-depart .leg-flight-station', $this).text();
res.Dest = $('.leg.return.flight .leg-flight-depart .leg-flight-station', $this).text();
res.prices = [];
res.prices = $('.details-group-altquotes-wrapper li a', $this).map(function() {
return parsePriceHTML($(this));
});
res.prices = res.prices.filter(function(item) {
return item != undefined;
});
res.prices = $.makeArray( res.prices );
res.prices.unshift({
Price : $('.mainquote-wrapper a.mainquote-price', $this).text(),
OTA : $('.mainquote-wrapper a.mainquote-agent', $this).text(),
});
return res;
});
res.Itineraries = $.makeArray( res.Itineraries );
if(res.Itineraries.length != 10)
{
console.log("no result found");
return;
}
console.log("Best Price: " + res.BestPrice + ", Orig: " + res.Itineraries[0].Origin + ", Dest: " + res.Itineraries[0].Dest);
searchdataChannel.publish('NewRes', res);
});
}
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}