forked from csuwildcat/DXCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserbite-downloader.js
164 lines (127 loc) · 4.87 KB
/
browserbite-downloader.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
159
160
161
162
163
164
"use strict";
var promised = require("promised-io/promise");
var Deferred = require('promised-io').Deferred;
var fs = require('fs');
var _ = require('lodash');
var baseUrl = 'http://app.browserbite.com';
var apiUrl = baseUrl + '/requests/';
var requestId = '26751';
var authKey = '44cd5c55';
var jobUrl = apiUrl + '%requestId%.json?authkey=%authKey%';
var issuesUrl = apiUrl + '%requestId%/%uniqueId%/differences/%captureId%.json?authkey=%authKey%';
var results = {};
var loaded = false;
var batch = require('./lib/batch.js');
var request = require('./lib/customRequest.js');
var start = Date.now();
var formatUrl = function (url, properties) {
var keys = Object.keys(properties);
for (var i = 0; i < keys.length; i++) {
var property = keys[i];
url = url.replace(new RegExp('%' + property + '%', 'g'), properties[property]);
}
return url;
}
var promises = [];
var requests = 0;
var getBrowserCapture = function (webpage) {
return function (capture) {
//var deferred = new Deferred();
requests++;
//promises.push(deferred.promise);
request(webpage.url,
function (err, response, body) {
var parsedBody;
var id = capture.is_baseline ?
'baseline' :
(capture.browser.group + capture.browser.version).replace('http://');
if (err) {
parsedBody = {};
} else {
try {
parsedBody = JSON.parse(body);
} catch (e) {
console.log(e);
parsedBody = {};
}
}
var issues = _.reduce(parsedBody.issues, function (acum, current) {
current.path = baseUrl + '/images/shots/' + capture.comparison_dir + '/' + current.path;
if (current.dynamic) {
acum.dynamic.push(current);
} else {
acum.static.push(current);
}
return acum;
}, {
dynamic: [],
static: []
});
var processedCapture = {
passes: !capture.is_defected,
img: baseUrl + '/images/shots/' + capture.images.document,
issues: issues
};
results[webpage.id].results[id] = processedCapture;
requests--;
if (requests === 0) {
loaded = true;
fs.writeFileSync('browserbiteresults.json', JSON.stringify(results, null, 2));
console.log('done!');
var totalTime = Math.ceil((Date.now() - start) / 1000);
console.log('Total time: ' + totalTime);
}
//deferred.resolve();
});
};
};
var processWebpage = function (webpage) {
var result = {
//id: webpage.url.replace('http://', ''),
id: webpage.website,
url: webpage.url,
results: {}
};
results[result.id] = result;
_.forEach(webpage.data, getBrowserCapture(result));
};
var getResults = function () {
console.log('finishing downloading websites resuts');
console.log('saving websites links');
fs.writeFile(websitesLinksFile, JSON.stringify(websites, null, 2));
_.forEach(websites, processWebpage);
console.log('downloading captures');
};
var websitesLinksFile = 'websites-links.json';
if (fs.existsSync(websitesLinksFile)) {
console.log('reading websites links');
var websites = JSON.parse(fs.readFileSync(websitesLinksFile, 'utf8'));
getResults();
} else {
var websitesFileName = 'browserbite-websites.csv';
var websites = (function () {
var websitesFile = fs.readFileSync(websitesFileName, { encoding: 'utf8' });
return _.reduce(websitesFile.split('\r\n'), function (acum, val) {
var items = val.split(',');
if (items.length === 2 && items[1] !== '') {
acum.push({
website: 'http://' + items[0],
url: items[1]
});
}
return acum;
}, []);
}());
batch.onFinish = getResults;
var count = 0;
var urls = _.pluck(websites, 'url');
console.log('Downloading ' + urls.length + ' websites info');
batch.start(10, urls, function (results) {
var website = _.find(websites, function (website) {
return website.url === results.url;
});
if (website) {
website.data = JSON.parse(results.body).webpages[0].captures;
}
});
}