Skip to content

Instantly share code, notes, and snippets.

@masoo
Created Oct 16, 2019
Embed
What would you like to do?
# MIT License
# -----------
#
# Copyright (c) 2019 FUNABARA Masao
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
require 'mechanize'
agent = Mechanize.new
page = agent.get("https://www.jma.go.jp/jp/yoho/333.html")
prefecture = page.at(".titleText").children.text.delete("天気予報\n: ")
table = page.search('#forecasttablefont')
INDEX_1_AREA = 3
INDEX_1 = 5
today_weather = []
today_weather[0] = {}
today_weather[0][:area] = table&.children[INDEX_1_AREA]&.search(".th-area")&.children&.at("div")&.children&.first&.text
today_weather[0][:weather] = table&.children[INDEX_1]&.search(".weather")&.children&.search("img")&.attribute("alt")&.value
today_weather[0][:rainy_percent] = table&.children[INDEX_1]&.search("table.rain")&.search("td")&.map(&:children)&.map(&:text)
today_weather[0][:temp] = {min: table&.children[INDEX_1]&.search("table.temp")&.search("td.min")&.children&.text, max: table&.children[INDEX_1]&.search("table.temp")&.search("td.max")&.children&.text}
INDEX_2_AREA = 11
INDEX_2 = 13
today_weather[1] = {}
today_weather[1][:area] = table&.children[INDEX_2_AREA]&.search(".th-area")&.children&.at("div")&.children&.first&.text
today_weather[1][:weather] = table&.children[INDEX_2]&.search(".weather")&.children&.search("img")&.attribute("alt")&.value
today_weather[1][:rainy_percent] = table&.children[INDEX_2]&.search("table.rain")&.search("td")&.map(&:children)&.map(&:text)
today_weather[1][:temp] = {min: table&.children[INDEX_2]&.search("table.temp")&.search("td.min")&.children&.text, max: table&.children[INDEX_2]&.search("table.temp")&.search("td.max")&.children&.text}
result = "```\n"
result << "#{prefecture}\n"
today_weather.each do |v|
result << "───────────\n"
result << "#{v[:area]}\n"
result << "#{v[:weather]}\n"
result << "🗾降水確率\n"
v[:rainy_percent].each_slice(2) do |r1, r2|
result << "#{r1} #{r2}\n"
end
result << "🌡️気温\n"
result << "最低 #{v[:temp][:min]}\n"
result << "最高 #{v[:temp][:max]}\n"
end
result << "```"
puts result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment