Created
April 4, 2017 13:50
-
-
Save vivahiraj/e7b0a1dd87ab0b1d0e8bdfaef8f40621 to your computer and use it in GitHub Desktop.
LINE Messaging APIを利用して雨が降りそうなことを通知する
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
# coding: utf-8 | |
require 'rest-client' | |
require 'json' | |
require 'date' | |
require 'holiday_jp' | |
require 'gmail' | |
require 'rexml/document' | |
class MyMail | |
#GMail関連の設定 | |
ID = "GMail address" | |
PW = "GMail password" | |
TO = "mail send to address" | |
def self.send(sbj,msg) | |
gmail = Gmail.new(ID, PW) | |
message = | |
gmail.generate_message do | |
to TO | |
subject sbj | |
body msg | |
end | |
gmail.deliver(message) | |
gmail.logout | |
end | |
end | |
class LineBot | |
#LINE関連の設定 | |
TOKEN = "Channel Access Token" | |
TO = "送信先のID" | |
def self.send(msg) | |
headers = { | |
"Content-Type" => "application/json; charser=UTF-8", | |
"Authorization" => "Bearer #{TOKEN}", | |
} | |
params = { | |
to: TO, | |
messages: [ | |
{ | |
type: "text", | |
text: msg, | |
} | |
] | |
} | |
RestClient.post "https://api.line.me/v2/bot/message/push", params.to_json, headers | |
end | |
end | |
class RainChk | |
#降水確率を取得したい地域情報 | |
#以下を確認して設定する | |
#http://www.drk7.jp/weather/ | |
#以下は東京の例 | |
PREF = "http://www.drk7.jp/weather/xml/13.xml" | |
AREA = "東京地方" | |
def self.get_info(day,limit) | |
begin | |
response = RestClient.get PREF | |
rescue => e | |
p e.response | |
end | |
doc = REXML::Document.new response.to_str | |
day = day.strftime("%Y/%m/%d") | |
ret = {} | |
ret[:rain] = false | |
ret[:info] = [] | |
doc.elements.each("weatherforecast/pref/area[@id='#{AREA}']/info[@date='#{day}']/rainfallchance/period") do |element| | |
next if element.attributes["hour"] == "00-06" | |
ret[:info].push [element.attributes["hour"],element.text + "%"] | |
ret[:rain] = true if element.text.to_i >= limit | |
end | |
ret | |
end | |
end | |
if HolidayJp.holiday?(Date.today) | |
print "#{Time.now.to_s} holiday!\n" | |
exit | |
end | |
#確認したい日付と降水確率が何%以上で雨を降るとみなすかの閾値を渡す | |
ret = RainChk.get_info Date.today,50 | |
if ret[:rain] | |
msg = "雨が降るかもしれません。" | |
ret[:info].each do |a| | |
msg = msg + "\n #{a[0]} #{a[1]}" | |
end | |
begin | |
LineBot.send(msg) | |
rescue => e | |
print "#{Time.now.to_s} line bot error raise!\n" | |
MyMail.send "line bot error raise",e.response | |
exit | |
end | |
print "#{Time.now.to_s} rainy day!\n" | |
else | |
print "#{Time.now.to_s} sunny day.\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment