-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentFunc.js
More file actions
69 lines (62 loc) · 2.36 KB
/
commentFunc.js
File metadata and controls
69 lines (62 loc) · 2.36 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
const puppeteer = require('puppeteer');
const axios = require('axios');
const { parseString } = require('xml2js');
const stateAbbreviations = require('states-abbreviations');
const getState = zip => new Promise(async (resolve, reject) => {
axios.get('https://secure.shippingapis.com/ShippingAPI.dll?', {
params: {
API: 'CityStateLookup',
xml: `<CityStateLookupRequest USERID="661DEMAN2298"><ZipCode ID="0"><Zip5>${zip}</Zip5></ZipCode></CityStateLookupRequest>`
}
})
.then((response) => {
const xml = response.data;
parseString(xml, (err, result) => {
const zipStateCityObj = result.CityStateLookupResponse.ZipCode[0];
const state = zipStateCityObj.State[0];
const stateFullName = stateAbbreviations[state];
resolve({ stateFullName });
});
})
.catch((error) => {
reject(error);
});
});
const postComment = reqBody => new Promise(
async (resolve, reject) => {
const {
first_name, last_name, email, zip, fcc_comment
} = reqBody;
const cityStateObj = await getState(zip);
const { stateFullName } = cityStateObj;
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
try {
await page.goto('https://www.regulations.gov/comment?D=FTC-2018-0049-0001', { waitUntil: 'networkidle0' })
await page.type('#x-auto-0-input', fcc_comment);
await page.type('#x-auto-1-input', first_name);
await page.type('#x-auto-2-input', last_name);
await page.type('#x-auto-6-input', stateFullName.trim());
await page.type('#x-auto-8-input', zip);
await page.type('#x-auto-10-input', email);
await page.evaluate((form) => {
form.click();
}, (await page.$x('/html/body/div[3]/div[2]/div[2]/div[3]/div/div[3]/div[1]/form/div[5]/span/button'))[0])
await page.click('#gwt-uid-250');
await page.click('#gwt-uid-382');
await page.waitFor(3000);
await page.type('#x-auto-21-input', email);
await page.evaluate((form) => {
form.click();
}, (await page.$x('/html/body/div[3]/div[2]/div[2]/div[3]/div/div[3]/div[3]/div[1]/div/div[1]/button'))[0])
await browser.close();
await resolve('completed');
} catch (error) {
reject(error);
browser.close();
}
},
);
module.exports = postComment;