Linked Data &
Semantic Web
Technology
Development of
Twitter Applications
Part 7. Search API
Dr. Myungjin Lee
Linked Data & Semantic Web Technology
Search API
• Search REST API
– find and return a collection of relevant Tweets based
on matching a specified query
• Limitation
– not complete index of all Tweets, but instead an index
of recent 6-9 days of Tweets
– cannot find Tweets older than about a week.
– limited due to complexity
– not support authentication meaning all queries are
made anonymously.
– focused in relevance and not completeness
– limited to 1,000 characters in query length, including
any operators.
2
Linked Data & Semantic Web Technology
Search Operator
3
Example 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 @twitterapi
to:twitterapi sent to the user @twitterapi
place:opentable:2 about the place with OpenTable ID 2
place:247f43d441defc03 about the place with Twitter ID 247f43d441defc03
@twitterapi mentioning @twitterapi
superhero 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 URL.
news source:tweet_button containing "news" and entered via the Tweet Button
Linked Data & Semantic Web Technology
GET search/tweets
• Resource URL
– https://api.twitter.com/1.1/search/tweets.json
• Parameters
• Other Information
– Requests per rate limit window: 180/user, 450/app
– Authentication: Required
– Response Object: Tweets
q
required
A UTF-8, URL-encoded search query of 1,000 characters maximum, including
operators. Queries may additionally be limited by complexity.
geocode
optional
Returns tweets by users located within a given radius of the given latitude/longitude.
lang
optional
Restricts tweets to the given language, given by an ISO 639-1 code. Language
detection is best-effort.
result_type
optional
Optional. Specifies what type of search results you would prefer to receive. The
current 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.
count
optional
The number of tweets to return per page, up to a maximum of 100. Defaults to 15.
until
optional
Returns tweets generated before the given date. Date should be formatted as
YYYY-MM-DD.
since_id
optional
Returns results with an ID greater than (that is, more recent than) the specified ID.
max_id
optional
Returns results with an ID less than (that is, older than) or equal to the specified ID.
include_entities
optional
The entities node will be disincluded when set to false.
callback
optional
If supplied, the response will use the JSONP format with a callback of the given
name.
4
Linked Data & Semantic Web Technology
Twitter4J 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
Linked Data & Semantic Web Technology
Twitter4J 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
Linked Data & Semantic Web Technology
Searching Tweets
1. 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 = tt.twitter.search(q);
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

Development of Twitter Application #7 - Search

  • 1.
    Linked Data & SemanticWeb Technology Development of Twitter Applications Part 7. Search API Dr. Myungjin Lee
  • 2.
    Linked Data &Semantic Web Technology Search API • Search REST API – find and return a collection of relevant Tweets based on matching a specified query • Limitation – not complete index of all Tweets, but instead an index of recent 6-9 days of Tweets – cannot find Tweets older than about a week. – limited due to complexity – not support authentication meaning all queries are made anonymously. – focused in relevance and not completeness – limited to 1,000 characters in query length, including any operators. 2
  • 3.
    Linked Data &Semantic Web Technology Search Operator 3 Example 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 @twitterapi to:twitterapi sent to the user @twitterapi place:opentable:2 about the place with OpenTable ID 2 place:247f43d441defc03 about the place with Twitter ID 247f43d441defc03 @twitterapi mentioning @twitterapi superhero 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 URL. news source:tweet_button containing "news" and entered via the Tweet Button
  • 4.
    Linked Data &Semantic Web Technology GET search/tweets • Resource URL – https://api.twitter.com/1.1/search/tweets.json • Parameters • Other Information – Requests per rate limit window: 180/user, 450/app – Authentication: Required – Response Object: Tweets q required A UTF-8, URL-encoded search query of 1,000 characters maximum, including operators. Queries may additionally be limited by complexity. geocode optional Returns tweets by users located within a given radius of the given latitude/longitude. lang optional Restricts tweets to the given language, given by an ISO 639-1 code. Language detection is best-effort. result_type optional Optional. Specifies what type of search results you would prefer to receive. The current 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. count optional The number of tweets to return per page, up to a maximum of 100. Defaults to 15. until optional Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD. since_id optional Returns results with an ID greater than (that is, more recent than) the specified ID. max_id optional Returns results with an ID less than (that is, older than) or equal to the specified ID. include_entities optional The entities node will be disincluded when set to false. callback optional If supplied, the response will use the JSONP format with a callback of the given name. 4
  • 5.
    Linked Data &Semantic Web Technology Twitter4J 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.
    Linked Data &Semantic Web Technology Twitter4J 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.
    Linked Data &Semantic Web Technology Searching Tweets 1. 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 = tt.twitter.search(q); 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