-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-legacy-for-zip.js
102 lines (83 loc) · 2.71 KB
/
get-legacy-for-zip.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
98
99
100
101
102
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
const getLegacyUrl = (lat, lon) => {
return `https://forecast.weather.gov/MapClick.php?lat=${lat}&lon=${lon}&FcstType=json`;
};
const getAPIDailyForecastFromLatLon = async (lat, lon) => {
const pointsUrl = `https://api.weather.gov/points/${lat},${lon}`;
const pointsResponse = await fetch(pointsUrl);
if(!pointsResponse.ok){
console.error(`Could not get API points for ${lat}, ${lon}`);
Deno.exit(-1);
}
const pointData = await pointsResponse.json();
const forecastUrl = pointData.properties.forecast;
const forecastResponse = await fetch(forecastUrl);
if(!forecastResponse.ok){
console.error(`Could not get API forecast for ${forecastUrl}`);
Deno.exit(-1);
}
const forecastData = await forecastResponse.json();
return forecastData.properties.periods.map(period => {
let high, low = null;
if(period.isDaytime){
high = period.temperature;
} else {
low = period.temperature;
}
return {
kind: "api",
periodName: period.name,
startTime: period.startTime,
probabilityOfPrecipitation: period.probabilityOfPrecipitation.value,
description: period.shortForecast,
longDescription: period.detailedForecast,
high,
low
};
});
};
// Parse cli args
if(Deno.args.length < 1){
console.error('Must provide a zip code as the argument');
Deno.exit(-1);
}
const zip = Deno.args[0];
const zipLookupFile = await Deno.readTextFile('./zip-to-geo.json', 'utf-8');
const zipLookup = JSON.parse(zipLookupFile);
const found = zipLookup[zip];
if(!found){
console.error(`No matching data for zip code ${zip}`);
Deno.exit(-1);
}
const legacyResponse = await fetch(
getLegacyUrl(found.lat, found.lon)
);
if(!legacyResponse.ok){
console.error(`Invalid NWS response: ${legacyResponse.status}`);
Deno.exit(-1);
}
const data = await legacyResponse.json();
let resultPeriods = data.time.startPeriodName.map(name => {return {periodName: name};});
resultPeriods.forEach((obj, index) => {
obj.startTime = data.time.startValidTime[index];
obj.kind = "legacy";
if(data.time.tempLabel[index] == "High"){
obj.high = data.data.temperature[index];
obj.low = null;
} else {
obj.high = null;
obj.low = data.data.temperature[index];
}
obj.probabilityOfPrecipitation = data.data.pop[index];
obj.description = data.data.weather[index];
obj.longDescription = data.data.text[index];
});
const apiData = await getAPIDailyForecastFromLatLon(found.lat, found.lon);
let output = [];
apiData.forEach((apiPeriod, index) => {
if(resultPeriods[index]){
output.push(resultPeriods[index]);
}
output.push(apiPeriod);
});
console.log(JSON.stringify(output, null, 4));