-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickptt.rb
266 lines (239 loc) · 5.97 KB
/
quickptt.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
class Ptt
require 'rubygems' unless defined?(Gem)
require 'json'
require 'net/telnet'
require 'pp'
require 'iconv'
AnsiSetDisplayAttr = '\x1B\[(?>(?>(?>\d+;)*\d+)?)m'
WaitForInput = '(?>\s+)(?>\x08+)'
AnsiEraseEOL = '\x1B\[K'
AnsiCursorHome = '\x1B\[(?>(?>\d+;\d+)?)H'
PressAnyKey = '\xAB\xF6\xA5\xF4\xB7\x4E\xC1\xE4\xC4\x7E\xC4\xF2'
Big5Code = '[\xA1-\xF9][\x40-\xF0]'
PressAnyKeyToContinue = "#{PressAnyKey}(?>\\s*)#{AnsiSetDisplayAttr}(?>(?:\\xA2\\x65)+)\s*#{AnsiSetDisplayAttr}"
PressAnyKeyToContinue2 = "\\[#{PressAnyKey}\\](?>\\s*)#{AnsiSetDisplayAttr}"
# (b)進板畫面
ArticleList = '\(b\)' + "#{AnsiSetDisplayAttr}" + '\xB6\x69\xAA\x4F\xB5\x65\xAD\xB1\s*' + "#{AnsiSetDisplayAttr}#{AnsiCursorHome}"
Signature = '\xC3\xB1\xA6\x57\xC0\xC9\.(?>\d+).+' + "#{AnsiCursorHome}"
$host = 'ptt.cc'
ARGV[2] ||= "Gossiping"
$board_name = ARGV[2]
$json_opt_path = "/var/www/quick-ptt/#{ARGV[2]}.html"
$pre_result = ''
$iconv_fail = 0
def connect(port, time_out, wait_time, host)
tn = Net::Telnet.new(
'Host' => host,
'Port' => port,
'Timeout' => time_out,
'Waittime' => wait_time
)
return tn
end
$check_relogin
def login(tn, id, password)
tn.waitfor(/guest.+new(?>[^:]+):(?>\s*)#{AnsiSetDisplayAttr}#{WaitForInput}\Z/){ |s| print(s) }
# 帳號
tn.cmd("String" => id, "Match" => /\xB1\x4B\xBD\x58:(?>\s*)\Z/){ |s| print(s) }
# 密碼, 按任意鍵繼續
tn.cmd("String" => password,
"Match" => /#{PressAnyKeyToContinue}\Z/){ |s|
print(s)
if $check_relogin == 'yes'
kick_self_off(tn)
end
}
tn.print("\n")
end
def kick_self_off(tn)
if tn.waitfor(/.*\xAA\x60\xB7\x4E\:.*/){ |s| print(s) }
tn.print('Y')
tn.print("\n")
tn.print("\n")
jump_board(tn, $board_name)
keep_check_board(tn)
end
end
#進入某板(等於從主畫面按's')
def jump_board(tn, board_name)
puts "\n jump_board() \n"
# [呼叫器]
tn.waitfor(/(\[\xA9\x49\xA5\x73\xBE\xB9\])#{AnsiSetDisplayAttr}.+#{AnsiCursorHome}\Z/){ |s| } #print(s)
tn.print('s')
#tn.waitfor(/\):(?>\s*)#{AnsiSetDisplayAttr}(?>\s*)#{AnsiSetDisplayAttr}#{AnsiEraseEOL}#{AnsiCursorHome}\Z/){ |s| } #print(s)
lines = tn.cmd( "String" => board_name, "Match" => /(?>#{PressAnyKeyToContinue}|#{ArticleList})\Z/ ) do |s|
print(s)
end
# 按任意鍵繼續
if not (/#{PressAnyKeyToContinue}\Z/ =~ lines)
return lines
end
lines = tn.cmd("String" => "", "Match" => /#{ArticleList}\Z/) do |s|
#print(s)
end
return lines
end
def gsub_ansi_by_space(s)
begin
s.gsub!(/\x1B\[(?:(?>(?>(?>\d+;)*\d+)?)m|(?>(?>\d+;\d+)?)H|K)/) do |m|
if m[m.size-1].chr == 'K'
"\n"
else
" "
end
end
rescue
puts "\n----gsub_ansi_by_space erro: ---\n #{s} \n"
end
end
def get_article_list(s)
list = []
begin
s.scan(/
# 文章ID
(?>\s*)(\d*)
# 推文狀態
\s+(\+|.|\~|X|x|S|s|\x580)
# 推文數量
\s*[^\xA1\xB9](?>\s*)(\xC3\x7A|\+\xC3\x7A|s\xC3\x7A|S\xC3\x7A|\s*\d*|' ')
# 日期
(?>\s*)(?>\s*)(\d+\/\d+)
# 帳號
(?>\s*)(?!(?>\d+\s))(\w{2,})\s+
# 文章標記
(?>\s*)(\xA1\xBC|R:|\xC2\xE0)
# 分類
(?>\s*)(\[\S*[^\xA4\xBD\xA7\x69]\S*\])\s+
# 主題
(?>\s*)(.*)
/x){
|articleID, pushStatus, pushCount, date, author, mark, type, title|
title = mine_checker(title)
fullLIst = articleID + ' ' + pushStatus + ' ' + pushCount + ' ' + date + ' ' + author + ' ' + mark + ' ' + type + ' ' + title
list.push(
"fullLIst"=> big5_2_utf8(fullLIst),
"articleID"=> articleID,
"pushStatus"=> big5_2_utf8(pushStatus),
"pushCount"=> big5_2_utf8(pushCount),
"date"=> date,
"author"=> big5_2_utf8(author),
"mark"=> big5_2_utf8(mark),
"type"=> big5_2_utf8(type),
"title"=> big5_2_utf8(title),
"createTime" => now_time
)
}
rescue
puts 'get_article_list error'
ensure
return list
end
end
def bottom(tn)
tn.print("\e[4~")
end
def big5_2_utf8(data) #@!!! Iconv::InvalidCharacter
begin
ic = Iconv.new("utf-8//IGNORE","big5")
data = ic.iconv(data.to_s)
$iconv_fail = 0
rescue
puts "\n iconv error #{data}\n"
return "iconv error"
$iconv_fail = 1
ensure
return data
end
end
def mine_checker(data)
return data.delete("\\\\").to_s.gsub(/"/, "'").gsub(/\xA1\xB9.*/,'')
end
def now_time()
time = Time.new
return now_time = time.strftime("%Y-%m-%d %H:%M:%S")
end
def dump_json(arr)
json = JSON.generate(arr)
#pp arr
#puts JSON.pretty_generate(arr)
puts "\n--------------------\nCount: #{arr.count} at #{now_time()} \n--------------------\n"
if arr.count >= 15 && arr.count <= 20 && $iconv_fail == 0
$pre_result = json
log(json, $json_opt_path)
else
log($pre_result, $json_opt_path)
end
end
def log(log, file_name="index.html")
File.open("#{file_name}","w+") do |f| f.puts log end
end
def line_me(s)
s.gsub!(/[^\w*]\d{5,}[^\w*]/) do |m|
"\n#{m}"
end
end
def keep_check_board(tn)
while (1)
begin
sleep(1)
tn.print("b")
sleep(1)
tn.print("\n")
sleep(1)
result = tn.waitfor(/(?>#{PressAnyKeyToContinue}|#{ArticleList})\Z/){ |s| } #print(s)
result = gsub_ansi_by_space(result)
result = line_me(result)
#puts result
arr = get_article_list(result)
dump_json(arr)
bottom(tn)
rescue
puts "keep check board faild"
crawer_retry_mode()
end
end
end
def crawer_ini()
begin
$check_relogin = 'no'
tn = connect(23, 10, 1, $host)
login(tn, ARGV[0], ARGV[1])
result = jump_board(tn, $board_name)
result = gsub_ansi_by_space(result)
result = line_me(result)
puts result
arr = get_article_list(result)
dump_json(arr)
keep_check_board(tn)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
crawer_retry_mode()
end
end
def crawer_retry_mode()
begin
$check_relogin = 'yes'
tn = connect(23, 10, 1, $host)
login(tn, ARGV[0], ARGV[1])
result = jump_board(tn, $board_name)
result = gsub_ansi_by_space(result)
result = line_me(result)
puts result
arr = get_article_list(result)
dump_json(arr)
keep_check_board(tn)
rescue Exception => e
sleep(3)
puts e.message
puts e.backtrace.inspect
retry
end
end
def hasInput()
if !ARGV[0] || !ARGV[1] then
print("8gua.rb ID PASSWORD\n")
exit
end
end
end #endcalss