Created
March 4, 2023 04:20
-
-
Save takahashilabo/08b5c709c02387371bb4bfa07987f33e 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
require 'fileutils' | |
#使い方 | |
#デスクトップ等でターミナルから「ruby pdf_downloder.rb」と入力してください。 | |
#最初に大会Webページからhtmlをダウンロードし、html内を解析してリンクされたpdfファイルをダウンロードします(サーバ負荷で迷惑にならないようにsleepかけています)。 | |
#最終的に、1〜8.htmlとpdfディレクトリがrubyを実行したディレクトリに生成されます。それぞれのhtmlファイルを開いてご利用ください。 | |
#参加登録したらメールで送られてくる論文アクセス情報がありますので、それぞれuseridとpasswordに設定してください | |
base = 'https://onsite.gakkai-web.net/ipsj/onsite/html/program' | |
userid = '*****' | |
password = '*****' | |
#内部定数(いじる必要はないです) | |
org_dir = './org' | |
pdf_dir = './pdf' | |
pdf_relative_path = '../../data/pdf' | |
WGET_COMMAND = "wget -q --http-user=#{userid} --http-password=#{password}" | |
#HTMLファイル(program1〜8.html)をダウンロードする | |
files = [] | |
(1..8).each do |e| | |
files << "program#{e}.html" | |
end | |
Dir.mkdir(org_dir) if not File.exist?(org_dir) | |
files.each do |f| | |
`#{WGET_COMMAND} #{base}/#{f} -O #{org_dir}/#{f}` | |
puts "#{f} downloaded" | |
sleep 0.5 #Webサーバに迷惑をかけないように | |
end | |
#ダウンロードしたHMTLファイル内のAタグをローカル仕様に変更する | |
files.each do |f| | |
content = "" | |
File.read("#{org_dir}/#{f}").split("\n").each do |e| | |
begin | |
content += e.gsub(pdf_relative_path, pdf_dir) | |
content += "\n" | |
rescue | |
#no opration | |
end | |
end | |
File.write(f, content) | |
end | |
Dir.mkdir(pdf_dir) if not File.exist?(pdf_dir) | |
files.each do |f| | |
File.read(f).split("\n").each do |e| | |
begin | |
pdf_filename = e.split('pdf/')[1].split('"')[0] | |
`#{WGET_COMMAND} #{base}/#{pdf_relative_path}/#{pdf_filename} -O #{pdf_dir}/#{pdf_filename}` | |
puts "#{pdf_filename} downloaded" | |
sleep 1 #Webサーバに迷惑をかけないように | |
rescue | |
#no opration | |
end | |
end | |
end | |
FileUtils.rm_rf(org_dir) | |
puts "Completed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment