Skip to content

Commit

Permalink
TFJ-139 streaming API support beta
Browse files Browse the repository at this point in the history
git-svn-id: http://yusuke.homeip.net/svn/twitter4j/trunk@292 117b7e0d-5933-0410-9d29-ab41bb01d86b
  • Loading branch information
yusuke committed May 17, 2009
1 parent e61c6ac commit a09b1f7
Show file tree
Hide file tree
Showing 21 changed files with 1,296 additions and 420 deletions.
31 changes: 31 additions & 0 deletions src/main/java/twitter4j/ExtendedUser.java
Expand Up @@ -30,6 +30,8 @@
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import twitter4j.http.Response;
import twitter4j.org.json.JSONObject;
import twitter4j.org.json.JSONException;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -67,6 +69,35 @@ public ExtendedUser(Response res, Element elem, Twitter twitter) throws TwitterE
super(res, elem, twitter);
init(elem);
}

public ExtendedUser(JSONObject json) throws TwitterException {
super(json);
init(json);
}

private void init(JSONObject json) throws TwitterException {
try {
profileBackgroundColor = json.getString("profile_background_color");
profileTextColor = json.getString("profile_text_color");
profileLinkColor = json.getString("profile_link_color");
profileSidebarFillColor = json.getString("profile_sidebar_fill_color");
profileSidebarBorderColor = json.getString("profile_sidebar_border_color");
friendsCount = json.getInt("friends_count");
createdAt = parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
favouritesCount = json.getInt("favourites_count");
utcOffset = getInt("utc_offset", json);
timeZone = json.getString("time_zone");
profileBackgroundImageUrl = json.getString("profile_background_image_url");
profileBackgroundTile = json.getString("profile_background_tile");
following = getBoolean("following", json);
notificationEnabled = getBoolean("notifications", json);
statusesCount = json.getInt("statuses_count");
} catch (JSONException jsone) {
throw new TwitterException(jsone.getMessage() + json.toString(), jsone);
}

}

private void init(Element elem) throws TwitterException{
profileBackgroundColor = getChildText("profile_background_color", elem);
profileTextColor = getChildText("profile_text_color", elem);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/twitter4j/QueryResult.java
Expand Up @@ -53,7 +53,7 @@ public class QueryResult extends TwitterResponse {
private List<Tweet> tweets;
private static final long serialVersionUID = -9059136565234613286L;

/*package*/ QueryResult(Response res, Twitter twitter) throws TwitterException {
/*package*/ QueryResult(Response res, TwitterSupport twitterSupport) throws TwitterException {
super(res);
JSONObject json = res.asJSONObject();
try {
Expand All @@ -70,7 +70,7 @@ public class QueryResult extends TwitterResponse {
tweets = new ArrayList<Tweet>(array.length());
for (int i = 0; i < array.length(); i++) {
JSONObject tweet = array.getJSONObject(i);
tweets.add(new Tweet(tweet, twitter));
tweets.add(new Tweet(tweet, twitterSupport));
}
} catch (JSONException jsone) {
throw new TwitterException(jsone.getMessage());
Expand Down
54 changes: 33 additions & 21 deletions src/main/java/twitter4j/Status.java
Expand Up @@ -30,6 +30,8 @@
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;
Expand All @@ -40,27 +42,6 @@
* @author Yusuke Yamamoto - yusuke at mac.com
*/
public class Status extends TwitterResponse implements java.io.Serializable {
/*
<status>
created_at
id
text
source
truncated
in_reply_to_status_id
in_reply_to_user_id
favorited
<user>
id
name
screen_name
description
location
profile_image_url
url
protected
followers_count 
*/

private Date createdAt;
private long id;
Expand All @@ -70,6 +51,7 @@ public class Status extends TwitterResponse implements java.io.Serializable {
private long inReplyToStatusId;
private int inReplyToUserId;
private boolean isFavorited;
private String inReplyToScreenName;
private static final long serialVersionUID = 1608000492860584608L;

/*package*/Status(Response res, Twitter twitter) throws TwitterException {
Expand All @@ -84,6 +66,24 @@ public class Status extends TwitterResponse implements java.io.Serializable {
init(res, elem, twitter);
}

public Status(String str) throws TwitterException {
super();
try {
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 ExtendedUser(json.getJSONObject("user"));
} catch (JSONException jsone) {
throw new TwitterException(jsone.getMessage() + ":" + str, jsone);
}
}

private void init(Response res, Element elem, Twitter twitter) throws
TwitterException {
ensureRootNodeNameIs("status", elem);
Expand All @@ -97,6 +97,7 @@ private void init(Response res, Element elem, Twitter twitter) throws
inReplyToStatusId = getChildInt("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);
}

/**
Expand Down Expand Up @@ -169,6 +170,16 @@ 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;
}

/**
* Test if the status is favorited
*
Expand Down Expand Up @@ -246,4 +257,5 @@ public String toString() {
", user=" + user +
'}';
}

}
38 changes: 38 additions & 0 deletions src/main/java/twitter4j/StatusListener.java
@@ -0,0 +1,38 @@
/*
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;

/**
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.0.4
*/
public interface StatusListener {
void onStatus(Status status);

void onException(Exception ex);
}
85 changes: 85 additions & 0 deletions src/main/java/twitter4j/StatusStream.java
@@ -0,0 +1,85 @@
/*
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 twitter4j.http.Response;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
*
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.0.4
*/
public class StatusStream {
private boolean streamAlive = true;
private BufferedReader br;
private InputStream is;
private Response response;

/*package*/ StatusStream(InputStream stream) throws IOException {
this.is = stream;
this.br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
}
/*package*/ StatusStream(Response response) throws IOException {
this(response.asStream());
this.response = response;
}
public Status next() throws TwitterException{
if(!streamAlive){
throw new IllegalStateException("Stream already closed.");
}
try {
String line;
while (streamAlive) {
line = br.readLine();
if (null != line && line.length() > 0) {
return new Status(line);
}
}
throw new TwitterException("Stream closed.");
} catch (IOException e) {
try {
is.close();
} catch (IOException ignore) {
}
streamAlive = false;
throw new TwitterException("Stream closed.", e);
}

}
public void close() throws IOException{
is.close();
br.close();
if(null != response){
response.disconnect();
}
}
}

1 comment on commit a09b1f7

@bgoyanna
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging

Please sign in to comment.