Twitter REST API and Streaming API

来源:互联网 发布:linux里sleep函数 编辑:程序博客网 时间:2024/05/21 15:06

Like my last blog, Twitter portlet of Liferay uses REST API. Twitter returns JSON Object, we just need to interpret it and store in our database. The bad thing for REST API is rate limit. In my previous blog, I mentioned that. For unauthenticated user, 150 per hour; authenticated user, 350 per hour. For a large website, it's not enough. The good news is we have another choice Streaming API. 

For my specific task, I just need to user streams. I also don't want to write from scratch because I don't know how to codeO(∩_∩)O~. Then I run into Twitter4jhttp://twitter4j.org/en/index.html. It has all APIs for REST API and Streaming API we want.  So actually we don't need to interpret JSON Object, we can ask Twitter4j do for us.

My task is: get real-time tweets and display in my own twitter portlet.

public class Test {public static void main(String[] args) {System.out.println("Start listening Twitter Streaming API....");TwitterStatusListener listener = context.getBean("twitterStatusListener",TwitterStatusListener.class);TwitterStream twitterStream = new TwitterStreamFactory().getInstance();twitterStream.addListener(listener);List<Feed> feeds = feedManager.getAllFeeds();//Here is to get Feeds which store twitterUserId.long[] followArray = new long[feeds.size()];for (int i = 0; i < feeds.size(); i++) {followArray[i] = feeds.get(i).getTwitterUserId();}twitterStream.filter(new FilterQuery(followArray)); //The FilterQuery just accepts long[] array of twitterUserids}}
If you want to do more coding, go to look at twitter4j-example. It's very useful and clear. You can find at least one example for every API.

Another issue I run into is if we call  twitterStream.filter(new FilterQuery(followArray)); once, it'll set up a new connection for us. Streaming API is used for a long-live connection, If we disconnect and reconnect frequently, our project will also hit rate limit. 发火抓狂快哭了