cho45 (owner)

Revisions

gist: 203155 Download_button fork
public
Public Clone URL: git://gist.github.com/203155.git
Embed All Files: show embed
2ch.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'uri'
require 'net/http'
require 'nkf'
require 'stringio'
require 'zlib'
Net::HTTP.version_1_2
 
class ThreadData
attr_accessor :uri
 
attr_accessor :last_modified, :size
 
Line = Struct.new(:n, :name, :mail, :misc, :body, :opts, :id)
 
def initialize(thread_uri)
@uri = URI(thread_uri)
_, _, _, @board, @num, = *@uri.path.split('/')
@dat = []
end
 
def subject
self[0].opts
end
 
def [](n)
l = @dat[n - 1]
name, mail, misc, body, opts = * l.split(/<>/)
id = misc[/ID:([^\s]+)/, 1]
 
body.gsub!(/<br>/, "\n")
body.gsub!(/<[^>]+>/, "")
body.gsub!(/^\s+|\s+$/, "")
body.gsub!(/&(gt|lt|amp);/) {|s|
{ 'gt' => ">", 'lt' => "<", 'amp' => "&" }[$1]
}
 
Line.new(n, name, mail, misc, body, opts, id)
end
 
def retrieve(force=false)
@dat = [] if @force
 
res = Net::HTTP.start(@uri.host, @uri.port) do |http|
req = Net::HTTP::Get.new('/%s/dat/%d.dat' % [@board, @num])
req['User-Agent'] = 'Monazilla/1.00 (2ig.rb/0.0e)'
req['Accept-Encoding'] = 'gzip' unless @size
unless force
req['If-Modified-Since'] = @last_modified if @last_modified
req['Range'] = "bytes=%d-" % @size if @size
end
 
http.request(req)
end
 
ret = nil
case res.code.to_i
when 200, 206
body = res.body
if res['Content-Encoding'] == 'gzip'
body = StringIO.open(body, 'rb') {|io| Zlib::GzipReader.new(io).read }
end
 
@last_modified = res['Last-Modified']
if res.code == '206'
@size += body.size
else
@size = body.size
end
 
body = NKF.nkf('-w', body)
 
curr = @dat.size + 1
@dat.concat(body.split(/\n/))
last = @dat.size
 
(curr..last).map {|n|
self[n]
}
when 416 # たぶん削除が発生
p ['416']
retrieve(true)
[]
when 304 # Not modified
[]
when 302 # dat 落ち
p ['302', res['Location']]
[]
else
p ['Unknown Status:', res.code]
[]
end
end
end
 
 
def encode_aa(aa)
uri = URI('http://tinyurl.com/api-create.php')
uri.query = 'url=' + URI.escape(<<-EOS.gsub(/[\n\t]/, ''))
data:text/html,<pre style='font-family:"IPA モナー Pゴシック"'>#{aa.gsub(/\n/, '<br>')}</pre>
EOS
ret = Net::HTTP.get(uri.host, uri.request_uri, uri.port)
puts ret
exit
end
 
def is_aa(body)
significants = body.scan(/[>\n0-9a-z0-9A-Za-zA-Zぁ-んァ-ン一-龠]/u).size.to_f
body_length = body.scan(/./u).size
is_aa = 1 - significants / body_length
 
(body[/\n/] && is_aa > 0.6)
end
 
dat = ThreadData.new('http://changi.2ch.net/test/read.cgi/ogame3/1254831474/')
loop do
dat.retrieve.each do |line|
puts "%d: %s\n%s\n\n" % [line.n, line.id, line.body]
end
sleep 5
end