Example usage for twitter4j IDs hasNext

List of usage examples for twitter4j IDs hasNext

Introduction

In this page you can find the example usage for twitter4j IDs hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:org.apache.streams.twitter.provider.TwitterFollowingProviderTask.java

License:Apache License

private void collectIds(Long id) {
    int keepTrying = 0;

    long curser = -1;

    twitter4j.User user;// www . j  a v a2  s .  c o  m
    String userJson;
    try {
        user = client.users().showUser(id);
        userJson = TwitterObjectFactory.getRawJSON(user);
    } catch (TwitterException ex) {
        LOGGER.error("Failure looking up " + id);
        return;
    }

    do {
        try {
            twitter4j.IDs ids = null;
            if (provider.getConfig().getEndpoint().equals("followers")) {
                ids = client.friendsFollowers().getFollowersIDs(id, curser,
                        provider.getConfig().getMaxItems().intValue());
            } else if (provider.getConfig().getEndpoint().equals("friends")) {
                ids = client.friendsFollowers().getFriendsIDs(id, curser,
                        provider.getConfig().getMaxItems().intValue());
            }

            Objects.requireNonNull(ids);
            Preconditions.checkArgument(ids.getIDs().length > 0);

            for (long otherId : ids.getIDs()) {

                try {
                    Follow follow = null;
                    if (provider.getConfig().getEndpoint().equals("followers")) {
                        follow = new Follow().withFollowee(new User().withId(id))
                                .withFollower(new User().withId(otherId));
                    } else if (provider.getConfig().getEndpoint().equals("friends")) {
                        follow = new Follow().withFollowee(new User().withId(otherId))
                                .withFollower(new User().withId(id));
                    }

                    Objects.requireNonNull(follow);

                    if (item_count < provider.getConfig().getMaxItems()) {
                        ComponentUtils.offerUntilSuccess(new StreamsDatum(follow), provider.providerQueue);
                        item_count++;
                    }
                } catch (Exception ex) {
                    LOGGER.warn("Exception: {}", ex);
                }
            }
            if (!ids.hasNext()) {
                break;
            }
            if (ids.getNextCursor() == 0) {
                break;
            }
            curser = ids.getNextCursor();
            page_count++;
        } catch (TwitterException twitterException) {
            keepTrying += TwitterErrorHandler.handleTwitterError(client, id, twitterException);
        } catch (Exception ex) {
            keepTrying += TwitterErrorHandler.handleTwitterError(client, null, ex);
        }
    } while (shouldContinuePulling() && curser != 0 && keepTrying < provider.getConfig().getRetryMax());
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

private void addFollowing(User twitterUser, boolean createNewUsers) {

    if (twitterUser == null) {
        return;/*from   w  w  w.  j a  v a2s  .  co  m*/
    }

    IDs following;
    long cursor = -1;

    // look up person for twitter user and connect
    Person connectToPerson = getPersonForTwitterUser(twitterUser);

    do {
        try {
            following = twitterAPI.getFriendsIDs(twitterUser.getId(), cursor);
            log("Retrieving following with cursor: " + cursor, LogService.LOG_INFO);
        } catch (TwitterException e) {
            log("Could not get more following Users from twitter. (Cursor: " + cursor + "): " + e.getMessage(),
                    LogService.LOG_DEBUG);
            break;
        }

        if (following == null) {
            return;
        }

        if (connectToPerson != null) {
            connectPersonsForTwitterUserIds(following.getIDs(), connectToPerson, true, createNewUsers);
        }

        // next cursor
        cursor = following.getNextCursor();
    } while (following.hasNext());
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

private void addFollower(User twitterUser, boolean createNewUsers) {

    if (twitterUser == null) {
        return;//ww  w.  j  a v a2  s .  co  m
    }

    int followersMax = 1000;

    int followersCount = twitterUser.getFollowersCount();
    if (followersCount > followersMax) {
        // ignore users with to much followers
        log("Ignoring the " + followersCount + " Followers of " + twitterUser.getName());
        return;
    }

    IDs followers;
    long cursor = -1;

    do {
        try {
            followers = twitterAPI.getFollowersIDs(twitterUser.getId(), cursor);
            log("Retrieving followers with cursor: " + cursor, LogService.LOG_INFO);
        } catch (TwitterException e) {
            log("Could not get more followers from twitter. (Cursor: " + cursor + "): " + e.getMessage(),
                    LogService.LOG_DEBUG);
            break;
        }

        if (followers == null) {
            return;
        }

        // look up person for twitter user and connect
        Person connectToPerson = getPersonForTwitterUser(twitterUser);
        if (connectToPerson != null) {
            connectPersonsForTwitterUserIds(followers.getIDs(), connectToPerson, false, createNewUsers);
        }
        // next cursor
        cursor = followers.getNextCursor();
    } while (followers.hasNext());
}

From source file:org.yukung.following2ldr.command.impl.FindFeedUrlCommand.java

License:Apache License

@Override
public void run() throws Throwable {
    // Twitter??/*www  .  j  ava  2  s  .  c  o m*/
    long start = System.currentTimeMillis();
    String consumerKey = config.getProperty(Constants.CONSUMER_KEY);
    String consumerSecret = config.getProperty(Constants.CONSUMER_SECRET);
    String userName = params.get(0);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    RequestToken requestToken = twitter.getOAuthRequestToken();
    String authorizationURL = requestToken.getAuthorizationURL();
    System.out.println(":" + authorizationURL);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter the PIN:");
    String pin = br.readLine();
    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, pin);
    twitter.setOAuthAccessToken(accessToken);
    long cursor = -1L;
    IDs friendIDs;
    List<Long> iDsList = new ArrayList<Long>(5000);
    do {
        friendIDs = twitter.getFriendsIDs(userName, cursor);
        long[] iDs = friendIDs.getIDs();
        for (long iD : iDs) {
            iDsList.add(iD);
        }
        cursor = friendIDs.getNextCursor();
    } while (friendIDs.hasNext());
    List<long[]> list = new ArrayList<long[]>();
    int offset = 0;
    long[] tmp = new long[100];
    for (Long id : iDsList) {
        if (offset < 100) {
            tmp[offset] = id;
            offset++;
        } else {
            list.add(tmp);
            offset = 0;
            tmp = new long[100];
        }
    }
    list.add(tmp);
    List<URL> urlList = new ArrayList<URL>();
    for (long[] array : list) {
        ResponseList<User> lookupUsers = twitter.lookupUsers(array);
        for (User user : lookupUsers) {
            log.info("URL:" + user.getURL());
            urlList.add(user.getURL());
        }
    }
    String path = "C:\\Users\\ikeda_yusuke\\Documents\\sandbox\\java\\data\\" + userName + ".txt";
    FileWriter writer = new FileWriter(path);
    BufferedWriter out = new BufferedWriter(writer);
    //      PrintWriter pw = new PrintWriter(writer);
    for (URL url : urlList) {
        if (url != null) {
            out.write(url.toString() + "\n");
        }
    }
    out.flush();
    out.close();
    long end = System.currentTimeMillis();
    log.info("?:" + (end - start) + " ms");

    // ??IDor??
    // Twitter API??????ID?
    // ?????ID?URL100???
    // ??URL?

}

From source file:TwitterDownload.TwitterHandler.java

public List<User> getUserFollowers(String searchPhrase, int pageSize) throws TwitterException {
    //todo test rate limit

    ArrayList<User> listFollowers = new ArrayList<User>();

    long cursor = -1;

    IDs followersIDs;

    do {/*from  www  .j a  va  2  s. c  om*/
        followersIDs = twitter.getFollowersIDs(searchPhrase, cursor);

        long[] ids = followersIDs.getIDs();

        while (ids.length > 0) {
            long[] searchIds = Arrays.copyOfRange(ids, 0, 100);
            if (ids.length >= 100)
                ids = Arrays.copyOfRange(ids, 100, ids.length);
            else
                ids = new long[0];

            ResponseList<User> users = twitter.lookupUsers(searchIds);

            for (User user : users) {
                listFollowers.add(user);
            }
        }

        if (followersIDs.hasNext())
            cursor = followersIDs.getNextCursor();
        else
            cursor = -1;
    } while (cursor > 0 && listFollowers.size() < pageSize);

    return listFollowers;
}

From source file:TwitterDownload.TwitterHandler.java

public List<User> getUserFriends(String searchPhrase, int pageSize) throws TwitterException {
    //todo test rate limit

    ArrayList<User> listFriends = new ArrayList<User>();

    long cursor = -1;

    IDs friendsIDs;

    do {//from  w  ww. j av  a  2s.c o  m
        friendsIDs = twitter.getFriendsIDs(searchPhrase, cursor);

        long[] ids = friendsIDs.getIDs();

        while (ids.length > 0) {
            long[] searchIds = Arrays.copyOfRange(ids, 0, 100);
            if (ids.length >= 100)
                ids = Arrays.copyOfRange(ids, 100, ids.length);
            else
                ids = new long[0];

            ResponseList<User> users = twitter.lookupUsers(searchIds);

            for (User user : users) {
                listFriends.add(user);
            }
        }

        if (friendsIDs.hasNext())
            cursor = friendsIDs.getNextCursor();
        else
            cursor = -1;
    } while (cursor > 0 && listFriends.size() < pageSize);

    return listFriends;
}

From source file:twitterrest.Followersids.java

License:Apache License

static List<Long> followers(Twitter twitter, String screenName) {
    List<Long> m_FollowersList = new ArrayList<Long>();

    long cursor = -1;
    //int count = 0;
    while (true) {
        IDs ids = null;
        try {//from ww w  .j  ava  2  s.com
            //IDs ids = twitter.getFollowersIDs(user.getId(), cursor);
            ids = twitter.getFollowersIDs(screenName, cursor);
        } catch (TwitterException twitterException) {
            // Rate Limit ?????????
            // () status code ?????????

            RateLimitStatus rateLimit = twitterException.getRateLimitStatus();
            int secondsUntilReset = rateLimit.getSecondsUntilReset();
            System.err.println("please wait for " + secondsUntilReset + " seconds");
            System.err.println("Reset Time : " + rateLimit.getResetTimeInSeconds());

            //() 120?getSecondsUntilReset() ????
            //?????????
            long waitTime = (long) (secondsUntilReset + 120) * 1000;
            //long waitTime = (long)(300 * 1000); // 300?
            try {
                Thread.sleep(waitTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            continue;
        } catch (Exception e) {
            e.printStackTrace();
        }

        long[] idArray = ids.getIDs();
        for (int i = 0; i < idArray.length; i++) {
            //System.out.println("["+(++count)+"]" + idArray[i]);
            m_FollowersList.add(new Long(idArray[i]));
        }

        if (ids.hasNext()) {
            cursor = ids.getNextCursor();
        } else {
            break;
        }
    }
    return m_FollowersList;
}