kaihou999の日記: さらに勝手に改造
日記 by
kaihou999
昨日のアレ。コメントも取れるようにしてみました。
[16:38 追記] キャッシュが既にある人は、必ずクリアしてから取得してください。
----------------------------------------------------------------------------------
#!/usr/local/bin/ruby -Ke
require 'net/http'
=begin
http://slashdot.jp の特定ユーザの日記を取得するクラス。コメントがあったらコメントも取得する
list = GetDiarySlashdotJapan.new
list.getList(uid)
=end
class GetDiarySlashdotJapan
SERVER = 'slashdot.jp'
JOURNAL_START = /<!-- start template: ID (\d+), journallist;journal;default -->/
JOURNAL_PATTERN = /<TD VALIGN=.*?><A HREF="(.*?)"><B>.*?<\/B><\/A><\/TD>/
JOURNAL_COMMENT_PATTERN = /(\d+)\xB8\xC4\xA4\xCE<A HREF="http:\/\/slashdot.jp(\/comments.pl\?sid=\d+)">/
def initialize(limit = 3)
@errorLimit = limit
end
def setErrorLimit(limit)
@errorLimit = limit
end
# HTMLを取得
def getHTML(path)
# 初期化
s = ""
Net::HTTP.version_1_2
while true
errCount = 0
begin
Net::HTTP.start(SERVER, 80) do |http|
res , = http.get(path)
return res.body
end
rescue => evar
p evar
errCount += 1
if @errorLimit <= errCount
raise "取得失敗"
end
end
end
end
# 日記を取得
def getJournal(url, name)
html = getHTML(url)
# ファイルに保存
/&id=(\d+)/ =~ url
id = $1
name = id + '.html'
file = open(name, "w")
file.write(html)
file.close
#日記コメントの取得
if JOURNAL_COMMENT_PATTERN =~ html
if $1.to_i() > 1
puts "http:\/\/slashdot.jp#{$2}"
html = getHTML("#{$2}&threshold=-1&mode=flat&commentsort=0&op=%CA%D1%B9%B9")
file = open(id + 'COMMENT.html', "w")
file.write(html)
file.close
end
end
end
# 日記のリストを取得
def getList(uid)
# html取得
html = getHTML("/journal.pl?op=list&uid=#{uid}").gsub(/\s+/m, ' ')
# リストの手前まで移動
JOURNAL_START =~ html
html = $'
if html == nil
print "ごめん。このユーザはまだ日記を書いてないようです。\n"
exit
end
# リストから日記を取得
html.scan(JOURNAL_PATTERN) do |s|
url = s.shift.gsub(/&\s*amp\;/, '&')
# ファイルが存在していなければ取得
/&id=(\d+)/ =~ url
name = $1 + '.html'
unless File.exist? name
print url, "\n"
getJournal(url, name)
end
end
end
end
#================================ main =================================
if __FILE__ == $0
if ARGV.empty?
print "Usage: #{$0} [user ID]\n"
exit
end
uid = ARGV.shift
# uidのディレクトリを作成
Dir.mkdir(uid) unless FileTest.exist?(uid)
Dir.chdir(uid)
# 日記取得
list = GetDiarySlashdotJapan.new
list.getList(uid)
Dir.chdir('..')
end
[16:38 追記] キャッシュが既にある人は、必ずクリアしてから取得してください。
----------------------------------------------------------------------------------
#!/usr/local/bin/ruby -Ke
require 'net/http'
=begin
http://slashdot.jp の特定ユーザの日記を取得するクラス。コメントがあったらコメントも取得する
list = GetDiarySlashdotJapan.new
list.getList(uid)
=end
class GetDiarySlashdotJapan
SERVER = 'slashdot.jp'
JOURNAL_START = /<!-- start template: ID (\d+), journallist;journal;default -->/
JOURNAL_PATTERN = /<TD VALIGN=.*?><A HREF="(.*?)"><B>.*?<\/B><\/A><\/TD>/
JOURNAL_COMMENT_PATTERN = /(\d+)\xB8\xC4\xA4\xCE<A HREF="http:\/\/slashdot.jp(\/comments.pl\?sid=\d+)">/
def initialize(limit = 3)
@errorLimit = limit
end
def setErrorLimit(limit)
@errorLimit = limit
end
# HTMLを取得
def getHTML(path)
# 初期化
s = ""
Net::HTTP.version_1_2
while true
errCount = 0
begin
Net::HTTP.start(SERVER, 80) do |http|
res , = http.get(path)
return res.body
end
rescue => evar
p evar
errCount += 1
if @errorLimit <= errCount
raise "取得失敗"
end
end
end
end
# 日記を取得
def getJournal(url, name)
html = getHTML(url)
# ファイルに保存
/&id=(\d+)/ =~ url
id = $1
name = id + '.html'
file = open(name, "w")
file.write(html)
file.close
#日記コメントの取得
if JOURNAL_COMMENT_PATTERN =~ html
if $1.to_i() > 1
puts "http:\/\/slashdot.jp#{$2}"
html = getHTML("#{$2}&threshold=-1&mode=flat&commentsort=0&op=%CA%D1%B9%B9")
file = open(id + 'COMMENT.html', "w")
file.write(html)
file.close
end
end
end
# 日記のリストを取得
def getList(uid)
# html取得
html = getHTML("/journal.pl?op=list&uid=#{uid}").gsub(/\s+/m, ' ')
# リストの手前まで移動
JOURNAL_START =~ html
html = $'
if html == nil
print "ごめん。このユーザはまだ日記を書いてないようです。\n"
exit
end
# リストから日記を取得
html.scan(JOURNAL_PATTERN) do |s|
url = s.shift.gsub(/&\s*amp\;/, '&')
# ファイルが存在していなければ取得
/&id=(\d+)/ =~ url
name = $1 + '.html'
unless File.exist? name
print url, "\n"
getJournal(url, name)
end
end
end
end
#================================ main =================================
if __FILE__ == $0
if ARGV.empty?
print "Usage: #{$0} [user ID]\n"
exit
end
uid = ARGV.shift
# uidのディレクトリを作成
Dir.mkdir(uid) unless FileTest.exist?(uid)
Dir.chdir(uid)
# 日記取得
list = GetDiarySlashdotJapan.new
list.getList(uid)
Dir.chdir('..')
end
さらに勝手に改造 More ログイン