forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalanche.coffee
85 lines (70 loc) · 1.71 KB
/
avalanche.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
# Description:
# Displays the current avalanche forecast for norway.
#
# Dependencies:
# "jsdom": "0.2.14"
# Configuration:
# None
#
# Commands:
# hubot avy me - Return a breakdown of the avalanche forecast from varsom.no
#
# Author:
# Alastair Brunton
jsdom = require 'jsdom'
getScores = ($, cb) ->
results = []
area_scores = {}
trs = $("tr")
tr_length = trs.length
trs.each ((index) ->
getRiskValues($, $(this), (err, values) ->
results.push values
if results.length == tr_length - 1
cb null, results
else
# do nothing
)
)
getRiskValues = ($, element, cb) ->
riskValues = []
element.children("td.hazard").each((index) ->
riskValues.push $(this).text().trim()
if riskValues.length == 3
cb null, riskValues
else
# do nothing
)
getPlaces = ($, cb) ->
locations = $("span.location")
locationNames = []
numLocations = locations.length
locations.each((index) ->
$(this).children().each((index2) ->
locationNames.push $(this).text()
if locationNames.length == numLocations
cb null, locationNames
else
# do nothing
)
)
joinArs = (places, scores) ->
joinedAr = []
i = 0
for place in places
score = scores[i].join(" ")
joinedAr.push("#{place} #{score}")
i = i + 1
joinedAr.join("\n")
module.exports = (robot)->
robot.respond /avy me/i, (msg) ->
jsdom.env("http://varsom.no/Snoskred/", ['http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'],
(errors, window) ->
$ = window.$
getScores($, (err, scores) ->
getPlaces($, (err, places) ->
avyReport = joinArs(places, scores)
msg.send avyReport
)
)
)