Created
April 10, 2016 12:08
-
-
Save ueki-kazuki/a4ca0db23a7cc60871b98ce58bd8df68 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'mechanize' | |
require 'nokogiri' | |
host = "https://forums.aws.amazon.com" | |
url = "#{host}/rss.jspa" | |
# open-uriだと302のリダイレクトが正しく処理できないためMechanizeを使います | |
mechanize = Mechanize.new | |
page = mechanize.get url | |
# AWSフォーラムはトップ・ミドル・フォーラムの3階層になっているようです | |
top_category="" | |
mid_category="" | |
# Machanizeで取得したHTMLをNokogiriに渡してパースします | |
doc = Nokogiri::HTML(page.body) | |
# jive-tableコンテナー内のテーブルの各行がサービスに対応しているようです | |
doc.xpath("//div[@class='jive-table']/table/tbody/tr").each do |node| | |
# 階層レベルとサービス名・フォーラム名は jive-first クラスの列内のテーブルにあります | |
# カテゴリの階層はテーブルの1列目でスペースが3つならトップ、6つならミドルです | |
indent_cell = node.xpath("td[@class='jive-first']/table/tbody/tr/td[1]") | |
title_cell = node.xpath("td[@class='jive-first']/table/tbody/tr/td[2]") | |
title = title_cell.text | |
if title =~ /^\s*Category\s*:\s*(.+)/ | |
if indent_cell.text.length == 3 | |
top_category = $1.strip | |
else | |
mid_category = $1.strip | |
end | |
end | |
# Forum以外のRSSは更新されないようなので無視します | |
next unless title =~ /^\s*Forum/ | |
# サービスのアナウンスは"Amazon Web Services"カテゴリで行われるようなので | |
# それ以外のトップカテゴリは無視します | |
next unless top_category == "Amazon Web Services" | |
# rssのリンクは jive-last クラスの列内のテーブルにあります | |
# AnnoucementsのRSSはテーブル1列目です | |
rsspath = node.xpath("td[@class='jive-last']/div/table/tbody/tr/td[1]/a").attribute("href") | |
# Forumという文字列と余分な空白を取り除きます | |
title.sub!("Forum:","") | |
title.strip! | |
next unless title.length > 0 | |
#puts "#{host}/#{rsspath} #{top_category}/#{mid_category}/#{title}" | |
puts "#{host}/#{rsspath} #{title}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment