Created
January 16, 2016 16:40
-
-
Save bamchoh/b8fd8dd996e833e6f676 to your computer and use it in GitHub Desktop.
This file contains 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
lass Time | |
def datetime | |
y = self.year | |
m = self.month | |
d = self.day | |
"%04d-%02d-%02d" % [y, m, d] | |
end | |
end | |
class Modifier | |
class << self | |
def modify(text) | |
text | |
.gsub(/ /, "") | |
.gsub(/後/, "からの") | |
.gsub(/一時/, "ちょっとま") | |
.gsub(/を伴う/, "がきよる") | |
.gsub(/時々/, "たま~に") | |
.gsub(/を伴い/, "と一緒に") | |
.gsub(/非常に/, "めっちゃ") | |
.gsub(/激しく/, "ぎょーさん") | |
.gsub(/山地/, "山のほう") | |
.gsub(/未明/, "夜おそーに") | |
.gsub(/では/, "らへんは") | |
.gsub(/所により/, "場所によっては") | |
.gsub(/海上/, "海の上") | |
.gsub(/夜遅く/, "夜遅くに") | |
.gsub(/晴れ/, "☀️、") | |
.gsub(/雨/, "☔️、") | |
.gsub(/雪/, "⛄️、") | |
.gsub(/くもり/, "☁️、") | |
.gsub(/雷/, "⚡️、") | |
end | |
end | |
end | |
def get_xml_link(api_url) | |
uri = URI.parse(URI.escape(api_url)) | |
http = SimpleHttp.new("http", uri.host, uri.port) | |
response = http.get(uri.path + "?" + uri.query) | |
latest_link = JSON.parse(response.body)["data"].max { |a, b| a["datetime"] <=> b["datetime"] } | |
return latest_link["link"] | |
end | |
def get_weather_report(xml_url) | |
weather_text = "" | |
uri = URI.parse(URI.escape(xml_url)) | |
http = SimpleHttp.new("http", uri.host, uri.port) | |
response = http.get(uri.path + (uri.query ? "?" + uri.query : "")) | |
xml_str = response.body | |
root = XmlParser.parse(xml_str) | |
sentence = root.find("WeatherForecastPart", {"refID"=>"1"}).find("Sentence").text | |
weather_text << Modifier.modify(sentence) | |
weather_text << "\n" | |
low_temp = "いっちゃん低い温度は " | |
high_temp = "逆にいっちゃん高い温度は " | |
meteo_infos = root.find('MeteorologicalInfos', {"type"=>"地点予報"}) | |
time_series_info = meteo_infos.find('TimeSeriesInfo') | |
time_series_info.find('TimeDefines').find_all('TimeDefine').each do |time_define| | |
node = time_define.find('Name') | |
case node.text | |
when "明日朝", "今日日中" | |
id = node.parent.attributes["timeId"] | |
temp_node = root.find("jmx_eb:Temperature", {"refID"=>"#{id}"}) | |
case temp_node.attributes["type"] | |
when "朝の最低気温" | |
__dir__ = File.dirname(__FILE__) | |
low_temp << File.read(File.join(__dir__, "lower_temp.txt")).chomp << " " << temp_node.attributes["unit"] | |
File.open(File.join(__dir__ , "lower_temp.txt"), "w") do |f| | |
f.write(temp_node.text) | |
end | |
when "日中の最高気温" | |
high_temp << temp_node.text << " " << temp_node.attributes["unit"] << "やで" | |
end | |
end | |
end | |
weather_text << low_temp | |
weather_text << "\n" | |
weather_text << high_temp | |
weather_text << "\n" | |
weather_text | |
end | |
def get_weather_text | |
now_time = Time.now | |
now_time = Time.gm(2016, 1, 16, 7, 0, 0) | |
datetime = now_time.datetime | |
api_url = "http://api.aitc.jp/jmardb-api/search?title=府県天気予報&areacode_mete=270000&datetime=#{datetime} 00:00:00&datetime=#{datetime} 07:00:00" | |
xml_url = get_xml_link(api_url) | |
get_weather_report(xml_url) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment