forked from Twitter4J/Twitter4J
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Status.java
311 lines (277 loc) · 9.36 KB
/
Status.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
/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import twitter4j.http.Response;
import twitter4j.org.json.JSONObject;
import twitter4j.org.json.JSONException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* A data class representing one single status of a user.
* @author Yusuke Yamamoto - yusuke at mac.com
*/
public class Status extends TwitterResponse implements java.io.Serializable {
private Date createdAt;
private long id;
private String text;
private String source;
private boolean isTruncated;
private long inReplyToStatusId;
private int inReplyToUserId;
private boolean isFavorited;
private String inReplyToScreenName;
private double latitude = -1;
private double longitude = -1;
private RetweetDetails retweetDetails;
private static final long serialVersionUID = 1608000492860584608L;
/*package*/Status(Response res, Twitter twitter) throws TwitterException {
super(res);
Element elem = res.asDocument().getDocumentElement();
init(res, elem, twitter);
}
/*package*/Status(Response res, Element elem, Twitter twitter) throws
TwitterException {
super(res);
init(res, elem, twitter);
}
public Status(String str) throws TwitterException, JSONException {
// StatusStream uses this constructor
super();
JSONObject json = new JSONObject(str);
id = json.getLong("id");
text = json.getString("text");
source = json.getString("source");
createdAt = parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
inReplyToStatusId = getLong("in_reply_to_status_id", json);
inReplyToUserId = getInt("in_reply_to_user_id", json);
isFavorited = getBoolean("favorited", json);
user = new User(json.getJSONObject("user"));
}
private void init(Response res, Element elem, Twitter twitter) throws
TwitterException {
ensureRootNodeNameIs("status", elem);
user = new User(res, (Element) elem.getElementsByTagName("user").item(0)
, twitter);
id = getChildLong("id", elem);
text = getChildText("text", elem);
source = getChildText("source", elem);
createdAt = getChildDate("created_at", elem);
isTruncated = getChildBoolean("truncated", elem);
inReplyToStatusId = getChildLong("in_reply_to_status_id", elem);
inReplyToUserId = getChildInt("in_reply_to_user_id", elem);
isFavorited = getChildBoolean("favorited", elem);
inReplyToScreenName = getChildText("in_reply_to_screen_name", elem);
NodeList georssPoint = elem.getElementsByTagName("georss:point");
if(0 == georssPoint.getLength()){
// quick workaround for http://groups.google.com/group/twitter-development-talk/browse_thread/thread/2fb55a05ddead3cf
georssPoint = elem.getElementsByTagName("georss:Point");
}
if(1 == georssPoint.getLength()){
String[] point = georssPoint.item(0).getFirstChild().getNodeValue().split(" ");
latitude = Double.parseDouble(point[0]);
longitude = Double.parseDouble(point[1]);
}
NodeList retweetDetailsNode = elem.getElementsByTagName("retweet_details");
if(1 == retweetDetailsNode.getLength()){
retweetDetails = new RetweetDetails(res,(Element)retweetDetailsNode.item(0),twitter);
}
}
/**
* Return the created_at
*
* @return created_at
* @since Twitter4J 1.1.0
*/
public Date getCreatedAt() {
return this.createdAt;
}
/**
* Returns the id of the status
*
* @return the id
*/
public long getId() {
return this.id;
}
/**
* Returns the text of the status
*
* @return the text
*/
public String getText() {
return this.text;
}
/**
* Returns the source
*
* @return the source
* @since Twitter4J 1.0.4
*/
public String getSource() {
return this.source;
}
/**
* Test if the status is truncated
*
* @return true if truncated
* @since Twitter4J 1.0.4
*/
public boolean isTruncated() {
return isTruncated;
}
/**
* Returns the in_reply_tostatus_id
*
* @return the in_reply_tostatus_id
* @since Twitter4J 1.0.4
*/
public long getInReplyToStatusId() {
return inReplyToStatusId;
}
/**
* Returns the in_reply_user_id
*
* @return the in_reply_tostatus_id
* @since Twitter4J 1.0.4
*/
public int getInReplyToUserId() {
return inReplyToUserId;
}
/**
* Returns the in_reply_to_screen_name
*
* @return the in_in_reply_to_screen_name
* @since Twitter4J 2.0.4
*/
public String getInReplyToScreenName() {
return inReplyToScreenName;
}
/**
* returns The location's latitude that this tweet refers to.
* @since Twitter4J 2.0.10
*/
public double getLatitude(){
return latitude;
}
/**
* returns The location's longitude that this tweet refers to.
* @since Twitter4J 2.0.10
*/
public double getLongitude(){
return longitude;
}
/**
* Test if the status is favorited
*
* @return true if favorited
* @since Twitter4J 1.0.4
*/
public boolean isFavorited() {
return isFavorited;
}
private User user = null;
/**
* Return the user
*
* @return the user
*/
public User getUser() {
return user;
}
/**
*
* @since Twitter4J 2.0.10
*/
public boolean isRetweet(){
return null != retweetDetails;
}
/**
*
* @since Twitter4J 2.0.10
*/
public RetweetDetails getRetweetDetails() {
return retweetDetails;
}
/*package*/
static List<Status> constructStatuses(Response res,
Twitter twitter) throws TwitterException {
Document doc = res.asDocument();
if (isRootNodeNilClasses(doc)) {
return new ArrayList<Status>(0);
} else {
try {
ensureRootNodeNameIs("statuses", doc);
NodeList list = doc.getDocumentElement().getElementsByTagName(
"status");
int size = list.getLength();
List<Status> statuses = new ArrayList<Status>(size);
for (int i = 0; i < size; i++) {
Element status = (Element) list.item(i);
statuses.add(new Status(res, status, twitter));
}
return statuses;
} catch (TwitterException te) {
ensureRootNodeNameIs("nil-classes", doc);
return new ArrayList<Status>(0);
}
}
}
@Override
public int hashCode() {
return (int) id;
}
@Override
public boolean equals(Object obj) {
if (null == obj) {
return false;
}
if (this == obj) {
return true;
}
return obj instanceof Status && ((Status) obj).id == this.id;
}
@Override
public String toString() {
return "Status{" +
"createdAt=" + createdAt +
", id=" + id +
", text='" + text + '\'' +
", source='" + source + '\'' +
", isTruncated=" + isTruncated +
", inReplyToStatusId=" + inReplyToStatusId +
", inReplyToUserId=" + inReplyToUserId +
", isFavorited=" + isFavorited +
", inReplyToScreenName='" + inReplyToScreenName + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", retweetDetails=" + retweetDetails +
", user=" + user +
'}';
}
}