Skip to content

Instantly share code, notes, and snippets.

@QB
Created March 1, 2013 23:17
Show Gist options
  • Save QB/5068724 to your computer and use it in GitHub Desktop.
Save QB/5068724 to your computer and use it in GitHub Desktop.
dat落ちした2chのスレのdatを拾ってきて保存。HTML化する機能も有り。●とか買う必要無いんで。
# encoding: utf-8
require "net/http"
# ■■ 設定ここから ■■
uri = "http://toro.2ch.net/test/read.cgi/news2/1353695121/" # みたいな感じでスレのURLを入れてね
html = true # HTML化してHTMLを保存するならtrue、datだけ拾ってくるならfalseを指定してね
file = true # 保存するときのファイル名を スレのidにするならtrue、スレタイにするならfalseを指定してね
# ファイル名にできない文字がスレタイに入ってるときはエラーが出るけど、諦めてね
# ■■ 設定ここまで ■■
p = %r{(.*?)/read.cgi/(.*?)/([0-9]*)/(.*)}
bord = uri.gsub(p, '\2')
id = uri.gsub(p, '\3')
uri.gsub!(%r{(http://)?[a-z]*.(2ch.net|bbspink.com)/}, 'http://mimizun.com/')
uri.gsub!(%r{test/read.cgi/(.*?)/([0-9]*)(.*)}, 'log/2ch/\1/\2.dat')
puts "requesting the dat file..."
doc = Net::HTTP.get(URI(uri))
doc.encode!("utf-8", "Shift_JIS")
title = doc.split("<>")[4].split("\n")[0] # 原始的
if !html
puts "writing data to the file..."
if !file
File.write(title + ".dat", doc)
else
File.write(bord + "_" + id + ".dat", doc)
end
puts "done!!"
exit(0)
end
# ここからHTML化の処理
puts "making HTML data..."
#名前とage,sageの変換
n = "\n"
clz = '</span>'
def n(color)
return '<span class="'+color+'">'
end
doc.gsub!(/^(.*?)<><>/, n("green") +'\1'+ clz + '[] ')
doc.gsub!(/^(.*?)<>(age|sage)<>/, n("blue") + '\1' + clz + '[\2] ')
doc.gsub!(/\n(.*?)<><>/, n + n("green") +'\1'+ clz + '[] ')
doc.gsub!(/\n(.*?)<>(age|sage)<>/, n + n("blue") + '\1' + clz + '[\2] ')
# 改行と区切りの変換
doc.gsub!("<br>", "<br />")
doc.gsub!("\n", "<hr />\n")
doc.gsub!("<><hr />\n", "</div><hr />\n")
# スレタイの抽出
p = /^(.*?)<>(.*?)<>(.*?)<hr \/>/
# p =~ doc
# title = $&.sub(p, '\3')
# datを取得した直後のあたりでもっと簡単に抽出したので、コメントアウトした
doc.sub!(p, '\1<>\2</div><hr />')
# 微妙なところ
doc.gsub!("<>\s", '<br /><div class="res">')
doc.gsub!(%r{<a (.*?)>&gt;&gt;([0-9]+)</a>}, '<a href="#r\2">&gt;&gt;\2</a>') #urlをリンク化する前に >>のリンクを調整する
doc.gsub!(%r{([\w]*)p://([\w\.\-/?%&=]*)}, '<a href="http://\2">\1p://\2</a>')
doc.gsub!("</b>", "<span class=\"kote\">")
doc.gsub!("<b>", "</span>")
# 番号振り
array = doc.split("\n")
for num in 1..array.length do
array[num-1] = '<a id="r' + num.to_s + '">'+ num.to_s + "</a>: " + array[num-1] + "\n"
end
doc = array.join
# ヘッダ、フッタの生成
footer = <<"EOS"
</body>
</html>
EOS
header = <<"EOS"
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>__</title>
<style>
*{ font-family: "--"; line-height: 1.1em; }
.green { color:green; font-weight:bold; }
.blue { color:blue; font-weight:bold; }
.res { margin:10px 0 0 30px; }
hr { margin: 20px 0; }
</style>
</head>
<body>
<h1>__</h1><hr />
EOS
header.gsub!("__", title)
header.gsub!("--", "MS\sPゴシック")
doc = header.encode!("utf-8") + doc + footer
# 当然htmlは真なので条件は省略
puts "writing data to the file..."
if !file
File.write(title + ".html", doc)
else
File.write(bord + "_" + id + ".html", doc)
end
puts "done!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment