Upload
myungjin-lee
View
844
Download
1
Tags:
Embed Size (px)
DESCRIPTION
This series of slides describes how to develop a twitter application. This slide describes how to get information about Twitter users using Twitter4J.
Citation preview
Linked Data &Semantic WebTechnology
Development ofTwitter Applications
Part 5. Users
Dr. Myungjin Lee
2Linked Data & Semantic Web Technology
Users
• Users in Twitter– anyone or anything who tweets, follows, creates lists,
has a home_timeline, can be mentioned, and can be looked up in bulk
followers: another Twitter user who has
followed you
following (frineds): the quan-tity of other Twitter users you
have chosen to follow
name: a name that can be differ-ent from your username and is used to locate you on Twitter
scree name (user name): unique and fewer than 15 characters to identify you
on Twitter for replies and mentions
location description
url
profile im-age
3Linked Data & Semantic Web Technology
REST API related to Users
Resource Description
GETusers/lookup
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters. This method is especially useful when used in conjunction with collections of user IDs returned from GET friends/ids and GET followers/...
GETusers/show
Returns a variety of information about the user specified by the required user_id or screen_name parameter. The author's most recent Tweet will be returned inline when possible. GET users/lookup is used to retrieve a bulk collection of user objects.
GETusers/search
Provides a simple, relevance-based search interface to public user accounts on Twitter. Try querying by topical interest, full name, company name, location, or other criteria. Exact match searches are not supported. Only the first 1,000 matching results are available.
4Linked Data & Semantic Web Technology
Users Primary Field Guide
Field Type Description
created_at String The UTC datetime that the user account was created on Twitter
description String Nullable. The user-defined UTF-8 string describing their account.
entities EntitiesEntities which have been parsed out of the url or description fields defined by the user.
favourites_count Int The number of tweets this user has favorited in the account's lifetime.
followers_count Int The number of followers this account currently has.
friends_count IntThe number of users this account is following (AKA their "followings").
id Int64 The integer representation of the unique identifier for this User.
id_str String The string representation of the unique identifier for this Tweet.
lang String The BCP 47 code for the user's self-declared user interface language.
listed_count Int The number of public lists that this user is a member of.
location String Nullable. The user-defined location for this account's profile.
name String The name of the user, as they've defined it.
protected Boolean When true, indicates that this user has chosen to protect their Tweets.
screen_name StringThe screen name, handle, or alias that this user identifies themselves with.
status Tweets Nullable. If possible, the user's most recent tweet or retweet.
statuses_count Int The number of tweets (including retweets) issued by the user.
url StringNullable. A URL provided by the user in association with their profile.
5Linked Data & Semantic Web Technology
Twitter4J Classes for Users
• UsersResources Interface– Methods
• ResponseList<User> lookupUsers(long[] ids)• ResponseList<User> searchUsers(String query, int page)• User showUser(long userId)• User showUser(String screenName)
• User Interface– A data interface representing Basic user information ele-
ment– Methods
• Date getCreatedAt()• String getDescription()• int getFavouritesCount() • int getFollowersCount() • int getFriendsCount()• long getId()• String getLang()• int getListedCount()• String getLocation()• String getName()• String getProfileImageURL()• String getScreenName()• int getStatusesCount()• String getURL()
6Linked Data & Semantic Web Technology
GET users/show
• Resource URL– http://api.twitter.com/1.1/users/show.json
• Parameters
• Other Information– Requests per rate limit window: 180/user, 180/app– Authentication: Required– Response Object: Users– API Version: v1.1
user_id required
The ID of the user for whom to return results for. Either an id or screen_name is required for this method.
screen_namerequired
The screen name of the user for whom to return results for. Either a id or screen_name is required for this method.
include_entitiesoptional
The entities node will be disincluded when set to false.
7Linked Data & Semantic Web Technology
Getting the User1. import java.util.List;
2. import twitter4j.Twitter;
3. import twitter4j.TwitterException;
4. import twitter4j.TwitterFactory;
5. import twitter4j.User;
6. public class TwitterUser {
7. Twitter twitter = null;
8. public TwitterUser() {
9. this.twitter = TwitterFactory.getSingleton();
10.this.twitter.setOAuthConsumer(TwitterAccessToken.consumerKey,
11. TwitterAccessToken.consumer-Secret);
12.this.twitter.setOAuthAccessToken(TwitterAccessToken.loadAccessToken());
13. }
14. public static void main(String args[]) throws TwitterException {
15. TwitterUser tt = new TwitterUser ();
16. User user = tt.twitter.showUser("ACSpressroom");
17. System.out.println(user.getId());
18. System.out.println(user.getName());
19. System.out.println(user.getScreenName());
20. System.out.println(user.getDescription());
21. System.out.println(user.getLocation());
22. System.out.println(user.getURL());
23. System.out.println(user.getProfileBackgroundImageURL());
24. }
25. }
8Linked Data & Semantic Web Technology
GET users/lookup
• Resource URL– https://api.twitter.com/1.1/users/lookup.json
• Parameters
• Other Information– Requests per rate limit window: 180/user, 60/app– Authentication: Required– Response Object: Users– API Version: v1.1
user_id optional
The ID of the user for whom to return results for. Either an id or screen_name is required for this method.
screen_nameoptional
The screen name of the user for whom to return results for. Either a id or screen_name is required for this method.
include_entitiesoptional
The entities node will be disincluded when set to false.
9Linked Data & Semantic Web Technology
Getting Users1. public static void main(String args[]) throws TwitterException {
2. TwitterUser tt = new TwitterUser ();
3. String[] users = {"ACSpressroom", "AIP_Publishing", "PLoSNTDs"};
4. List<User> userList = tt.twitter.lookupUsers(users);
5. for(int i = 0; i < userList.size(); i++) {
6. User user = userList.get(i);
7. System.out.println(user.getId());
8. }
9. }
10Linked Data & Semantic Web Technology
GET users/search
• Resource URL– https://api.twitter.com/1.1/users/search.json
• Parameters
• Other Information– Requests per rate limit window: 180/user, 60/app– Authentication: Required– Response Object: Users– API Version: v1.1
qrequired
The search query to run against people search.
pageoptional
Specifies the page of results to retrieve.
countoptional
The number of potential user results to retrieve per page. This value has a maximum of 20.
include_entitiesoptional
The entities node will be disincluded when set to false.
11Linked Data & Semantic Web Technology
Searching Users1. public static void main(String args[]) throws TwitterException {
2. TwitterUser tt = new TwitterUser ();
3. List<User> userList = tt.twitter.searchUsers("journal", 1);
4. for(int i = 0; i < userList.size(); i++) {
5. User user = userList.get(i);
6. System.out.println(user.getId() + ", " + user.get-Name());
7. }
8. }
12Linked Data & Semantic Web Technology
REST API related to Friends & Followers
Resource Description
GET friends/ids
Returns a cursored collection of user IDs for every user the specified user is following (otherwise known as their "friends"). At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues....
GET followers/ids
Returns a cursored collection of user IDs for every user following the specified user. At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues. Results are given in groups of 5,000 user...
GET friendships/lookup
Returns the relationships of the authenticating user to the comma-separated list of up to 100 screen_names or user_ids provided. Values for connections can be: following, following_requested, followed_by, none.
GET friendships/showReturns detailed information about the relationship between two arbitrary users.
13Linked Data & Semantic Web Technology
Twitter4J Classes for Friends & Fol-lowers• FriendsFollowersResources Interface
– Methods• IDs getFollowersIDs(long cursor)• IDs getFollowersIDs(long userId, long cursor)• IDs getFollowersIDs(String screenName, long cursor)• IDs getFriendsIDs(long cursor) • IDs getFriendsIDs(long userId, long cursor) • IDs getFriendsIDs(String screenName, long cursor)
• IDs– A data interface representing array of numeric IDs.– Methods
• long[] getIDs()
14Linked Data & Semantic Web Technology
GET friends/ids & followers/ids
• Resource URL– https://api.twitter.com/1.1/friends/ids.json– https://api.twitter.com/1.1/followers/ids.json
• Parameters
• Other Information– Requests per rate limit window: 180/user, 60/app– Authentication: Required– Response Object: Users– API Version: v1.1
user_idoptional
The ID of the user for whom to return results for.
screen_nameoptional
The screen name of the user for whom to return results for.
cursorsemi-optional
Causes the list of connections to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor is provided, a value of -1 will be assumed, which is the first "page."
stringify_idsoptional
Many programming environments will not consume our Tweet ids due to their size. Provide this option to have ids returned as strings instead.
countoptional
Specifies the number of IDs attempt retrieval of, up to a maximum of 5,000 per distinct request. The value of count is best thought of as a limit to the number of results to return. When using the count parameter with this method, it is wise to use a consistent count value across all requests to the same user's collection. Usage of this parameter is encouraged in environ-ments where all 5,000 IDs constitutes too large of a response.
15Linked Data & Semantic Web Technology
Getting Friends and Followers1. public static void main(String args[]) throws TwitterException {
2. TwitterUser tt = new TwitterUser ();
3. IDs friends = tt.twitter.getFriendsIDs("ACSpressroom", -1);
4. System.out.println("friends:");
5. long[] ids = friends.getIDs();
6. for(int i = 0; i < ids.length; i++) {
7. System.out.println("\t" + ids[i]);
8. }
9. IDs followers = tt.twitter.getFollowersIDs("ACSpressroom", -1);
10. ids = followers.getIDs();
11. System.out.println("followers:");
12. for(int i = 0; i < ids.length; i++) {
13. System.out.println("\t" + ids[i]);
14. }
15. }