-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.js
99 lines (74 loc) · 3.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const {ipcRenderer, shell} = require('electron')
document.addEventListener('click', (event) => {
if (event.target.href) {
// Open links in external browser
shell.openExternal(event.target.href)
event.preventDefault()
} else if (event.target.classList.contains('js-refresh-action')) {
updateWeather()
} else if (event.target.classList.contains('js-quit-action')) {
window.close()
}
})
const getGeoLocation = () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject)
})
}
const getWeather = (position) => {
// FIXME replace with your own API key
// Register for one at https://darksky.net/dev/
const apiKey = '98cab43abdc8442a64255fc0a9f10b97'
const location = `${position.coords.latitude},${position.coords.longitude}`
console.log(`Getting weather for ${location}`)
const url = `https://api.forecast.io/forecast/${apiKey}/${location}`
return window.fetch(url).then((response) => {
return response.json()
})
}
const updateView = (weather) => {
const currently = weather.currently
document.querySelector('.js-summary').textContent = currently.summary
document.querySelector('.js-update-time').textContent = `at ${new Date(currently.time).toLocaleTimeString()}`
document.querySelector('.js-temperature').textContent = `${Math.round(currently.temperature)}° F`
document.querySelector('.js-apparent').textContent = `${Math.round(currently.apparentTemperature)}° F`
document.querySelector('.js-wind').textContent = `${Math.round(currently.windSpeed)} mph`
document.querySelector('.js-wind-direction').textContent = getWindDirection(currently.windBearing)
document.querySelector('.js-dewpoint').textContent = `${Math.round(currently.dewPoint)}° F`
document.querySelector('.js-humidity').textContent = `${Math.round(currently.humidity * 100)}%`
document.querySelector('.js-visibility').textContent = `${Math.round(currently.windSpeed)} miles`
document.querySelector('.js-cloud-cover').textContent = `${Math.round(currently.cloudCover * 100)}%`
document.querySelector('.js-precipitation-chance').textContent = `${Math.round(currently.precipProbability * 100)}%`
document.querySelector('.js-precipitation-rate').textContent = currently.precipIntensity
}
const getWindDirection = (direction) => {
if (direction < 45) return 'NNE'
if (direction === 45) return 'NE'
if (direction < 90) return 'ENE'
if (direction === 90) return 'E'
if (direction < 135) return 'ESE'
if (direction === 135) return 'SE'
if (direction < 180) return 'SSE'
if (direction === 180) return 'S'
if (direction < 225) return 'SSW'
if (direction === 225) return 'SW'
if (direction < 270) return 'WSW'
if (direction === 270) return 'W'
if (direction < 315) return 'WNW'
if (direction === 315) return 'NW'
if (direction < 360) return 'NNW'
return 'N'
}
const updateWeather = () => {
getGeoLocation().then(getWeather).then((weather) => {
// Use local time
weather.currently.time = Date.now()
console.log('Got weather', weather)
ipcRenderer.send('weather-updated', weather)
updateView(weather)
sendNotification(weather)
previousWeather = weather
})
}
// Update initial weather when loaded
document.addEventListener('DOMContentLoaded', updateWeather)