-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather-module.js
103 lines (87 loc) · 2.85 KB
/
weather-module.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
103
const https = require("https")
const API_KEY = require("./owm_api_key.js").key
const URL = `https://api.openweathermap.org/data/2.5/weather?lat=53.551086&lon=9.993682&units=metric&appid=` + API_KEY
const ICON_COLOR = "#EE8000"
const colorize = icon => `%{F${ICON_COLOR}}${icon}%{F-} `
const randomize = icons => {
return icons[Math.round(Math.random() * (icons.length - 1))]
}
const request = https.request(URL, response => {
let data = ""
response.on("data", chunk => {
data += chunk.toString()
})
response.on("end", () => {
const body = JSON.parse(data)
let weatherDescription = body.weather[0].description + ","
const now = new Date().getTime() / 1000
let isNight = true
if (now > body.sys.sunrise && now < body.sys.sunset) {
isNight = false
}
switch (body.weather[0].description) {
case "clear sky":
weatherDescription = colorize(isNight ? randomize(["", "", ""]) : "")
break
case "light rain":
weatherDescription = colorize(isNight ? "" : "")
break
case "broken clouds":
weatherDescription = colorize(isNight ? "" : "")
break
case "scattered clouds":
weatherDescription = colorize(isNight ? "" : "")
break
case "light intensity drizzle rain":
weatherDescription = colorize(isNight ? "" : "")
break
case "light intensity drizzle":
weatherDescription = colorize(isNight ? "" : "")
break
case "light intensity shower rain":
weatherDescription = colorize(isNight ? "" : "")
break
case "few clouds":
weatherDescription = colorize(isNight ? "" : "")
break
case "overcast clouds":
weatherDescription = colorize("")
break
case "fog":
weatherDescription = colorize(isNight ? "" : "")
break
case "mist":
weatherDescription = colorize(isNight ? "" : "")
break
case "moderate rain":
weatherDescription = colorize("")
break
case "snow":
weatherDescription = colorize("")
break
case "light snow":
weatherDescription = colorize("")
break
case "shower rain":
weatherDescription = colorize("")
break
case "heavy intensity rain":
weatherDescription = colorize("")
break
case "thunderstorm with heavy rain":
weatherDescription = colorize("")
break
case "tunderstorm with rain":
weatherDescription = colorize("")
break
}
if (Math.floor(Math.random() * 10 ** 100) == 420) {
weather = colorize("")
}
console.log(`${weatherDescription}${Math.round(body.main.feels_like)}°C`)
})
})
request.on("error", error => {
throw new Error(error)
})
request.end()