Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active September 17, 2019 15:59
Show Gist options
  • Save gofer/cf0ef5991881a1f8f8f5ade28c4c6095 to your computer and use it in GitHub Desktop.
Save gofer/cf0ef5991881a1f8f8f5ade28c4c6095 to your computer and use it in GitHub Desktop.
Yahoo!天気・災害の提供する各RSS情報のURLをいい感じにJSONにするやつ

How to use

bundle install --path=vendor/bundle

# If you need, download root server cert file
# curl -O http://curl.haxx.se/ca/cacert.pem

bundle exec ruby main.rb > yahoo_weather_rss_info.json
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'irb'
gem 'nokogiri'
require 'nokogiri'
require 'open-uri'
require 'json'
# If you need, set ssl cert file
# ENV['SSL_CERT_FILE']= File.expand_path('./cacert.pem')
html = Nokogiri::HTML(open('https://weather.yahoo.co.jp/weather/rss/'))
BaseURL = 'https://rss-weather.yahoo.co.jp/rss/days/'
pattern = Regexp.new(BaseURL.gsub(/\//, '\/') + '(\d+).xml')
puts JSON.dump(
html.xpath('//table[@class=\'yjw_table\']').map do |table|
{
:region_name => table.xpath('tr/td/h3')[0].text,
:prefectures => table.xpath('tr')[1..].map do |tr|
{
:prefecture_name => tr.xpath('td/h3')[0].text,
:prefecture_url => tr.xpath('td/h3')[0].xpath('a')[0]['href'],
:prefecture_id => tr.xpath('td/h3')[0].xpath('a')[0]['href'].gsub(pattern, '\1').to_i,
:prefecture_warn_url => tr.xpath('td/a')[0]['href'],
:locations => tr.xpath('td/ul/li').map do |x|
{
:location_name => x.text,
:location_url => x.xpath('a')[0]['href'],
:location_id => x.xpath('a')[0]['href'].gsub(pattern, '\1').to_i
}
end
}
end
}
end
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment