-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·97 lines (73 loc) · 2.73 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
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
#!/usr/bin/env node
const chalk = require('chalk');
const cli = require('commander');
const Configstore = require('configstore');
const fs = require('fs');
const promptly = require('promptly');
const pkg = require('./package.json');
const {
printOrderByRestaurantsGraph,
printOrdersOfLastMonthsGraph,
printOrdersByDayGraph,
calculateTotalMoneySpent,
} = require('./orders.js');
const scrapeZomatoOrders = require('./zomato');
const SECTION_BREAK = '\n\n';
const readOrdersFromFile = file => JSON.parse(fs.readFileSync(file));
const writeOrdersToFile = (orders, file) => fs.writeFileSync(file, JSON.stringify(orders));
const conf = new Configstore(pkg.name);
const getCredentialsFromPrompt = async () => {
console.log('Please enter your Zomato credentials');
emailID = await promptly.prompt('Username / Email: ', { trim: true });
password = await promptly.prompt('Password: ', { trim: true, silent: true, replace: '*' });
conf.set('emailID', emailID);
conf.set('password', password);
return { emailID, password };
};
const getUserCredentials = async () => {
const emailID = conf.get('emailID');
const password = conf.get('password');
if (emailID && password) {
return { emailID, password };
}
return await getCredentialsFromPrompt();
};
async function main() {
let orders;
if (cli.input) {
orders = readOrdersFromFile(cli.input);
} else {
const { emailID, password } = await getUserCredentials();
orders = await scrapeZomatoOrders({ emailID, password });
if (cli.save) {
writeOrdersToFile(orders, cli.save);
}
}
const isOrderDelivered = ({ status }) => status === 'Delivered';
const deliveredOrders = orders.filter(isOrderDelivered);
console.log(chalk.bold(`\nYour total delivered orders are: ${deliveredOrders.length}`));
console.log(chalk.bold(`Total money spent: ${calculateTotalMoneySpent(orders)}`))
console.log(SECTION_BREAK);
console.log(chalk.bold("Top 10 Restaurants from where you've ordered:"));
printOrderByRestaurantsGraph(deliveredOrders, 10);
console.log(SECTION_BREAK);
console.log(chalk.bold('Distribution of your spendings over the last 12 Months:'));
printOrdersOfLastMonthsGraph(deliveredOrders, 12);
console.log(SECTION_BREAK);
console.log(chalk.bold('Weekday wise distribution of your spendings:'));
printOrdersByDayGraph(deliveredOrders);
}
(async () => {
cli
.version(pkg.version)
.option('-i, --input <file>', 'Read orders from file')
.option('-s, --save <file>', 'Save the extracted orders to file')
.option('-c, --configure', 'Update your user credentials')
.parse(process.argv);
if (cli.configure) {
await getCredentialsFromPrompt();
console.log('Your credentials have been updated');
return;
}
main();
})();