-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
executable file
·125 lines (98 loc) · 3.61 KB
/
app.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
#!/usr/bin/env node
const CSVToJSON = require('csvtojson');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const inquirer = require('inquirer');
const axios = require('axios');
const axiosRetry = require("axios-retry");
const { Client, Status } = require('@googlemaps/google-maps-services-js');
const client = new Client({});
axiosRetry(axios, {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
shouldResetTimeout: true,
retryCondition: (axiosError) => {
return true;
},
});
(async function() {
clear();
console.log(chalk.yellow(
figlet.textSync('The Pharmacist', { horizontalLayout: 'full' })
));
console.log('\n');
const answers = await inquirer.prompt([
{
name: 'filename',
message: 'Enter filename: ',
},
{
name: 'apiKey',
message: 'Enter Google API key: ',
},
]);
console.log('\n');
console.log(chalk.cyan('Processing...'));
console.log('--------------------------------------------------------------');
try {
const filename = answers.filename;
const apiKey = answers.apiKey;
const data = await CSVToJSON().fromFile(filename);
const currentPath = process.cwd();
const d = new Date();
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
const mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d);
const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);
const currentDate = `${da}-${mo}-${ye}`;
let formattedList = [];
let id = 0;
for (let item of data) {
let newItem = {};
id++;
newItem.id = id;
newItem.name = item['Pharmacy Name'];
newItem.address = `${item.Address}, ${item.City}, ${item.State} ${item['Post Code']}`;
newItem.suburb = item.City;
newItem.state = item.State;
newItem.postcode = parseInt(item['Post Code']);
newItem.phone = item.Phone;
newItem.homeDelivery = item['Home Delivery Service Available'] === 'Yes' ? true : false;
setTimeout(() => client
.geocode({
params: {
address: newItem.address,
key: apiKey,
},
rate: 50,
timeout: 2000,
})
.then((response) => {
newItem.coords = {
lat: response.data.results[0].geometry.location.lat, lng: response.data.results[0].geometry.location.lng,
};
console.log(newItem);
console.log('--------------------------------------------------------------');
formattedList.push(newItem);
formattedList.sort((a,b) => a.id - b.id);
fs.writeFile(
`${currentPath}/output-${currentDate}.js`,
JSON.stringify(formattedList, null, 4).replace(/"([^"]+)":/g, '$1:'),
(err) => {
if (err) {
console.log(err)
}
return;
});
})
.catch((error) => {
console.log(error);
}), 1000 * id);
}
} catch (err) {
console.log(chalk.red('Error'));
console.log(err);
}
})();