-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.js
65 lines (57 loc) · 1.73 KB
/
location.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
const fetch = require("node-fetch");
const Sequelize = require('sequelize');
const axios = require('axios');
const token = 'pk.eyJ1IjoieWFrb3ZsZXZmcm9udGVuZCIsImEiOiJja2IzZXQ2amIwZTJzMzJ0ODNkbjM5cHplIn0.McJ1QES2k_kA3vKALtx-iA';
// or we can load data from bigQuery?
const data = require('./users');
// db, user, password
const sequelize = new Sequelize('locations', 'lonya', '', {
host: 'localhost',
port: 5432,
dialect: 'postgres'
});
const locationModel = sequelize.define('users', {
id: {
primaryKey: true,
type: Sequelize.INTEGER,
},
location: Sequelize.STRING,
reputation: Sequelize.INTEGER,
mapbox: Sequelize.JSON
}, {
timestamps: false
});
// clear table
// locationModel.destroy({ truncate: true , restartIdentity: true });
// help function
const loadLocation = async (token, location) => {
let res = location ? `${location}.json` : '';
let locationToUrl = encodeURI(res);
try {
let load = await axios.get(`https://api.mapbox.com/geocoding/v5/mapbox.places/${locationToUrl}?access_token=${token}`);
let result = await load.data;
return result
} catch (e) {
console.log(e);
return null;
}
};
(async function() {
for (let i = 0; i < data.length; i++) {
try {
let mapbox = await loadLocation(token, data[i].location) || null;
if (mapbox) {
let ob = {
id: data[i].id,
location: data[i].location,
reputation: data[i].reputation,
mapbox: mapbox,
};
locationModel.create(ob);
}
} catch (e) {
console.log(e);
}
console.log(i);
}
})();