This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.coffee
124 lines (117 loc) · 4.1 KB
/
weather.coffee
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
Promise = require("promise")
Module = require("../module")
http = require("http")
natural = require("natural")
verbInflector = new natural.PresentVerbInflector()
class WeatherModule extends Module
apiKey = "{{YOURAPIKEY}}" # register at http://openweathermap.org to get your APPID to use as an API key
constructor: (@socket) ->
console.log ">>> WeatherModule"
parseTemperature: (temp) ->
temp = Math.round(temp)
response = ""
condition = 0
if temp < 0
response = "extremely cold, below freezing"
condition = 0
else if temp < 3
response = "very cold, near freezing"
condition = 0
else if temp < 5
response = "quite cold, around #{temp} degrees"
condition = 1
else if temp < 10
response = "cold, around #{temp} degrees"
condition = 2
else if temp < 15
response = "cool, around #{temp} degrees"
condition = 3
else if temp < 20
response = "moderate, around #{temp} degrees"
condition = 4
else if temp < 25
response = "warm, around #{temp} degrees"
condition = 5
else if temp < 30
response = "hot, around #{temp} degrees"
condition = 6
else
response = "very hot, above 30 degrees"
condition = 6
{
response: response
condition: condition
}
parseRain: (rain, snow) ->
response = ""
if rain
if rain["3hr"] < 0.2
response = "might rain"
else if rain["3hr"] < 0.5
response = "is likely to rain"
else if rain["3hr"] < 1
response = "will probably rain"
else
response = "will rain"
response
runClientSide: =>
new Promise (resolve, reject) =>
# set up transcript
transcript = "Currently the weather is "
# get location
http.get("http://ipinfo.io", (res) =>
body = ""
res.on("data", (chunk) ->
body += chunk
)
res.on("end", =>
res = JSON.parse(body)
location = res.loc.split(",")
# get the forcast
http.get("http://api.openweathermap.org/data/2.5/forecast?lat=#{location[0]}&lon=#{location[1]}&units=metric&appid=#{apiKey}", (res) =>
body = ""
res.on("data", (chunk) ->
body += chunk
)
res.on("end", =>
res = JSON.parse(body)
# upcoming is in list
list = res.list
# get now
now = list[0]
nowTemp = @parseTemperature(now.main.temp)
# add current temp to transcript
transcript += nowTemp.response
isRaining = now.weather.main is "Rain"
if isRaining
transcript += " and it is raining"
# get soon
soon = list[1]
soonTemp = @parseTemperature(soon.main.temp)
soonRain = @parseRain(soon.rain)
if soonRain isnt "" and isRaining is false
transcript += " and it #{soonRain} soon"
# get later
later = list[2]
laterTemp = @parseTemperature(later.main.temp)
laterRain = @parseRain(soon.rain)
# check to see if it changes
laterPostfx = false
if Math.abs(now.main.temp - later.main.temp) > 2.5
warmerColder = if now.main.temp - later.main.temp > 0 then "colder" else "warmer"
transcript += ", but will become #{warmerColder}, around #{Math.round(later.main.temp)} degrees later"
laterPostfx = true
if laterRain isnt soonRain
if laterRain is ""
transcript += " and doesn't look like it will rain #{if laterPostfx is false then "later"}"
else
transcript += " and it #{laterRain} #{if laterPostfx is false then "later"}"
transcript += "."
console.log new Date().toString(), "WeatherModule ::> runClientSide > transcript: '#{transcript}'"
@socket.emit("speech", speak: transcript)
resolve()
)
)
)
)
module.exports = WeatherModule