Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for the latest Twitter API features #1

Merged
5 commits merged into from Sep 3, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/FilterQuery.java
Expand Up @@ -40,6 +40,8 @@ public final class FilterQuery implements java.io.Serializable {
private int[] follow;
private String[] track;
private double[][] locations;
private boolean includeEntities;

/**
* Creates a new FilterQuery
*/
Expand Down Expand Up @@ -132,6 +134,16 @@ public FilterQuery locations(double[][] locations){
return this;
}

/**
* Set whether to include extracted entities in the stream.
* @param include True if entities should be included, else false.
* @return this instance
*/
public FilterQuery setIncludeEntities(boolean include) {
includeEntities = include;
return this;
}

/*package*/ HttpParameter[] asHttpParameterArray(){
ArrayList<HttpParameter> params = new ArrayList<HttpParameter>();

Expand All @@ -148,6 +160,9 @@ public FilterQuery locations(double[][] locations){
params.add(new HttpParameter("locations"
, toLocationsString(locations)));
}
if (includeEntities) {
params.add(new HttpParameter("include_entities", true));
}

HttpParameter[] paramArray = new HttpParameter[params.size()];
return params.toArray(paramArray);
Expand Down
39 changes: 39 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/Status.java
Expand Up @@ -26,6 +26,7 @@
*/
package twitter4j;

import java.net.URL;
import java.util.Date;

/**
Expand Down Expand Up @@ -144,4 +145,42 @@ public interface Status extends Comparable<Status>, TwitterResponse, java.io.Ser
* @since Twitter4J 2.1.2
*/
String[] getContributors();

/**
* Returns the number of times this tweet has been retweeted, or -1 when the tweet was
* created before this feature was enabled.
*
* @return the retweet count.
*/
long getRetweetCount();

/**
* Returns true if the authenticating user has retweeted this tweet, or false when the tweet was
* created before this feature was enabled.
*
* @return whether the authenticating user has retweeted this tweet.
*/
boolean wasRetweetedByMe();

/**
* Returns an array of users mentioned in the tweet, or null if no users were mentioned.
* Note that these users only have data for ID, screen name, and name.
*
* @return An array of users mentioned in the tweet.
*/
User[] getUserMentions();

/**
* Returns an array of URLs mentioned in the tweet, or null if no URLs were mentioned.
*
* @return An array of URLs mentioned in the tweet.
*/
URL[] getURLs();

/**
* Returns an array of hashtags mentioned in the tweet, or null if no hashtags were mentioned.
*
* @return An array of users mentioned in the tweet.
*/
String[] getHashtags();
}
73 changes: 73 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/StatusJSONImpl.java
Expand Up @@ -31,6 +31,8 @@
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;

Expand All @@ -57,10 +59,16 @@
private String inReplyToScreenName;
private GeoLocation geoLocation = null;
private Place place = null;
private long retweetCount;
private boolean wasRetweetedByMe;

private String[] contributors;

private Status retweetedStatus;
private User[] userMentions;
private URL[] urls;
private String[] hashtags;

private static final long serialVersionUID = 1608000492860584608L;

/*package*/StatusJSONImpl(HttpResponse res) throws TwitterException {
Expand All @@ -83,6 +91,8 @@ private void init(JSONObject json) throws TwitterException {
inReplyToUserId = getInt("in_reply_to_user_id", json);
isFavorited = getBoolean("favorited", json);
inReplyToScreenName = getUnescapedString("in_reply_to_screen_name", json);
retweetCount = getLong("retweet_count", json);
wasRetweetedByMe = getBoolean("retweeted", json);
try {
if (!json.isNull("user")) {
user = new UserJSONImpl(json.getJSONObject("user"));
Expand Down Expand Up @@ -116,6 +126,34 @@ private void init(JSONObject json) throws TwitterException {
} else{
contributors = null;
}
if (!json.isNull("entities")) {
try {
JSONObject entities = json.getJSONObject("entities");

JSONArray userMentionsArray = entities.getJSONArray("user_mentions");
userMentions = new User[userMentionsArray.length()];
for(int i=0;i<userMentionsArray.length();i++){
userMentions[i] = new UserJSONImpl(userMentionsArray.getJSONObject(i));
}

JSONArray urlArray = entities.getJSONArray("urls");
urls = new URL[urlArray.length()];
for(int i=0;i<urlArray.length();i++){
try {
urls[i] = new URL(urlArray.getJSONObject(i).getString("url"));
} catch (MalformedURLException e) {
urls[i] = null;
}
}

JSONArray hashtagsArray = entities.getJSONArray("hashtags");
hashtags = new String[hashtagsArray.length()];
for(int i=0;i<hashtagsArray.length();i++){
hashtags[i] = hashtagsArray.getJSONObject(i).getString("text");
}
} catch (JSONException ignore) {
}
}
}

public int compareTo(Status that) {
Expand Down Expand Up @@ -237,6 +275,41 @@ public Status getRetweetedStatus() {
return retweetedStatus;
}

/**
* {@inheritDoc}
*/
public long getRetweetCount() {
return retweetCount;
}

/**
* {@inheritDoc}
*/
public boolean wasRetweetedByMe() {
return wasRetweetedByMe;
}

/**
* {@inheritDoc}
*/
public User[] getUserMentions() {
return userMentions;
}

/**
* {@inheritDoc}
*/
public URL[] getURLs() {
return urls;
}

/**
* {@inheritDoc}
*/
public String[] getHashtags() {
return hashtags;
}

/*package*/ static ResponseList<Status> createStatusList(HttpResponse res) throws TwitterException {
try {
JSONArray list = res.asJSONArray();
Expand Down
10 changes: 7 additions & 3 deletions twitter4j-core/src/main/java/twitter4j/Twitter.java
Expand Up @@ -321,7 +321,8 @@ public ResponseList<Status> getUserTimeline(String screenName, Paging paging)
return StatusJSONImpl.createStatusList(http.get(conf.getRestBaseURL()
+ "statuses/user_timeline.json",
mergeParameters(new HttpParameter[]{new HttpParameter("screen_name", screenName)
, new HttpParameter("include_rts", "true")}
, new HttpParameter("include_rts", "true")
, new HttpParameter("include_entities", "true")}
, paging.asPostParameterArray()), auth));
}

Expand All @@ -333,7 +334,8 @@ public ResponseList<Status> getUserTimeline(int userId, Paging paging)
return StatusJSONImpl.createStatusList(http.get(conf.getRestBaseURL()
+ "statuses/user_timeline.json",
mergeParameters(new HttpParameter[]{new HttpParameter("user_id", userId)
, new HttpParameter("include_rts", "true")}
, new HttpParameter("include_rts", "true")
, new HttpParameter("include_entities", "true")}
, paging.asPostParameterArray()), auth));
}

Expand Down Expand Up @@ -485,7 +487,9 @@ public IDs getRetweetedByIDs(long statusId, Paging paging) throws TwitterExcepti
* {@inheritDoc}
*/
public Status showStatus(long id) throws TwitterException {
return new StatusJSONImpl(http.get(conf.getRestBaseURL() + "statuses/show/" + id + ".json", auth));
return new StatusJSONImpl(http.get(conf.getRestBaseURL() + "statuses/show/" + id + ".json",
new HttpParameter[] { new HttpParameter("include_entities", "true") },
auth));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/User.java
Expand Up @@ -222,4 +222,20 @@ public interface User extends Comparable<User>, TwitterResponse, java.io.Seriali
boolean isVerified();

boolean isFollowing();

/**
* Returns the number of public lists the user is listed on, or -1
* if the count is unavailable.
*
* @return the number of public lists the user is listed on.
*/
int getListedCount();

/**
* Returns true if the authenticating user has requested to follow this user,
* otherwise false.
*
* @return true if the authenticating user has requested to follow this user.
*/
boolean isFollowRequestSent();
}
18 changes: 18 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/UserJSONImpl.java
Expand Up @@ -73,6 +73,8 @@
private boolean isGeoEnabled;
private boolean isVerified;
private boolean isFollowing;
private int listedCount;
private boolean isFollowRequestSent;
private static final long serialVersionUID = -6345893237975349030L;

/*package*/UserJSONImpl(HttpResponse res) throws TwitterException {
Expand Down Expand Up @@ -115,6 +117,8 @@ private void init(JSONObject json) throws TwitterException {
profileBackgroundTiled = getBoolean("profile_background_tile", json);
lang = getRawString("lang", json);
statusesCount = getInt("statuses_count", json);
listedCount = getInt("listed_count", json);
isFollowRequestSent = getBoolean("follow_request_sent", json);
if (!json.isNull("status")) {
JSONObject statusJSON = json.getJSONObject("status");
status = new StatusJSONImpl(statusJSON);
Expand Down Expand Up @@ -393,6 +397,20 @@ public boolean isFollowing() {
return isFollowing;
}

/**
* {@inheritDoc}
*/
public int getListedCount() {
return listedCount;
}

/**
* {@inheritDoc}
*/
public boolean isFollowRequestSent() {
return isFollowRequestSent;
}

/*package*/ static PagableResponseList<User> createPagableUserList(HttpResponse res) throws TwitterException {
try {
JSONObject json = res.asJSONObject();
Expand Down
20 changes: 20 additions & 0 deletions twitter4j-core/src/test/java/twitter4j/StreamAPITest.java
Expand Up @@ -166,6 +166,26 @@ public void testFilterTrackPush() throws Exception {
twitterStream.cleanup();
}

public void testFilterIncludesEntities() throws Exception {
this.ex = null;

FilterQuery query = new FilterQuery(0, null, new String[]{"http", "#", "@"});
query.setIncludeEntities(true);
twitterStream.filter(query);

boolean sawURL, sawMention, sawHashtag;
do {
waitForStatus();
sawURL = status.getURLs().length > 0;
sawMention = status.getUserMentions().length > 0;
sawHashtag = status.getHashtags().length > 0;
} while (!sawURL || !sawMention || !sawHashtag);

assertNull(ex);

twitterStream.cleanup();
}

public void onFriendList(int[] friendIds) {
System.out.println("onFriendList");
this.friendIds = friendIds;
Expand Down
25 changes: 24 additions & 1 deletion twitter4j-core/src/test/java/twitter4j/TwitterTestUnit.java
Expand Up @@ -27,6 +27,7 @@
package twitter4j;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
import static twitter4j.DAOTest.*;
Expand Down Expand Up @@ -124,6 +125,9 @@ public void testShowUser() throws Exception {
assertFalse(user.isStatusFavorited());
assertNull(user.getStatusInReplyToScreenName());

assertTrue(1 < user.getListedCount());
assertFalse(user.isFollowRequestSent());

//test case for TFJ-91 null pointer exception getting user detail on users with no statuses
//http://yusuke.homeip.net/jira/browse/TFJ-91
unauthenticated.showUser("twit4jnoupdate");
Expand Down Expand Up @@ -335,7 +339,9 @@ public void testShowStatus() throws Exception {
status2 = unauthenticated.showStatus(7185737372l);
assertEquals("\\u5e30%u5e30 <%}& foobar",status2.getText());


status = twitterAPI2.showStatus(1000l);
assertTrue(-1 <= status.getRetweetCount());
assertFalse(status.wasRetweetedByMe());
}

public void testStatusMethods() throws Exception {
Expand Down Expand Up @@ -881,4 +887,21 @@ public void testRetweetedBy() throws Exception {
assertTrue(ids.getIDs().length == 1);
assertEquals(ids.getIDs()[0], 5933482);
}

public void testEntities() throws Exception {
Status status = twitterAPI2.showStatus(22035985122L);
assertEquals(2, status.getUserMentions().length);
assertEquals(1, status.getURLs().length);

User user1 = status.getUserMentions()[0];
assertEquals(20263710, user1.getId());
assertEquals("rabois", user1.getScreenName());
assertEquals("Keith Rabois", user1.getName());
assertEquals(new URL("http://j.mp/cHv0VS"), status.getURLs()[0]);

status = twitterAPI2.showStatus(22043496385L);
assertEquals(2, status.getHashtags().length);
assertEquals("pilaf", status.getHashtags()[0]);
assertEquals("recipe", status.getHashtags()[1]);
}
}