generated from comp426-2022-fall/a02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·98 lines (89 loc) · 2.95 KB
/
cli.js
File metadata and controls
executable file
·98 lines (89 loc) · 2.95 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
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 tz = require ('moment-timezone')
import mt from 'moment-timezone';
const args = process.argv.slice(2)
// console.log('args',process.argv,args)
//process.abort()
function showhelp() {
console.log("Usage: galosh.js [options] -[n|s] LATITUDE -[e|w] LONGITUDE -z TIME_ZONE")
console.log("-h Show this help message and exit.")
console.log("-n, -s Latitude: N positive; S negative.")
console.log("-e, -w Longitude: E positive; W negative.")
console.log("-z Time zone: uses tz.guess() from moment-timezone by default.")
console.log("-d 0-6 Day to retrieve weather: 0 is today; defaults to 1.")
console.log("-j Echo pretty JSON from open-meteo API and exit.")
}
//showhelp()
let exitCode = 1;
if (args.length == 0) {
console.log("no arguments")
}
let counter = 0;
let latitude;
let longitude;
let timezone = mt.tz.guess();
let day = 1
let prettyprint = false
for (const arg of args) {
if (arg === '-h') {
showhelp();
exitCode = 0
}
if (arg == `-n`) {
latitude = parseFloat(args[counter + 1])
}
if (arg == `-s`) {
latitude = -parseFloat(args[counter + 1])
}
if (arg == `-e`) {
longitude = +parseFloat(args[counter + 1])
}
if (arg == `-w`) {
longitude = -parseFloat(args[counter + 1])
}
if (arg == `-z`) {
timezone = args[counter + 1]
}
if (arg == `-d`) {
day = +args[counter + 1]
if (day > 6 || day < 0) {
console.log("day must be a number from 0 to 6")
exitCode = 1
}
}
if (arg == `-j`) {
prettyprint = true
}
counter++;
}
// getweather(latitude, longitude, timezone, day, prettyprint)
console.log({latitude,longitude,timezone,day})
function getweather(latitude, longitude, timezone, day, prettyprint) {
// get start and end dates
const start_date = mt().add(day, "day").format('YYYY-MM-DD')
const end_date = mt().add(day, "day").format('YYYY-MM-DD')
fetch(`https://api.open-meteo.com/v1/forecast?start_date=${start_date}&end_date=${end_date}&latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t_weather=true&timezone=${timezone}`)
.then(
(result) => {
result.json()
.then(
(data) => {
if (prettyprint) {
console.log(JSON.stringify(data, null, 2))
}
else {
console.log(data)
}
process.exit(0)
}
)
.catch((error) => {
console.log(error)
process.exit(1)
})
}
).catch((error) => {
console.log(error)
process.exit(1)
})
}