Development of Twitter Applications Part 4. Timeline
Download
Report
Transcript Development of Twitter Applications Part 4. Timeline
Linked Data &
Semantic Web
Technology
Development of
Twitter Applications
Part 7. Search API
Dr. Myungjin Lee
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
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
3
Linked Data & Semantic Web Technology
GET search/tweets
• Resource URL
– https://api.twitter.com/1.1/search/tweets.json
• Parameters
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.
• Other Information
– Requests per rate limit window: 180/user, 450/app
– Authentication: Required
– Response Object: Tweets
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.
3.
4.
5.
6.
7.
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
8.
public class TwitterSearch {
9.
Twitter twitter = null;
10.
11.
12.
13.
14.
15.
public TwitterSearch() {
this.twitter = TwitterFactory.getSingleton();
this.twitter.setOAuthConsumer(TwitterAccessToken.consumerKey,
TwitterAccessToken.consumerSecret);
this.twitter.setOAuthAccessToken(TwitterAccessToken.loadAccessToken());
}
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
public static void main(String args[]) throws TwitterException {
TwitterSearch tt = new TwitterSearch();
Query q = new Query();
q.setQuery("GentleMan");
q.setResultType(Query.RECENT);
q.setCount(100);
QueryResult qr = tt.twitter.search(q);
List<Status> tweets = qr.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Status tweet = tweets.get(i);
System.out.println("Text: " + tweet.getText() + ", "
+ tweet.getCreatedAt() + ", " + tweet.getRetweetCount());
}
}
30.
}
7
Linked Data & Semantic Web Technology