-
Notifications
You must be signed in to change notification settings - Fork 1
/
SutemaBurstStreamMain.java
353 lines (281 loc) · 12.8 KB
/
SutemaBurstStreamMain.java
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package jp.co.touyouhk;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import jp.co.touyouhk.nichannel.Util;
import jp.co.touyouhk.nichannel.subjecttext.SubjectTextEntity;
import jp.co.touyouhk.nichannel.subjecttext.SubjectTextUtil;
import jp.co.touyouhk.sutemaburststream.Constant;
import jp.co.touyouhk.sutemaburststream.dao.QueueDAO;
import jp.co.touyouhk.sutemaburststream.model.MailConf;
import jp.co.touyouhk.sutemaburststream.model.ZweiConf;
import jp.co.touyouhk.util.UniversalUtil;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/**
* ステマ バースト ストリーム
* SutemaBurstStream (SBS)
* Version 0.1
*
* @author touyouhk
*/
public class SutemaBurstStreamMain {
public static final String subjectTextDirectory = "subjectText";
//JSON
//http://gihyo.jp/dev/serial/01/engineer_toolbox/0028
//http://gihyo.jp/dev/serial/01/engineer_toolbox/0027?page=1
// ループする時間間隔(分単位)
public static int loopTime = 30;//デフォルト
// ループのカウンタ
private static int loopConter;
private static String sbsConfFileName = "sbs_conf.json";
/**
* @param args コマンドライン引数
* @throws ClassNotFoundException
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
* @throws EmailException
* @throws InterruptedException
*/
public static void main(String[] args) throws ClassNotFoundException, JsonParseException, JsonMappingException,
IOException, EmailException, InterruptedException {
Class.forName("org.sqlite.JDBC");
List<ZweiConf> zweiConfList;
// ■コマンドライン引数解釈
try {
if (!parseArgument(args)) {
//失敗ならアプリを終了
System.exit(-1);
}
} catch (Exception e) {
e.printStackTrace();
}
// ObjectMapperを作成
ObjectMapper mapper = new ObjectMapper();
// mydata.jsonからルートノードを取得
JsonNode rootNode = mapper.readValue(new File(args[0]), JsonNode.class);
zweiConfList = parseZweiConf(rootNode);
JsonNode sbsNode = mapper.readValue(new File(sbsConfFileName), JsonNode.class);
MailConf mailConf = parseSbsConf(sbsNode);
//System.exit(0);
String mailLine = "";
while (true) {
loopConter++;
System.out.println("------------ ループ " + loopConter + "回目");
for (ZweiConf zweiConf : zweiConfList) {
String result = execute(zweiConf);
if (result != "" && result != null) {
mailLine += result;
}
}
//Mail送信
if (mailLine != "" && mailLine.length() > 0) {
System.out.println("メールを送信します。");
try {
sendMail(mailConf, mailLine);
mailLine = "";
} catch (Exception e) {
// 一度だけリトライする
Thread.sleep(1000 * 60);
try {
sendMail(mailConf, mailLine);
mailLine = "";
} catch (Exception ee) {
System.out.println("Eメール送信エラー" + ee.toString());
}
}
} else {
System.out.println("メールを送信しません。");
}
// ■スリープ
endLoopWorkAndSleep();
}
}
public static String execute(ZweiConf zweiConf) throws UnsupportedEncodingException {
// TODO 毎回チェックするのは無駄なので処理をかえる
// ■subject.txt保存フォルダの生成
String saveFolder = saveFolderCreate(zweiConf.getUrl());
if (saveFolder == null) {
//失敗ならアプリを終了
System.exit(-1);
}
// ■subjectTextダウンロード
String subjectFileNameFullPath = SubjectTextUtil.download(zweiConf.getUrl(),
saveFolder);
if (subjectFileNameFullPath == null) {
System.out.println("subject.txtのダウンロードに失敗");
return null;
}
// ■subjectTextのモデルの作成
List<SubjectTextEntity> subjectTextEntities = SubjectTextUtil
.subjectText2Entity(subjectFileNameFullPath);
List<String> mailStrings = new ArrayList<>();
for (SubjectTextEntity subjectTextEntity : subjectTextEntities) {
String title = new String(subjectTextEntity.getTitle().getBytes("UTF-8"), "UTF-8");
zweiConf.getKeyWords().parallelStream().filter(s -> title.indexOf(s) != -1).forEach(s -> {
String result = register(zweiConf, subjectTextEntity, s);
if (result != null) {
mailStrings.add(result);
}
}
);
/*
for (String keyword : zweiConf.getKeyWords()) {
if (title.indexOf(keyword) != -1) {
String result = register(zweiConf, subjectTextEntity, keyword);
if (result != null){
mailStrings.add(result);
}
break;
}
}*/
}
return mailStrings.parallelStream().collect(Collectors.joining());
}
public static String register(ZweiConf zweiConf, SubjectTextEntity subjectTextEntity, String keyword) {
String mailString = null;
QueueDAO dao = new QueueDAO(Util.boardUrl2EnglishName(zweiConf.getUrl()));
try {
if (!dao.findById(Integer.valueOf(subjectTextEntity.getThreadNumber()))) {
dao.regist(Integer.valueOf(subjectTextEntity.getThreadNumber()),
subjectTextEntity.getTitle(), keyword);
mailString = "[" + zweiConf.getItaName() + "]" + " " + subjectTextEntity.getTitle() + " /key= " +
keyword + System.getProperty("line.separator") + "====================" +
System.getProperty("line.separator");
}
} catch (Exception e) {
// キーワードがスレ番号が異常なものに引っかかる場合(9から始まるものとか)
System.out.println(e);
} finally {
dao.closeHandler();
}
return mailString;
}
public static void sendMail(MailConf mailConf, String mailLine) throws EmailException {
Email email = new SimpleEmail();
email.setHostName(mailConf.getSmtp_server());
email.setSmtpPort(mailConf.getSmtp_port());
email.setAuthenticator(new DefaultAuthenticator(mailConf.getUser_name(), mailConf.getPassword()));
email.setSSLOnConnect(mailConf.isSmtp_ssl_flag());
email.setFrom(mailConf.getSrc_mail_adress());
email.setSubject(Constant.SOFTWARE_NAME + " " + System.currentTimeMillis());
email.setContent(mailLine, "text/plain; charset=ISO-2022-JP");
email.setCharset("ISO-2022-JP");
// try {
// email.setMsg(new String(mailLine.getBytes("iso-2022-jp")));
// } catch (UnsupportedEncodingException e) {
// // TODO 自動生成された catch ブロック
// e.printStackTrace();
// }
email.addTo(mailConf.getDest_mail_adress());
email.send();
}
/**
* メインクラスに渡された引数を解釈して変数に格納します。
* 引数が足りない場合false、それ以外true
*
* @param argument メインクラスに渡された引数
* @return 成功したらtrue、失敗したらfalse
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static boolean parseArgument(String[] argument) throws JsonParseException, JsonMappingException, IOException {
if (argument.length == 0) {
return false;
}
return true;
}
/**
* Subject.txtフォルダを生成します
* すでにフォルダが生成されている場合は処理をせずに成功とみなします。
*
* @return 成功したら、フォルダのフルパスを返却、失敗したらNULL
*/
public static String saveFolderCreate(String itaUrl) {
// ファイル区切り文字 (UNIX では「/」,Windowsでは「\」)
String fileSeparator = System.getProperties().getProperty(
"file.separator");
// カレントディレクトリの取得 eg: C:\work
String currentDirectory = System.getProperties().getProperty("user.dir");
//URLから板の英名抽出
String[] itaUrls = itaUrl.split("/");
String itaName = (itaUrls[itaUrls.length - 1]);
// subjectフォルダーのパス名の生成
// subjectTextフォルダの中に板名でフォルダを作成します
String createDirectoryPath = currentDirectory + fileSeparator
+ subjectTextDirectory + fileSeparator + itaName;
if (UniversalUtil.createDirectryExistCheak(createDirectoryPath)) {
return createDirectoryPath;
} else {
return null;
}
}
/**
* 1ループ終了時の共通的な終了処理と、スレッドスリープを呼び出します。
* スレッドスリープでの失敗は異常ルートなのでプロセスを終了させます。
*
* @return true
*/
public static boolean endLoopWorkAndSleep() {
// ■スリープ
System.out.println("スリープモードに移行します " + loopTime + "分");
try {
Thread.sleep(loopTime * 1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
// スレッドの割り込み例外・・異常ルート
System.exit(-1);
}
return true;
}
public static List<ZweiConf> parseZweiConf(JsonNode rootNode) {
List<ZweiConf> zweiConfList = new ArrayList<ZweiConf>();
Iterator<String> itaField = rootNode.getFieldNames();
while (itaField.hasNext()) {
ZweiConf zweiConf = new ZweiConf();
String itaNodeField = itaField.next();
zweiConf.setItaName(itaNodeField);
//System.out.println(" " + itaNodeField + ": " + rootNode.get(itaNodeField));
//JsonNode current = rootNode.get(itaNodeField);
JsonNode url = rootNode.get(itaNodeField).get("url");
zweiConf.setUrl(url.getTextValue());
JsonNode keywords = rootNode.get(itaNodeField).get("keywords");
System.out.println(url.getTextValue());
Iterator<JsonNode> keywordList = keywords.getElements();
List<String> keyWordArrayList = new ArrayList<String>();
while (keywordList.hasNext()) {
//System.out.println(keywordList.next().toString());
keyWordArrayList.add(keywordList.next().getTextValue());
}
zweiConf.setKeyWords(keyWordArrayList);
zweiConfList.add(zweiConf);
}
return zweiConfList;
}
public static MailConf parseSbsConf(JsonNode rootNode) {
JsonNode loop_node = rootNode.get("loop_minute");
loopTime = loop_node.asInt();
JsonNode mail_node = rootNode.get("mail");
MailConf mailConf = new MailConf();
mailConf.setSmtp_server(mail_node.get("smtp_server").getTextValue());
mailConf.setSmtp_port(mail_node.get("smtp_port").asInt());
mailConf.setSmtp_ssl_flag(mail_node.get("smtp_ssl_flag").asBoolean());
mailConf.setUser_name(mail_node.get("user_name").getTextValue());
mailConf.setPassword(mail_node.get("password").getTextValue());
mailConf.setSrc_mail_adress(mail_node.get("src_mail_adress").getTextValue());
mailConf.setDest_mail_adress(mail_node.get("dest_mail_adress").getTextValue());
return mailConf;
}
}