-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm.rb
145 lines (130 loc) · 3.07 KB
/
gm.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
$DIR = File.expand_path(File.dirname(__FILE__)) + '/'
require 'rubygems'
require 'gmail'
require 'base64'
require 'json'
require 'pp'
require 'iconv'
require $DIR + 'msql.rb' #if run by crontab, use full path
require $DIR + 'mhq.rb' #if run by crontab, use full path
gmail_username = ''
gmail_password = ''
mongo_ini('', '')
$json_opt_path = $DIR + 'mailer.log'
def convert_month(month)
$m = month
case $m
when 'Jan'
return '1'
when 'Feb'
return '2'
when 'Mar'
return '3'
when 'Apr'
return '4'
when 'May'
return '5'
when 'Jun'
return '6'
when 'Jul'
return '7'
when 'Aug'
return '8'
when 'Sep'
return '9'
when 'Oct'
return '10'
when 'Nov'
return '11'
when 'Dec'
return '12'
else
return 0
end
end
def make_list(s, cont_que)
date = nil
s.scan(/(作者:\s+(.*)\s+\(.*\).*\s*標題:\s+(.*\S)\s+時間:\s+\w+\s+(\w+)\s+(\d+).*(\d\d\d\d))\s*((.|\n)*)/){
|head, author, title, month, day, year, full_article|
month = convert_month(month)
date = "#{year}-#{month}-#{day}"
cont_que.push("head"=>head, "author"=>author, "title"=>title, "date"=>date, "full_article"=>full_article, "createTime"=>$createTime) #, "AID"=>aid
}
puts "\n article_info parse done\n"
pp cont_que
end
def day_add_zero(data)
puts data.size
end
def big5_2_utf8(data)
begin
ic = Iconv.new("utf-8//IGNORE","big5")
return ic.iconv(data)
rescue
return 'big5_2_utf8 error'
puts "\n big5_2_utf8() error\n"
end
end
def clean_ansi_color(s)
s.gsub!(/\[\d+;\d*m|\[m|\[\d*m|\[\w+m/, '')
end
def clean_utf8_space(s)
s.to_s.gsub!(/\\u001b/, '')
end
def add_br(s)
s.gsub!(/\\n/, "\/n")
end
def now_time()
time = Time.new
return time.strftime("%Y-%m-%d %H:%M:%S")
end
def today()
time = Time.new
return time.strftime("%Y-%m-%d")
end
def log(log, file_name="index.html")
File.open("#{file_name}","w+") do |f| f.puts log end
end
def dump_json(arr)
#begin
json = JSON.generate(arr)
pp arr
res = clean_utf8_space(add_br(json))
#puts JSON.pretty_generate(arr)
log(res, $json_opt_path)
return JSON.parse(res)
#rescue
# puts "\n nothing to dump! \n"
#end
end
$createTime = now_time()
Gmail.connect(gmail_username, gmail_password) do |gmail|
cont_que = []
cont = []
if gmail.logged_in?
puts "login! \n"
end
puts "work date is #{today()}"
gmail.inbox.emails(:unread, :before => Date.parse(today()), :from => "*.bbs@ptt.cc").each do |email|
#puts email.message.to_s
cte = email.message.to_s.scan(/Content-Transfer-Encoding:\s*(.*)/).to_s
if cte == "quoted-printable\r"
match = email.message.to_s.scan(/(=A7@=AA=CC:(.|\n)*)/)
c = match.to_s.unpack "M"
pp c
cont = big5_2_utf8(clean_ansi_color(c.to_s))
make_list(cont, cont_que)
else
match = email.message.to_s.scan(/(p0CqzDog(.|\n)*)|[a-zA-Z0-9+]{30,}/) #p0CqzDog "作者"(big5) encoding of base64 at article header
if match.size == 0
match = email.message.to_s.scan(/LS0tLS0t(.|\n)*/) #gmail 轉寄 gmail
end
cont = big5_2_utf8(clean_ansi_color(Base64.decode64(match.to_s)))
make_list(cont, cont_que)
end
#email.unread!
end
arr = dump_json(cont_que)
mongo_insert_data(arr)
insert_data(arr)
end