/
log-to-psql.js
executable file
·41 lines (33 loc) · 1.12 KB
/
log-to-psql.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
#!/usr/bin/env node
const laundry = require("./fetch");
const dorm_names = require("./dorm_names");
process.setMaxListeners(0);
const { Pool } = require("pg");
const pool = new Pool();
async function push(machines, dorm_name, dorm_id) {
const client = await pool.connect();
try {
await client.query("BEGIN");
const res = machines.map(machine => {
return client.query(
"INSERT INTO ucla_laundry" +
" (dorm_name, dorm_id, index, type, status, eta)" +
" VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
[dorm_name, String(dorm_id), String(machine.index),
machine.type, machine.status, machine.eta]
);
});
/*console.log((await Promise.all(res)).map(result => result.rows[0]));*/
await client.query("COMMIT");
} catch (e) {
await client.query("ROLLBACK");
throw e;
} finally {
await client.release();
}
}
dorm_names.map(async (name, index) => {
const machines = await laundry({quiet:true}, index);
await push(machines, name, index);
console.log(`${name.padEnd(15, ' ')} - ${String(machines.length).padStart(2, ' ')} machines scraped`);
});