Such as I advanced in Twitter
Twitter API ME, such as Twitter API, is splitted into two main components: Search and REST
- Query
- QueryComposer
- SearchDevice
- Tweet
It is also possible to create a query with more than one parameter. In this case, you can concatenate two queries, by calling the method QueryComposer.append(Query, Query). It will return a new query object with both queries concatenated.
SearchDevice class is the entry point to Twitter Search API. This class provides the methods to submit a query to Twitter and retrieve the search's result. Its use is very straighforward: create a query and pass to SearchDevice.searchTweets(Query) method. The return of this method is an array of Tweet objects. Tweet class represents a tweet: a Twitter's message.
To retrieve the tweet's info (e.g. message, author, publish date, etc.), just use one of its "get" methods (e.g. Tweet.getString(String)), passing one of the attributes defined by MetadataSet interface. The Tweet's attributes are represented by the contants that start with TWEET prefix, e.g., MetadataSet.TWEET_CONTENT.
Now let's show some code. This example demonstrates how to search for all the tweets sent by a given user (e.g. me) and that contains the word "F1" (we will get a lot of results :-)):
...
SearchDevice sd = SearchDevice.getInstance();
Query q1 = QueryComposer.from("ernandesmjr");
Query q2 = QueryComposer.contain("F1");
Query q3 = QueryComposer.append(q1, q2);
Tweet[] result = sd.searchTweets(q3);
for (int i = 0; i < 0; i++) {
list.append(result[i].getString(MetadataSet.TWEET_CONTENT), null);
}
...
Let's search for tweets that references or mentions a certain person. In additional, we will request to Twitter Search API to return up to 5 tweets:
...
SearchDevice sd = SearchDevice.getInstance();
Query q1 = QueryComposer.reference("ernandesmjr");
Query q2 = QueryComposer.resultCount(5);
Query q3 = QueryComposer.append(q1, q2);
Tweet[] result = sd.searchTweets(q3);
for (int i = 0; i < 0; i++) {
System.out.println(result[i].getString(MetadataSet.TWEET_CONTENT));
System.out.println(result[i].getString(MetadataSet.TWEET_PUBLISH_DATE));
}
...
It is easy, isn't it? The rest, for while, is just a matter of discovering the others methods available in thoses classes. But the main use is what is described in these code snippets. More details you can check on javadoc.
See you in the nest post...
No comments:
Post a Comment