-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.69 KB
/
index.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
// This version fetches data from the publicstuff API and write it to a database;
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const { Client } = require("pg")
require("dotenv").config()
async function go(){
try{
let api_url_base = 'https://api.publicstuff.com/app/requests?limit=500&filters[client_id]=819'
let api_url = api_url_base
let api_key = process.env.api_key
let next_page = 0
const client = new Client({
host: process.env.host,
user: process.env.user,
port: process.env.port,
password: process.env.password,
database: process.env.database,
max: 10,
idleTimeoutMillis: 10000,
});
await client.connect();
do {
let res = await fetch(api_url, {
method: 'GET',
headers: {
'Authorization': 'Token token=' + api_key
},
})
data = await res.json()
next_page = data.properties.next_page
api_url = api_url_base + '&page=' + next_page
await load_db(client,data.entities)
} while (next_page)
await client.end()
}catch(err){
console.log(err)
}
}
go()
async function load_db(client,rows) {
try {
for(const row of rows) {
let sql = `
INSERT INTO avl_app_requests (id, json_data)
VALUES(${row.properties.id},'${JSON.stringify(row).replace(/'/g,"''")}')
`
console.log(sql)
const res = await client.query(sql)
}
}
catch(err){
throw err
}
}