Development of Twitter Application #7 - Search


Published on

This series of slides describes how to develop a twitter application.
This slide describes how to search tweets using Twitter Search RESTful Open API and how to implement it using Twitter4J.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Development of Twitter Application #7 - Search

  1. 1. Linked Data &Semantic WebTechnologyDevelopment ofTwitter ApplicationsPart 7. Search APIDr. Myungjin Lee
  2. 2. Linked Data & Semantic Web TechnologySearch API• Search REST API– find and return a collection of relevant Tweets basedon matching a specified query• Limitation– not complete index of all Tweets, but instead an indexof recent 6-9 days of Tweets– cannot find Tweets older than about a week.– limited due to complexity– not support authentication meaning all queries aremade anonymously.– focused in relevance and not completeness– limited to 1,000 characters in query length, includingany operators.2
  3. 3. Linked Data & Semantic Web TechnologySearch Operator3Example Finds tweets...twitter search containing both "twitter" and "search". This is the default operator"happy hour" containing the exact phrase "happy hour"love OR hate containing either "love" or "hate" (or both)beer -root containing "beer" but not "root"#haiku containing the hashtag "haiku"from:twitterapi sent from the user @twitterapito:twitterapi sent to the user @twitterapiplace:opentable:2 about the place with OpenTable ID 2place:247f43d441defc03 about the place with Twitter ID 247f43d441defc03@twitterapi mentioning @twitterapisuperhero since:2011-05-09 containing "superhero" and sent since date "2011-05-09"twitterapi until:2011-05-09 containing "twitterapi" and sent before the date "2011-05-09".movie -scary :) containing "movie", but not "scary", and with a positive attitude.flight :( containing "flight" and with a negative attitude.traffic ? containing "traffic" and asking a question.hilarious filter:links containing "hilarious" and with a source:tweet_button containing "news" and entered via the Tweet Button
  4. 4. Linked Data & Semantic Web TechnologyGET search/tweets• Resource URL–• Parameters• Other Information– Requests per rate limit window: 180/user, 450/app– Authentication: Required– Response Object: TweetsqrequiredA UTF-8, URL-encoded search query of 1,000 characters maximum, includingoperators. Queries may additionally be limited by complexity.geocodeoptionalReturns tweets by users located within a given radius of the given latitude/longitude.langoptionalRestricts tweets to the given language, given by an ISO 639-1 code. Languagedetection is best-effort.result_typeoptionalOptional. Specifies what type of search results you would prefer to receive. Thecurrent default is "mixed." Valid values include:* mixed: Include both popular and real time results in the response.* recent: return only the most recent results in the response* popular: return only the most popular results in the response.countoptionalThe number of tweets to return per page, up to a maximum of 100. Defaults to 15.untiloptionalReturns tweets generated before the given date. Date should be formatted asYYYY-MM-DD.since_idoptionalReturns results with an ID greater than (that is, more recent than) the specified ID.max_idoptionalReturns results with an ID less than (that is, older than) or equal to the specified ID.include_entitiesoptionalThe entities node will be disincluded when set to false.callbackoptionalIf supplied, the response will use the JSONP format with a callback of the givenname.4
  5. 5. Linked Data & Semantic Web TechnologyTwitter4J Classes for Search• SearchResource Interface– Methods• QueryResult search(Query query)• Query Class– A data class represents search query.– Constructor• Query()• Query(java.lang.String query)– Methods• void setCount(int count)• void setGeoCode(GeoLocation location, double radius,java.lang.String unit)• void setLang(java.lang.String lang)• void setLocale(java.lang.String locale)• void setMaxId(long maxId)• void setQuery(java.lang.String query)• void setResultType(java.lang.String resultType)– MIXED, POPULAR, RECENT• void setSince(java.lang.String since)• void setSinceId(long sinceId)• void setUntil(java.lang.String until)5
  6. 6. Linked Data & Semantic Web TechnologyTwitter4J Classes for Search• QueryResult Interface– A data interface representing search API response– Methods• int getCount()• long getMaxId()• java.lang.String getQuery()• long getSinceId()• java.util.List<Status> getTweets()• boolean hasNext()• Query nextQuery()6
  7. 7. Linked Data & Semantic Web TechnologySearching Tweets1. import java.util.List;2. import twitter4j.Query;3. import twitter4j.QueryResult;4. import twitter4j.Status;5. import twitter4j.Twitter;6. import twitter4j.TwitterException;7. import twitter4j.TwitterFactory;8. public class TwitterSearch {9. Twitter twitter = null;10. public TwitterSearch() {11. this.twitter = TwitterFactory.getSingleton();12. this.twitter.setOAuthConsumer(TwitterAccessToken.consumerKey,13. TwitterAccessToken.consumerSecret);14. this.twitter.setOAuthAccessToken(TwitterAccessToken.loadAccessToken());15. }16. public static void main(String args[]) throws TwitterException {17. TwitterSearch tt = new TwitterSearch();18. Query q = new Query();19. q.setQuery("GentleMan");20. q.setResultType(Query.RECENT);21. q.setCount(100);22. QueryResult qr =;23. List<Status> tweets = qr.getTweets();24. for (int i = 0; i < tweets.size(); i++) {25. Status tweet = tweets.get(i);26. System.out.println("Text: " + tweet.getText() + ", "27. + tweet.getCreatedAt() + ", " + tweet.getRetweetCount());28. }29. }30. }7