Skip to content

Instantly share code, notes, and snippets.

@itkq
Last active September 7, 2016 09:50
Show Gist options
  • Save itkq/284d0df9dc1fbd2afc4f378032af453c to your computer and use it in GitHub Desktop.
Save itkq/284d0df9dc1fbd2afc4f378032af453c to your computer and use it in GitHub Desktop.
東工大修士向け2016年度3Q/4Qの文系選択を支援するやつ

これはなに

東工大修士向けの2016年3Q/4Qの文系選択を支援するもの.
OCWにアクセスしてシラバスを見るのが面倒な人向け.
各科目についてOCWから該当シラバスをローカルに保存し,HTMLへのリンクをまとめたインデックスページを生成する.
ローカルサーバを建ててブラウザから閲覧可能.

つかいかた

Cloneした後,教務Webシステムから,アンケート回答ページのHTMLを "questionnaire.htm"という名前で同ディレクトリに保存.

$ bundle install --path .bundle/
$ bunlde exec ruby bunkei.rb # WEBrickが起動

ブラウザで "http://localhost:8000/main.html" にアクセスする.

動作環境

Ruby 2.0 (>=)

うまく動かない人へ

すみません

require 'webrick'
require 'erb'
require 'mechanize'
$filename = 'questionnaire.htm'
$dir = "html"
$template = "main.html.template"
class Util
@@name_patterns = {
/^世界を知る:/ => "",
/(【\d+】|:.+$)/ => "",
}
@@place_patterns = {
"大岡山" => "oookayama",
"すずかけ台" => "suzukakedai",
/^遠隔.+$/ => "remote",
}
@@time_patterns = {
"月" => "Mon",
"火" => "Tue",
"水" => "Wed",
"木" => "Thu",
"金" => "Fri",
"土" => "Sat",
"日" => "Sun",
}
def self.parse filename
html = Nokogiri::HTML(File.read(filename))
html.css('.tblEnquete')[1].css('tr')[1..-1].map{|tr|
type = tr.css('td')[0].text.strip
season = tr.css('td')[2].text.strip
code = tr.css('td')[6].text.strip
query = name = tr.css('td')[4].text.strip
en_place = place = tr.css('td')[1].text.strip
en_time = time = tr.css('td')[5].text.strip
@@place_patterns.each{|k,v| en_place = en_place.gsub(k,v)}
@@name_patterns.each{|k,v| query = query.gsub(k,v)}
@@time_patterns.each{|k,v| en_time = en_time.gsub(k,v)}
{
type: type,
place: place,
season: season,
name: name,
time: time,
code: code,
query: query,
en_place: en_place,
en_time: en_time,
}
}
end
def self.make_main_html data
shaped = {}
keys = [:place, :type, :season, :time]
data.each do |d|
t = shaped
keys.each do |k|
unless t[d[k]]
t[d[k]] = k == :time ? [] : {}
end
t = t[d[k]]
end
t << d
end
content = "<ul>\n"
shaped.each do |place, p|
content += self.print_begining place
p.each do |type, y|
content += self.print_begining type
y.each do |season, s|
content += self.print_begining season
s.each do |time, t|
content += self.print_begining time
t.each do |d|
dir = "#{d[:en_place]}/#{d[:type]}/#{d[:season]}/#{d[:en_time]}/"
content += "<a href=./#{dir}#{d[:code]}.html>#{d[:name]}</a><br>\n"
end
content += print_ending
end
content += print_ending
end
content += print_ending
end
content += print_ending
end
content += "</ul>\n"
html = File.read($template)
erb = ERB.new(html)
File.write("#{$dir}/main.html", erb.result(binding))
end
private
def self.print_begining str
"<li>#{str}\n<ul>\n"
end
def self.print_ending
"</ul>\n</li>\n"
end
end
class Client
BASE = 'http://www.ocw.titech.ac.jp'
ENDPOINT = 'http://www.ocw.titech.ac.jp/index.php?module=General&action=Search&search_category=1&lang=JA'
def initialize
@agent = Mechanize.new
@agent.user_agent_alias = 'Mac Safari'
end
def save_lecture_info data
res = @agent.post(ENDPOINT, {query: data[:query]})
html = Nokogiri::HTML(res.body)
unless (trs = html.css('.searchResult > tbody > tr')).empty?
url = BASE + trs.first.css('a').attr('href').value
page = @agent.get(url)
dir = "#{$dir}/#{data[:en_place]}/#{data[:type]}/#{data[:season]}/#{data[:en_time]}/"
unless Dir.exists? dir
FileUtils.mkdir_p dir
end
page.save_as(dir + data[:code] + ".html")
else
false
end
end
end
clnt = Client.new
unless Dir.exists? $dir
Dir.mkdir $dir
data = Util.parse $filename
data.each do |d|
clnt.save_lecture_info d
end
Util.make_main_html data
end
WEBrick::HTTPServer.new({
DocumentRoot: "./#{$dir}",
BindAddress: '127.0.0.1',
Port: '8000',
}).start
# frozen_string_literal: true
# A sample Gemfile
source "https://rubygems.org"
gem "mechanize"
GEM
remote: https://rubygems.org/
specs:
domain_name (0.5.20160826)
unf (>= 0.0.5, < 1.0.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
mechanize (2.7.5)
domain_name (~> 0.5, >= 0.5.1)
http-cookie (~> 1.0)
mime-types (>= 1.17.2)
net-http-digest_auth (~> 1.1, >= 1.1.1)
net-http-persistent (~> 2.5, >= 2.5.2)
nokogiri (~> 1.6)
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (>= 0.0.9, < 0.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
net-http-digest_auth (1.4)
net-http-persistent (2.9.4)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
ntlm-http (0.1.1)
pkg-config (1.1.7)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
webrobots (0.1.2)
PLATFORMS
ruby
DEPENDENCIES
mechanize
BUNDLED WITH
1.12.0.rc
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<%= content %>
</div>
</body>
<html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment