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
// import command line parsing from the std
import { parse } from "https://deno.land/std/flags/mod.ts";
// parse command line flags to get location and duration
const { lat, long, duration } = parse(Deno.args);
// lookup url for forecast specified at lat, long
let res = await fetch(`https://api.weather.gov/points/${lat},${long}`);
const json = await res.json();
const { city, state } = json.properties.relativeLocation.properties;
const url = json.properties.forecastHourly;
// fetch actual forecast data
res = await fetch(url);
const forecast = await res.json();
// limit console output to specified duration
const periods = forecast.properties.periods.slice(0, duration);
console.log(`The forecast for ${city}, ${state}` +
`for the next ${periods.length} hours 🔮\n`);
// iterate through each hour's forecast
for (let p of periods) {
const timestamp = new Date(p.startTime);
const hours = `${timestamp.getHours() % 12 || 12}`.padStart(2);
// convert 24h time to 12h
const cycle = (timestamp.getHours() / 12) < 1 ? 'AM' : 'PM';
const units = p.temperatureUnit === 'F' ? '℉' : '℃';
// add padding to make thing print nicer
const weather = p.shortForecast.padStart(12);
const direction = p.windDirection.padEnd(3);
console.log(`${hours} ${cycle}\t${weather}\t${p.temperature}` +
`${units}\t${p.windSpeed} ${direction} 🌬`);
}