Example usage for twitter4j User getId

List of usage examples for twitter4j User getId

Introduction

In this page you can find the example usage for twitter4j User getId.

Prototype

long getId();

Source Link

Document

Returns the id of the user

Usage

From source file:com.aremaitch.codestock2010.library.TwitterLib.java

License:Apache License

public HashMap<String, Long> getUserIDsFromScreenNames(List<String> screenNames) throws TwitterException {
    HashMap<String, Long> result = new HashMap<String, Long>();

    //  Talk about awkward syntax:
    ResponseList<User> users = t.lookupUsers(screenNames.toArray(new String[screenNames.size()]));
    if (users.size() > 0) {
        for (User u : users) {
            result.put(u.getScreenName(), u.getId());
        }//w w w. j  a v  a2  s.co m
    }
    return result;
}

From source file:com.avishkar.NewGetFollowersIDs.java

License:Apache License

private static List<Long> getUserFollwers(long twitterUserId)
        throws UnknownHostException, TwitterException, InterruptedException {
    Twitter twitter = new TwitterFactory(AccessTokenUtil.getConfig()).getInstance();
    List<Long> filteredUserListOnFollowerCount = new ArrayList<Long>();
    final Gson gson = new Gson();
    try {//from w  w w  .j a v a 2s.  c  o m

        checkRateLimit("/followers/ids", twitter);
        System.out.println("Listing followers's Follower for ID:" + twitterUserId + System.lineSeparator());
        IDs followerIDs = twitter.getFollowersIDs(twitterUserId, -1);
        JsonObject followerJSON = new JsonObject();

        followerJSON.addProperty("id", twitterUserId);
        followerJSON.addProperty("followers", gson.toJson(followerIDs.getIDs()));
        DBAccess.insert(gson.toJson(followerJSON));

        System.out.println("Current Followers Fetched Size:" + followerIDs.getIDs().length);
        // Filtering for influential user
        if (followerIDs.getIDs().length > 1000) {
            System.out.println("User assumed as Influential");
            return null;
        }
        int from = 0;
        int to = 99;
        int limit = checkRateLimit("/users/lookup", twitter);
        while (from < followerIDs.getIDs().length) {
            if (to > followerIDs.getIDs().length)
                to = followerIDs.getIDs().length - 1;
            if (limit == 0)
                checkRateLimit("/users/lookup", twitter);
            ResponseList<User> followers = twitter
                    .lookupUsers(Arrays.copyOfRange(followerIDs.getIDs(), from, to));
            System.out.println("Recieved User count:" + followers.size());
            for (User user : followers) {
                DBAccess.insertUser(gson.toJson(user));
                if (user.getFollowersCount() < 1000) {
                    // if(user.getStatusesCount()>0 &&
                    // user.getStatus()!=null &&
                    // sevenDaysAgo.after(user.getStatus().getCreatedAt()))
                    filteredUserListOnFollowerCount.add(user.getId());
                    getStatuses(user.getScreenName(), twitter);
                } else
                    System.out.println("User " + user.getScreenName()
                            + " is pruned for Influential or over subscription." + " Follower count:"
                            + user.getFollowersCount() + " Friends Count:" + user.getFriendsCount());
            }

            from += 100;
            to += 100;
            limit--;
        }
    } catch (TwitterException te) {
        if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                || te.getStatusCode() == HttpResponseCode.NOT_FOUND) {
            System.out.println("Encountered locked profile. Skipping " + twitterUserId);
            return null;

            // log something here
        }
        te.printStackTrace();
        System.out.println("Failed to get followers' Follower: " + te.getMessage());
        // System.exit(-1);
    }

    return filteredUserListOnFollowerCount;
}

From source file:com.daemon.database.Transactor.java

License:Open Source License

/**
 * Creates an error message with user information for further analysis.
 * @param user The user whose information is to be represented.
 * @return The error message./*  w w  w . ja  va2  s  .c o m*/
 */
private String getUserErrorString(User user) {
    StringBuffer sb = new StringBuffer();

    sb.append("User information:\n");
    sb.append("  id: " + user.getId() + "\n");
    sb.append("  screen name: " + user.getScreenName() + "\n");
    sb.append("  name: " + user.getName() + "\n");
    sb.append("  description: " + user.getDescription() + "\n");
    sb.append("    length: " + user.getDescription().length() + "\n");
    sb.append("   elength: " + StringUtil.escapeNonAscii(user.getDescription()).length() + "\n");
    sb.append("  language: " + user.getLang() + "\n");
    sb.append("  location: " + user.getLocation());

    return sb.toString();
}

From source file:com.daemon.database.Transactor.java

License:Open Source License

/**
 * Adds only the given User to the SQL.Batch which will be saved
 *  in the DB by savedAllTweetsTransactionsafe  
 * /*from   www  .j a  va 2  s.c o m*/
 * @param user The user to be saved.
 * @throws SQLException
 */
private void saveUser(User user) throws SQLException {
    prepStatementUser.setLong(1, user.getId());
    prepStatementUser.setString(2, user.getName());
    prepStatementUser.setString(3, user.getScreenName());
    prepStatementUser.setString(4, user.getProfileImageURL());
    prepStatementUser.setTimestamp(5, new java.sql.Timestamp(user.getCreatedAt().getTime()));
    prepStatementUser.setString(6, user.getLocation());
    prepStatementUser.setString(7, user.getURL());
    prepStatementUser.setString(8, user.getLang());
    prepStatementUser.setInt(9, user.getFollowersCount());
    prepStatementUser.setBoolean(10, user.isVerified());
    prepStatementUser.setString(11, user.getTimeZone());
    prepStatementUser.setString(12, user.getDescription());
    prepStatementUser.setInt(13, user.getStatusesCount());
    prepStatementUser.setInt(14, user.getFriendsCount());

    prepStatementUser.addBatch();
}

From source file:com.daiv.android.twitter.adapters.FollowersArrayAdapter.java

License:Apache License

@Override
public void setFollowingStatus(ViewHolder holder, User u) {
    if (holder.following != null) {
        Log.v("Test_followers", "checking follow status for: " + u.getName());
        Long l = u.getId();
        if (followingIds.contains(l)) {
            holder.following.setVisibility(View.VISIBLE);
            Log.v("Test_followers", "i am following this person");
        } else {//from   w  w  w  .j a  v  a2  s .  com
            holder.following.setVisibility(View.GONE);
            Log.v("Test_followers", "i am not following this person");
        }
    }
}

From source file:com.daiv.android.twitter.adapters.PeopleArrayAdapter.java

License:Apache License

public void bindView(final View view, int position, final User user) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    final long id = user.getId();
    holder.userId = id;//from w w  w  .jav a 2s .  co m

    holder.name.setText(user.getName());
    holder.screenName.setText("@" + user.getScreenName());

    //holder.picture.loadImage(user.getBiggerProfileImageURL(), true, null, NetworkedCacheableImageView.CIRCLE);
    final String url = user.getBiggerProfileImageURL();
    if (settings.roundContactImages) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (holder.userId == id) {
                    loadCircleImage(context, holder, url, mCache, id);
                }
            }
        }, 500);
    } else {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (holder.userId == id) {
                    loadImage(context, holder, url, mCache, id);
                }
            }
        }, 500);
    }

    if (openFirst && position == 0) {
        holder.background.performClick();
        ((Activity) context).finish();
    }

    setFollowingStatus(holder, user);
}

From source file:com.daiv.android.twitter.adapters.UserListMembersArrayAdapter.java

License:Apache License

@Override
public void bindView(final View view, int position, final User user) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    final long id = user.getId();
    holder.userId = id;/*from   ww  w .  j  a v  a2s  . c  om*/

    holder.name.setText(user.getName());
    holder.screenName.setText("@" + user.getScreenName());

    final String url = user.getBiggerProfileImageURL();
    if (settings.roundContactImages) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (holder.userId == id) {
                    loadCircleImage(context, holder, url, mCache, id);
                }
            }
        }, 500);
    } else {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (holder.userId == id) {
                    loadImage(context, holder, url, mCache, id);
                }
            }
        }, 500);
    }

    holder.background.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.remove_user))
                    .setMessage(context.getResources().getString(R.string.remove) + " " + user.getName() + " "
                            + context.getResources().getString(R.string.from_list) + "?")
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            try {
                                new RemoveUser().execute(user.getId() + "");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).create().show();
        }
    });
}

From source file:com.daiv.android.twitter.data.sq_lite.FavoriteUsersDataSource.java

License:Apache License

public synchronized void createUser(User user, int account) {
    ContentValues values = new ContentValues();

    long id = user.getId();
    String screenName = user.getScreenName();
    String name = user.getName();
    String proPicUrl = user.getOriginalProfileImageURL();

    values.put(FavoriteUsersSQLiteHelper.COLUMN_ACCOUNT, account);
    values.put(FavoriteUsersSQLiteHelper.COLUMN_ID, id);
    values.put(FavoriteUsersSQLiteHelper.COLUMN_NAME, name);
    values.put(FavoriteUsersSQLiteHelper.COLUMN_PRO_PIC, proPicUrl);
    values.put(FavoriteUsersSQLiteHelper.COLUMN_SCREEN_NAME, screenName);

    try {//w  ww.  j  a  v a  2  s .  c  om
        database.insert(FavoriteUsersSQLiteHelper.TABLE_HOME, null, values);
    } catch (Exception e) {
        open();
        database.insert(FavoriteUsersSQLiteHelper.TABLE_HOME, null, values);
    }
}

From source file:com.daiv.android.twitter.data.sq_lite.FollowersDataSource.java

License:Apache License

public synchronized void createUser(User user, int account) {
    ContentValues values = new ContentValues();

    long id = user.getId();
    String screenName = user.getScreenName();
    String name = user.getName();
    String proPicUrl = user.getOriginalProfileImageURL();

    values.put(FollowersSQLiteHelper.COLUMN_ACCOUNT, account);
    values.put(FollowersSQLiteHelper.COLUMN_ID, id);
    values.put(FollowersSQLiteHelper.COLUMN_NAME, name);
    values.put(FollowersSQLiteHelper.COLUMN_PRO_PIC, proPicUrl);
    values.put(FollowersSQLiteHelper.COLUMN_SCREEN_NAME, screenName);

    if (database == null || !database.isOpen()) {
        open();/*  ww  w  .  j a  v  a  2  s .c o  m*/
    }

    try {
        database.insert(FollowersSQLiteHelper.TABLE_HOME, null, values);
    } catch (Exception e) {
        // already exist. primary key must be unique
    }
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.ProfileFragment.java

License:Apache License

public void getFollowers(final User user, final AsyncListView listView) {
    spinner.setVisibility(View.VISIBLE);
    canRefresh = false;/*w ww.ja v  a  2 s. c  o  m*/

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                try {
                    if (followingIds == null && user.getId() == settings.myId) {
                        long currCursor = -1;
                        IDs idObject;
                        int rep = 0;

                        do {
                            // gets 5000 ids at a time
                            idObject = twitter.getFriendsIDs(settings.myId, currCursor);

                            long[] lIds = idObject.getIDs();
                            followingIds = new ArrayList<Long>();
                            for (int i = 0; i < lIds.length; i++) {
                                followingIds.add(lIds[i]);
                            }

                            rep++;
                        } while ((currCursor = idObject.getNextCursor()) != 0 && rep < 3);
                    }
                } catch (Throwable t) {
                    followingIds = null;
                }

                PagableResponseList<User> friendsPaging = twitter.getFollowersList(user.getId(),
                        currentFollowers, 100);

                for (int i = 0; i < friendsPaging.size(); i++) {
                    followers.add(friendsPaging.get(i));
                }

                if (friendsPaging.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                currentFollowers = friendsPaging.getNextCursor();

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (followersAdapter == null) {
                            if (followingIds == null) {
                                // we will do a normal array adapter
                                followersAdapter = new PeopleArrayAdapter(context, followers);
                            } else {
                                followersAdapter = new FollowersArrayAdapter(context, followers, followingIds);
                            }
                            listView.setAdapter(followersAdapter);
                        } else {
                            followersAdapter.notifyDataSetChanged();
                        }

                        if (settings.roundContactImages) {
                            ImageUtils.loadSizedCircleImage(context, profilePicture,
                                    thisUser.getOriginalProfileImageURL(), mCache, 96);
                        } else {
                            ImageUtils.loadImage(context, profilePicture, thisUser.getOriginalProfileImageURL(),
                                    mCache);
                        }

                        String url = user.getProfileBannerURL();
                        ImageUtils.loadImage(context, background, url, mCache);

                        canRefresh = true;
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (user != null && user.isProtected()) {
                            Toast.makeText(context, getResources().getString(R.string.protected_account),
                                    Toast.LENGTH_SHORT).show();
                            if (settings.roundContactImages) {
                                ImageUtils.loadSizedCircleImage(context, profilePicture,
                                        thisUser.getOriginalProfileImageURL(), mCache, 96);
                            } else {
                                ImageUtils.loadImage(context, profilePicture, user.getOriginalProfileImageURL(),
                                        mCache);
                            }

                            String url = user.getProfileBannerURL();
                            ImageUtils.loadImage(context, background, url, mCache);
                        } else {
                            Toast.makeText(context, getResources().getString(R.string.error_loading_timeline),
                                    Toast.LENGTH_SHORT).show();
                        }
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                        hasMore = false;
                    }
                });
            }
        }
    }).start();
}