|
| 1 | +# Description: |
| 2 | +# Displays the current avalanche forecast for norway. |
| 3 | +# |
| 4 | +# Dependencies: |
| 5 | +# "jsdom": "0.2.14" |
| 6 | +# Configuration: |
| 7 | +# None |
| 8 | +# |
| 9 | +# Commands: |
| 10 | +# hubot avy me - Return a breakdown of the avalanche forecast from varsom.no |
| 11 | +# |
| 12 | +# Author: |
| 13 | +# Alastair Brunton |
| 14 | + |
| 15 | +jsdom = require 'jsdom' |
| 16 | + |
| 17 | +getScores = ($, cb) -> |
| 18 | + results = [] |
| 19 | + area_scores = {} |
| 20 | + trs = $("tr") |
| 21 | + tr_length = trs.length |
| 22 | + |
| 23 | + trs.each ((index) -> |
| 24 | + getRiskValues($, $(this), (err, values) -> |
| 25 | + results.push values |
| 26 | + if results.length == tr_length - 1 |
| 27 | + cb null, results |
| 28 | + else |
| 29 | + # do nothing |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | +getRiskValues = ($, element, cb) -> |
| 34 | + riskValues = [] |
| 35 | + element.children("td.hazard").each((index) -> |
| 36 | + riskValues.push $(this).text().trim() |
| 37 | + if riskValues.length == 3 |
| 38 | + cb null, riskValues |
| 39 | + else |
| 40 | + # do nothing |
| 41 | + ) |
| 42 | + |
| 43 | +getPlaces = ($, cb) -> |
| 44 | + locations = $("span.location") |
| 45 | + locationNames = [] |
| 46 | + numLocations = locations.length |
| 47 | + locations.each((index) -> |
| 48 | + $(this).children().each((index2) -> |
| 49 | + locationNames.push $(this).text() |
| 50 | + if locationNames.length == numLocations |
| 51 | + cb null, locationNames |
| 52 | + else |
| 53 | + # do nothing |
| 54 | + ) |
| 55 | + |
| 56 | + ) |
| 57 | + |
| 58 | +joinArs = (places, scores) -> |
| 59 | + joinedAr = [] |
| 60 | + i = 0 |
| 61 | + for place in places |
| 62 | + score = scores[i].join(" ") |
| 63 | + joinedAr.push("#{place} #{score}") |
| 64 | + i = i + 1 |
| 65 | + joinedAr.join("\n") |
| 66 | + |
| 67 | + |
| 68 | +module.exports = (robot)-> |
| 69 | + robot.respond /avy me/i, (msg) -> |
| 70 | + jsdom.env("http://varsom.no/Snoskred/", ['http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'], |
| 71 | + (errors, window) -> |
| 72 | + $ = window.$ |
| 73 | + |
| 74 | + getScores($, (err, scores) -> |
| 75 | + getPlaces($, (err, places) -> |
| 76 | + avyReport = joinArs(places, scores) |
| 77 | + msg.send avyReport |
| 78 | + ) |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
0 commit comments