-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (77 loc) · 2.19 KB
/
index.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
/* global geolocator */
/**
* @param {number} number
* @param {number} precision
*/
function round(number, precision) {
const shift = function (num, precis, reverseShift) {
if (reverseShift) {
precis = -precis
}
const numArray = ('' + num).split('e')
return +(
numArray[0] + 'e' + (numArray[1] ? +numArray[1] + precis : precis)
)
}
return shift(Math.round(shift(number, precision, false)), precision, true)
}
window.onload = function () {
const options = {
enableHighAccuracy: true,
timeout: 10000,
maximumWait: 20000,
maximumAge: 0
}
// @ts-ignore
geolocator.locate(options, function (err, location) {
if (err) return geoLocationFail(err, location)
$.get(
'https://api.weather.gov/points/' +
location.coords.latitude +
',' +
location.coords.longitude,
getFirstStation
).fail(function () {
$('h2.answer').text('Sorry, weather service API did not work right now')
})
})
}
function geoLocationFail(err, location) {
$('h2.answer').text(
'Sorry, geolocation did not work, or is not enabled in your browser'
)
console.error(err.message)
console.log(location)
}
// get the list of weather stations near the user's location
function getFirstStation(data) {
const observationStationsURL = data.properties.observationStations
$.get(observationStationsURL, function (stations) {
stationData(stations.observationStations[0])
}).fail(function () {
$('h2.answer').text(
'Sorry, the Weather Service API observations stations list was not available'
)
})
}
// get current observation for station
function stationData(url) {
$.get(url, function (station) {
$.get(
`https://api.weather.gov/stations/${station.properties.stationIdentifier}/observations/`,
function (data) {
updater(data.features[0].properties.temperature.value)
$('span.location').text(station.properties.name)
}
)
})
}
function updater(tmp) {
const tempF = round(tmp * (9 / 5) + 32, 0)
const coldEnough = tempF <= 46
if (coldEnough) {
$('h2.answer').text('🍺 HELL YEAH!')
} else {
$('h2.answer').text('🌴 No way, José. It\'s only ' + tempF + ' degrees.')
}
}