-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (29 loc) · 1015 Bytes
/
index.ts
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
import { parse } from 'https://deno.land/std@0.61.0/flags/mod.ts';
import { fromUnixTime, format } from 'https://deno.land/x/date_fns@v2.15.0/index.js';
import AsciiTable from 'https://deno.land/x/ascii_table/mod.ts';
const args = parse(Deno.args);
if (args.city === undefined) {
console.error("No city supplied");
Deno.exit();
}
const apiKey = "75c820f8630ffd51e18f9ed9ffd46cf5";
const res = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?q=${args.city}&units=metric&appid=${apiKey}`,
);
const data = await res.json();
interface forecastItem {
dt: string;
main: { temp: number };
weather: { description: string }[];
}
const forecast = data.list.slice(0, 8).map((item: forecastItem) => [
format(fromUnixTime(item.dt), "do LLL, k:mm", {}),
`${item.main.temp.toFixed(1)}C`,
item.weather[0].description,
]);
const table = AsciiTable.fromJSON({
title: `${data.city.name} Forecast`,
heading: ["Time", "Temp", "Weather"],
rows: forecast,
});
console.log(table.toString());