-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·69 lines (63 loc) · 1.68 KB
/
index.js
File metadata and controls
executable file
·69 lines (63 loc) · 1.68 KB
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
#!/usr/bin/env node
const chalk = require('chalk');
const request = require('request');
const Table = require('cli-table3');
const config = require('./config');
const readlineSync = require('readline-sync');
//ask user for city
function registerCity(){
var city = "";
do{
city = readlineSync.question(chalk.cyanBright('Where do you live? '));
}while(city=="");
console.log(chalk.cyanBright(`Looking up prayer times in ${city}..\n\n\n`));
config.CITY = city;
config.API_URL += "&city="+config.CITY;
};
//details of GET request sent to prayer times API
function adanRequest(){
const options = {
url: config.API_URL,
method: 'GET'
};
request(options, (err, res, body) => {
if (err) {
return false;
}
if(body.length == 0) {
console.log(chalk.redBright(`${config.CITY} doesn't exist..`));
return false;
}
var val = JSON.parse(body);
if(typeof val === undefined){
console.log(chalk.redBright(`${config.CITY} doesn't exist..`));
return false;
}
else{
val = val.results.datetime[0].times;
var times = [];
var values = [];
for(var j in val){
var sub_key = j;
times.push(sub_key);
var sub_val = val[j];
values.push(sub_val);
}
// Create table for prayer times :D
var table = new Table({
head: times,
});
table.push(values);
console.log(chalk.yellowBright(`Below are the prayer times in ${config.CITY}: \n `));
console.log(table.toString());
return true;
}
})
};
//main adan-cli function
function main(){
console.log(chalk.magentaBright("Welcome to Chai's CLI tool to show you prayer times in your city (。◕‿‿◕。)\n\n"));
registerCity();
adanRequest();
};
main();