-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathscraper_common.js
222 lines (205 loc) · 8.33 KB
/
scraper_common.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
const dotenv = require("dotenv");
//note: this only works locally; in Lambda we use environment variables set manually
dotenv.config();
const chromium = require("chrome-aws-lambda");
const { addExtra } = require("puppeteer-extra");
const Puppeteer = addExtra(chromium.puppeteer);
const { getAllCoordinates } = require("./getGeocode");
const {
logGlobalMetric,
logScraperRun,
getTotalNumberOfAppointments,
} = require("./lib/metrics");
const dataDefaulter = require("./data/dataDefaulter");
const fetch = require("node-fetch");
const file = require("./lib/file");
const Recaptcha = require("puppeteer-extra-plugin-recaptcha");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
const s3 = require("./lib/s3");
const { writeScrapedData } = require("./lib/db/scraper_data");
const moment = require("moment");
const AWS = require("aws-sdk");
const { Lambda } = require("faunadb");
const alertsLambda = new AWS.Lambda();
const { scrapersToSkip } = require("./scraper_config");
const WRITE_TO_FAUNA = true;
async function execute(usePuppeteer, scrapers) {
const globalStartTime = new Date();
let browser = null;
if (usePuppeteer) {
Puppeteer.use(StealthPlugin());
Puppeteer.use(
Recaptcha({
provider: { id: "2captcha", token: process.env.RECAPTCHATOKEN },
})
);
browser = process.env.DEVELOPMENT
? await Puppeteer.launch({
executablePath: process.env.CHROMEPATH,
headless: true,
})
: await Puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
}
const gatherData = async () => {
const results = [];
for (const scraper of scrapers) {
if (scrapersToSkip.indexOf(scraper.name) !== -1) {
console.log(
"Skipping... " + scraper.name + " (see scraper_config.js)"
);
continue;
}
const startTime = new Date();
let isSuccess = true;
const returnValue = await scraper.run(browser).catch((error) => {
//print out the issue but don't fail, this way we still publish updates
//for other locations even if this website's scrape doesn't work
console.log(error);
isSuccess = false;
return null;
});
const numberAppointments = getTotalNumberOfAppointments(
returnValue?.individualLocationData
);
await logScraperRun(
scraper.name,
isSuccess,
new Date() - startTime,
startTime,
numberAppointments
);
// Add the parentLocation's timestamp to each individual location
returnValue?.individualLocationData.map((loc) => {
loc.timestamp = returnValue.timestamp;
});
// Save the individualLocationData for the out.json file.
results.push(returnValue?.individualLocationData);
// Coerce the results into the format we want.
let returnValueArray = [];
if (Array.isArray(returnValue?.individualLocationData)) {
returnValueArray = returnValue.individualLocationData;
} else if (returnValue?.individualLocationData) {
returnValueArray = [returnValue.individualLocationData];
}
// Write the data to FaunaDB.
if (WRITE_TO_FAUNA && process.env.FAUNA_DB) {
try {
if (returnValue) {
console.log(JSON.stringify(returnValue));
await writeScrapedData(returnValue).then(
({
parentLocationRefId,
parentScraperRunRefId,
}) => {
if (process.env.NODE_ENV === "production") {
alertsLambda.invoke(
{
FunctionName:
process.env.ALERTSFUNCTIONNAME,
InvocationType: "Event",
Payload: JSON.stringify({
parentLocationRefId,
parentScraperRunRefId,
}),
},
() => {}
);
} else {
console.log(
"would call alerting function with the following args:"
);
console.log({
parentLocationRefId,
parentScraperRunRefId,
});
}
}
);
}
} catch (e) {
console.error("Failed to write to Fauna, got error:", e);
}
}
}
if (usePuppeteer) {
const pages = await browser.pages();
if (pages.length > 1) {
console.log(
"Did you forget to call page.close()? The following pages were left open... " +
pages.map((page) => page.url())
);
}
await browser.close();
}
let scrapedResultsArray = [];
for (const result of results) {
if (Array.isArray(result)) {
scrapedResultsArray.push(...result);
} else if (result) {
//ignore nulls
scrapedResultsArray.push(result);
}
}
let cachedResults;
if (process.env.NODE_ENV !== "test") {
cachedResults = await fetch(
"https://mzqsa4noec.execute-api.us-east-1.amazonaws.com/prod"
)
.then((res) => res.json())
.then((unpack) => JSON.parse(unpack.body).results);
}
let finalResultsArray = [];
if (process.argv.length <= 2) {
// Only add default data if we're not testing individual scrapers.
// We are not passing in the optional 3rd arg of mergeResults;
// this means that there is no time limit on stale data being merged in.
finalResultsArray = dataDefaulter.mergeResults(
scrapedResultsArray,
cachedResults
);
} else {
finalResultsArray = scrapedResultsArray;
}
const responseJson = {
// Version number of the file
version: 1,
// Timestamp for the archived data.json file.
timestamp: s3.getTimestampForFile(),
// Add geocoding for all locations
results: await getAllCoordinates(finalResultsArray, cachedResults),
};
logGlobalMetric(
usePuppeteer ? "SuccessfulRun" : "SuccessfulNoBrowserRun",
1,
new Date()
);
logGlobalMetric(
usePuppeteer ? "Duration" : "NoBrowserDuration",
new Date() - globalStartTime,
new Date()
);
const webData = JSON.stringify(responseJson, null, 2);
if (process.env.NODE_ENV !== "production") {
const outFile = usePuppeteer ? "out.json" : "out_no_browser.json";
console.log(
"The data that would be published is in '" + outFile + "'"
);
file.write(outFile, webData);
return responseJson;
} else {
const uploadResponse = await s3.saveWebData(
webData,
responseJson.timestamp
);
return uploadResponse;
}
};
return await gatherData();
}
module.exports = { execute };