Newer
Older
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
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class SmartNews {
private static final ObjectMapper MAPPER = new ObjectMapper();
final private String category;
final private String originalTitle;
final private String plainTitle;
final private String stemTitle;
final private String plainDescription;
final private String stemDescription;
final private String url;
final private String publicationDate;
public SmartNews(String jsonString) throws JsonProcessingException {
JsonNode jsonMessage = MAPPER.readTree(jsonString);
this.category = jsonMessage.path("category").asText();
this.originalTitle = jsonMessage.path("originalTitle").asText();
this.plainTitle = jsonMessage.path("plainTitle").asText();
this.stemTitle = jsonMessage.path("stemTitle").asText();
this.plainDescription = jsonMessage.path("plainDescription").asText();
this.stemDescription = jsonMessage.path("stemDescription").asText();
this.url = jsonMessage.path("url").asText();
this.publicationDate = jsonMessage.path("publicationDate").asText();
}
public String getCategory() {
return category;
}
public String getOriginalTitle() {
return originalTitle;
}
public String getPlainTitle() {
return plainTitle;
}
public String getStemTitle() {
return stemTitle;
}
public String getPlainDescription() {
return plainDescription;
}
public String getStemDescription() {
return stemDescription;
}
public String getUrl() {
return url;
}
public String getPublicationDate() {
return publicationDate;
}
public long getEventTimeMillis() {
Date pubDate = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
pubDate = sdf.parse(this.getPublicationDate());
} catch (ParseException e) {
e.printStackTrace();
return new Date().toInstant().toEpochMilli();
}
return pubDate.toInstant().toEpochMilli();
}
public String getNewsDomain() {
String url = this.getRedirectUrl();
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
return "news.google.com";
}
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
public String getRedirectUrl() {
try {
URLConnection con = new URL(this.getUrl()).openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.connect();
InputStream is = con.getInputStream();
String redirectUrl = con.getURL().toString();
is.close();
return redirectUrl;
} catch (MalformedURLException e) {
//e.printStackTrace();
return this.getUrl();
} catch (IOException e) {
//e.printStackTrace();
try {
URLConnection con = new URL(this.getUrl()).openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.connect();
String redirectUrl = con.getURL().toString();
return redirectUrl;
} catch (MalformedURLException ex) {
//e.printStackTrace();
return this.getUrl();
} catch (IOException ex) {
//e.printStackTrace();
return this.getUrl();
}
}
}
public String toJsonString() {
ObjectNode smartNews = MAPPER.getNodeFactory().objectNode();
smartNews.put("category", this.getCategory());
smartNews.put("originalTitle", this.getOriginalTitle());
smartNews.put("plainTitle", this.getPlainTitle());
smartNews.put("stemTitle", this.getStemTitle());
smartNews.put("plainDescription", this.getPlainDescription());
smartNews.put("stemDescription", this.getStemDescription());
smartNews.put("url", this.getUrl());
smartNews.put("publicationDate", this.getPublicationDate());
return smartNews.toString();
}
}