Last active
September 13, 2016 08:43
-
-
Save yoshimov/7972cc84886df08caa16db593855a2b5 to your computer and use it in GitHub Desktop.
Notify tenki.jp weather to Slack using Google Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GAS Script for weather daemon on Slack | |
| // ScriptProperties: | |
| // - weatherUrl: Local weather web page URL on tenki.jp | |
| // - slackHookUrl: Slack web hook URL | |
| // - slackChannel: Channel name of Slack | |
| function rainRetrieve() { | |
| var url = PropertiesService.getScriptProperties().getProperty("weatherUrl"); | |
| var res = UrlFetchApp.fetch(url); | |
| var content = res.getContentText(); | |
| // get weather text | |
| var index = content.search(/"forecast_point_entry">/i); | |
| if (index < 0) { | |
| return; | |
| } | |
| var point = content.substring(index + 23); | |
| point = point.substring(0, point.search(/<\/tr>/)); | |
| var weather = point.substring(point.search(/fs_m3">/) + 7); | |
| weather = weather.substring(0, weather.search(/<\//)); | |
| var m = point.substring(point.search(/forecast_point_precip/)); | |
| m = m.substring(m.search(/">/) + 2); | |
| m = m.substring(0, m.search(/<\//)); | |
| m = parseInt(m, 10); | |
| var t = point.substring(point.search(/forecast_point_hour/)); | |
| t = t.substring(t.search(/">/) + 2); | |
| t = t.substring(0, t.search(/<\//)); | |
| var rain = false; | |
| var text = ""; | |
| if (m > 0 || weather.search(/雨/) >= 0) { | |
| rain = true; | |
| text = "<" + url + "|" + t + "> :umbrella:" + weather + " " + m + "mm"; | |
| } else { | |
| text = "<" + url + "|" + t + "> :sunny:" + weather; | |
| } | |
| Logger.log(text); | |
| var norain = PropertiesService.getScriptProperties().getProperty("norain"); | |
| if (rain) { | |
| PropertiesService.getScriptProperties().deleteProperty("norain"); | |
| var prem = PropertiesService.getScriptProperties().getProperty("rainm"); | |
| PropertiesService.getScriptProperties().setProperty("rainm", m); | |
| if (norain || (prem && parseInt(prem, 10) < m)) { | |
| sendNotify(text); | |
| } | |
| } else { | |
| PropertiesService.getScriptProperties().setProperty("norain", text); | |
| if (!norain) { | |
| sendNotify(text); | |
| } | |
| } | |
| } | |
| function sendNotify(text) { | |
| var channel = PropertiesService.getScriptProperties().getProperty("slackChannel"); | |
| var url = PropertiesService.getScriptProperties().getProperty("slackHookUrl"); | |
| var payload = {"channel": "#" + channel, "username": "weather daemon", "text": text}; | |
| var params = {"method": "POST", "payload": JSON.stringify(payload)}; | |
| var res = UrlFetchApp.fetch(url, params); | |
| if (res) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment