Users on Twitter can tweet, follow other users, create lists, view their home timeline, be mentioned by others, and have their profile looked up. The Twitter API provides methods to look up user profiles by ID or screen name, search for users, and get friends and follower IDs. The Twitter4J Java library implements these API methods and defines User and IDs classes to represent user data and lists of user IDs.
DevEX - reference for building teams, processes, and platforms
Development of Twitter Application #5 - Users
1. Linked Data &
Semantic Web
Technology
Development of
Twitter Applications
Part 5. Users
Dr. Myungjin Lee
2. Linked 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
2
followers: another
Twitter user who has
followed you
following (frineds): the
quantity of other Twitter users
you have chosen to follow
name: a name that can be
different 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 image
3. Linked Data & Semantic Web Technology
REST API related to Users
3
Resource Description
GET
users/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/...
GET
users/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.
GET
users/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.
4. Linked Data & Semantic Web Technology
Users Primary Field Guide
4
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 Entities
Entities 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 Int
The 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 String
The 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 String
Nullable. A URL provided by the user in association with their
profile.
5. Linked 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
element
– 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()
5
6. Linked 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_name
required
The screen name of the user for whom to return results for. Either a id
or screen_name is required for this method.
include_entities
optional
The entities node will be disincluded when set to false.
6
7. Linked Data & Semantic Web Technology
Getting the User
1. 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.consumerSecret);
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. }
7
8. Linked 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_name
optional
The screen name of the user for whom to return results for. Either a id
or screen_name is required for this method.
include_entities
optional
The entities node will be disincluded when set to false.
8
9. Linked Data & Semantic Web Technology
Getting Users
1. 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. }
9
10. Linked 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
q
required
The search query to run against people search.
page
optional
Specifies the page of results to retrieve.
count
optional
The number of potential user results to retrieve per page. This value
has a maximum of 20.
include_entities
optional
The entities node will be disincluded when set to false.
10
11. Linked Data & Semantic Web Technology
Searching Users
1. 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.getName());
7. }
8. }
11
12. Linked Data & Semantic Web Technology
REST API related to Friends & Followers
12
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/show
Returns detailed information about the relationship between two
arbitrary users.
13. Linked Data & Semantic Web Technology
Twitter4J Classes for Friends & Followers
• 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()
13
14. Linked 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_id
optional
The ID of the user for whom to return results for.
screen_name
optional
The screen name of the user for whom to return results for.
cursor
semi-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_ids
optional
Many programming environments will not consume our Tweet ids due to
their size. Provide this option to have ids returned as strings instead.
count
optional
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
environments where all 5,000 IDs constitutes too large of a response.
14
15. Linked Data & Semantic Web Technology
Getting Friends and Followers
1. 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. }
15