-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomate.java
More file actions
358 lines (317 loc) · 12.8 KB
/
automate.java
File metadata and controls
358 lines (317 loc) · 12.8 KB
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
354
355
356
357
358
package com.chargebee.tool;
//pending
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.ibm.json.java.OrderedJSONObject;
import org.json.JSONObject;
import org.json.simple.JSONArray;
import java.io.BufferedReader;
import java.io.FileReader;
import java.time.Month;
import java.util.*;
class TextParser {
static String year;
static String month;
static String day;
static String dates;
static String images;
static boolean status = false;
static Multimap<String, Multimap> bucket = ArrayListMultimap.create();
static org.json.JSONObject object = new JSONObject();
static JSONArray yearList = new JSONArray();
String title;
String tag;
String link;
String content;
String date;
String image;
public static void main(String[] args) throws Exception {
String inputFile = "/Users/cb-aravindh/Desktop/test/index.txt";
object.put("headline", "A timeline of feature releases");
object.put("head-copy", "Subscribe below to get periodic digests on new features and their business impacts.");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
//year and month object
List<String> list = new ArrayList<>();
String content = "";
while ((line = br.readLine()) != null) {
try {
if (line.contains("section")) {
if (content.length() != 0) {
list.add("CONTENT:" + removeTag(content.replace("[]", "").trim()));
content = "";
}
// list clean
if (list.size() != 0) {
execute(list);
list.clear();
images = "NOIMAGE";
status = false;
}
year = line.split(",")[1].split(" ")[2].replace("\"", "").replace("]", "");
month = getMonth(line.split(",")[1].split(" ")[1].replace("\"", "").replace("]", ""));
day = line.split(",")[0].split(" ")[2];
dates = getDate(day, Month.valueOf(month.toUpperCase()).getValue(), year);
} else if (line.contains("hero") || line.contains("relnote")) {
if (content.length() != 0) {
list.add("CONTENT:" + removeTag(content.replace("[]", "")));
content = "";
}
if (list.size() != 0) {
execute(list);
list.clear();
images = "NOIMAGE";
}
String title = line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'));
list.add("TITLE:" + title);
String tag = line.split(" ")[1].equals("Introducing") ? "new"
: line.split(" ")[1].equals("Improved") ? "NOTAG"
: line.split(" ")[1].equals("Beta") ? "beta"
: line.split(" ")[1].equals("new") ? "new" : "NOTAG";
list.add("TAG:" + tag);
try {
String link = line.substring(line.indexOf("http"), line.lastIndexOf("]"));
list.add("LINK:" + link);
} catch (Exception e) {
list.add("LINK:NOLINK");
}
status = true;
} else if (line.contains("image")) {
images = line.split(" ")[1].replace("]", "");
} else if (status) {
content += line;
}
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
System.exit(1);
}
}
if (content.length() != 0) {
list.add("CONTENT:" + removeTag(content.replace("[]", "")));
content = "";
}
if (list.size() != 0) {
execute(list);
list.clear();
}
check();
}
}
//TITLE
//TAG
//LINK
//IMAGE
//CONTENT
public static String removeTag(String content) {
while (content.indexOf('<') != -1) {
int x = content.indexOf('<');
int y = content.indexOf('>');
content = content.replace(content.substring(x, y + 1), "");
}
return content;
}
public static void execute(List list) {
Multimap<String, TextParser> monthBucket = ArrayListMultimap.create();
TextParser textParser = new TextParser();
for (int i = 0; i < list.size(); i++) {
String val = list.get(i).toString();
String first = val.substring(0, val.indexOf(':'));
String second = val.substring(val.indexOf(':') + 1, val.length());
switch (first) {
case "TITLE":
textParser.setTitle(second);
break;
case "TAG":
textParser.setTag(second);
break;
case "LINK":
textParser.setLink(second);
break;
case "CONTENT":
textParser.setContent(second);
break;
}
}
textParser.setDate(dates);
textParser.setImage(images);
monthBucket.put(month, textParser);
bucket.put(year, monthBucket);
}
public static String getMonth(String month) {
String monthVal = null;
if (month.equals("Jan") || month.equals("January")) {
monthVal = "January";
} else if (month.equals("Feb") || month.equals("February")) {
monthVal = "February";
} else if (month.equals("Mar") || month.equals("March")) {
monthVal = "March";
} else if (month.equals("Apr") || month.equals("April")) {
monthVal = "April";
} else if (month.equals("May")) {
monthVal = "May";
} else if (month.equals("Jun") || month.equals("June")) {
monthVal = "June";
} else if (month.equals("Jul") || month.equals("July")) {
monthVal = "July";
} else if (month.equals("Aug") || month.equals("August")) {
monthVal = "August";
} else if (month.equals("Sep") || month.equals("September")) {
monthVal = "September";
} else if (month.equals("Oct") || month.equals("October")) {
monthVal = "October";
} else if (month.equals("Nov") || month.equals("November")) {
monthVal = "November";
} else if (month.equals("Dec") || month.equals("December")) {
monthVal = "December";
}
return monthVal;
}
public static String getDate(String day, int month, String year) {
String dayVal = null;
if (day.equals("1")) {
dayVal = "02";
} else if (day.equals("2")) {
dayVal = "09";
} else if (day.equals("3")) {
dayVal = "16";
} else if (day.equals("4")) {
dayVal = "23";
} else {
dayVal = "29";
}
return dayVal + "-" + month + "-" + year;
}
public static void check() throws Exception {
String year[] = {"2020", "2019", "2018", "2017", "2016", "2015"};
String month[] = {"December", "November", "October", "September", "August", "July", "June", "May", "April", "March", "February", "January"};
JSONArray yl = new JSONArray();
for( String y :year) {
Collection<Multimap> years = bucket.get(y);
org.json.JSONObject yearobj = new org.json.JSONObject();
yearobj.put("year", y);
JSONArray monthList = new JSONArray();
for (String monthval : month) {
JSONArray arr = new JSONArray();
org.json.JSONObject monthobj = new org.json.JSONObject();
monthobj.put("month", monthval);
for (Multimap map : years) {
Collection<TextParser> dd = map.get(monthval);
for (TextParser list : dd) {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("feature", list.getTitle());
JSONArray tagList =new JSONArray();
if(!list.getTag().equals("NOTAG")){
tagList.add(list.getTag());
obj.put("tags",tagList);
}
try {
if (!list.getImage().equals("NOIMAGE")) {
obj.put("image", list.getImage());
}
}
catch (Exception e){}
if(!list.getLink().equals("NOLINK")){
obj.put("learn_more",list.getLink());
}
obj.put("desc", list.getContent());
obj.put("release_date",list.getDate());
arr.add(obj);
}
}
if (arr.size() != 0) {
monthobj.put("features", arr);
monthList.add(monthobj);
}
}
yearobj.put("months", monthList);
yl.add(yearobj);
}
object.put("years",yl);
System.out.println(object.toString());
// JSONArray yearLists = new JSONArray();
// for (String yearVal : year) {
// Collection<Multimap> yearList = bucket.get(yearVal);
// JSONObject yearObj = new JSONObject();
// yearObj.put("year",yearVal);
// for (Multimap monthobj : yearList) {
// JSONArray monthList = new JSONArray();
// for (String monthVal : month) {
// JSONObject monthObj = new JSONObject();
// monthObj.put("month",monthVal);
// Collection<TextParser> dd = monthobj.get(monthVal);
// System.out.println(dd.size());
// JSONArray featureList =new JSONArray();
// for (TextParser ff : dd) {
// JSONObject featureObj = new JSONObject();
// featureObj.put("feature",ff.getTitle());
// JSONArray tagList =new JSONArray();
// if(!ff.getTag().equals("NOTAG")){
// tagList.add(ff.getTag());
// featureObj.put("tag",tagList);
// }
// try {
// if (!ff.getImage().equals("NOIMAGE")) {
// featureObj.put("image", ff.getImage());
// }
// }
// catch (Exception e){}
// featureObj.put("desc",ff.getContent());
// if(!ff.getLink().equals("NOLINK")){
// featureObj.put("learn_more",ff.getLink());
// }
// featureObj.put("release_date",ff.getDate());
// featureList.add(featureObj);
// }
// if(featureList.size()!=0) {
// System.out.println(featureList.toJSONString());
// System.out.println("=============");
// monthObj.put("features", featureList);
// monthList.add(monthObj);
// }
// }
// if(monthList.size()!=0) {
// yearObj.put("months", monthList);
// yearLists.add(yearObj);
// }
// }
//
//
// }
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}