Example usage for twitter4j IDs getIDs

List of usage examples for twitter4j IDs getIDs

Introduction

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

Prototype

long[] getIDs();

Source Link

Usage

From source file:org.loklak.harvester.TwitterAPI.java

License:Open Source License

public static JSONObject getNetworkerNames(final String screen_name, final int max_count,
        final Networker networkRelation) throws IOException, TwitterException {
    if (max_count == 0)
        return new JSONObject();
    boolean complete = true;
    Set<Number> networkingIDs = new LinkedHashSet<>();
    Set<Number> unnetworkingIDs = new LinkedHashSet<>();
    JsonFactory mapcapsule = (networkRelation == Networker.FOLLOWERS ? DAO.followers_dump : DAO.following_dump)
            .get("screen_name", screen_name);
    if (mapcapsule == null) {
        JsonDataset ds = networkRelation == Networker.FOLLOWERS ? DAO.followers_dump : DAO.following_dump;
        mapcapsule = ds.get("screen_name", screen_name);
    }//from www. j  a va 2s .c  om

    if (mapcapsule != null) {
        JSONObject json = mapcapsule.getJSON();

        // check date and completeness
        complete = json.has("complete") ? (Boolean) json.get("complete") : Boolean.FALSE;
        String retrieval_date_string = json.has("retrieval_date") ? (String) json.get("retrieval_date") : null;
        DateTime retrieval_date = retrieval_date_string == null ? null
                : AbstractObjectEntry.utcFormatter.parseDateTime(retrieval_date_string);
        if (complete && System.currentTimeMillis() - retrieval_date.getMillis() < DateParser.DAY_MILLIS)
            return json;

        // load networking ids for incomplete retrievals (untested)
        String nr = networkRelation == Networker.FOLLOWERS ? "follower" : "following";
        if (json.has(nr)) {
            JSONArray fro = json.getJSONArray(nr);
            for (Object f : fro)
                networkingIDs.add((Number) f);
        }
    }
    TwitterFactory tf = getUserTwitterFactory(screen_name);
    if (tf == null)
        tf = getAppTwitterFactory();
    if (tf == null)
        return new JSONObject();
    Twitter twitter = tf.getInstance();
    long cursor = -1;
    collect: while (cursor != 0) {
        try {
            IDs ids = networkRelation == Networker.FOLLOWERS ? twitter.getFollowersIDs(screen_name, cursor)
                    : twitter.getFriendsIDs(screen_name, cursor);
            RateLimitStatus rateStatus = ids.getRateLimitStatus();
            if (networkRelation == Networker.FOLLOWERS) {
                getFollowerIdRemaining = rateStatus.getRemaining();
                getFollowerIdResetTime = System.currentTimeMillis() + rateStatus.getSecondsUntilReset() * 1000;
            } else {
                getFollowingIdRemaining = rateStatus.getRemaining();
                getFollowingIdResetTime = System.currentTimeMillis() + rateStatus.getSecondsUntilReset() * 1000;
            }
            //System.out.println("got: " + ids.getIDs().length + " ids");
            //System.out.println("Rate Status: " + rateStatus.toString() + "; time=" + System.currentTimeMillis());
            boolean dd = false;
            for (long id : ids.getIDs()) {
                if (networkingIDs.contains(id))
                    dd = true; // don't break loop here
                networkingIDs.add(id);
            }
            if (dd)
                break collect; // this is complete!
            if (rateStatus.getRemaining() == 0) {
                complete = false;
                break collect;
            }
            if (networkingIDs.size() >= Math.min(10000, max_count >= 0 ? max_count : 10000)) {
                complete = false;
                break collect;
            }
            cursor = ids.getNextCursor();
        } catch (TwitterException e) {
            complete = false;
            break collect;
        }
    }
    // create result
    JSONObject json = new JSONObject(true);
    json.put("screen_name", screen_name);
    json.put("retrieval_date", AbstractObjectEntry.utcFormatter.print(System.currentTimeMillis()));
    json.put("complete", complete);
    Map<String, Number> networking = getScreenName(networkingIDs, max_count, true);
    Map<String, Number> unnetworking = getScreenName(unnetworkingIDs, max_count, true);
    if (networkRelation == Networker.FOLLOWERS) {
        json.put("followers_count", networking.size());
        json.put("unfollowers_count", unnetworking.size());
        json.put("followers_names", networking);
        json.put("unfollowers_names", unnetworking);
        if (complete)
            DAO.followers_dump.putUnique(json); // currently we write only complete data sets. In the future the update of datasets shall be supported
    } else {
        json.put("following_count", networking.size());
        json.put("unfollowing_count", unnetworking.size());
        json.put("following_names", networking);
        json.put("unfollowing_names", unnetworking);
        if (complete)
            DAO.following_dump.putUnique(json);
    }
    return json;
}

From source file:org.mule.twitter.automation.TwitterTestUtils.java

License:Open Source License

public static boolean isUserIdOnIdList(IDs ids, long userId) {

    boolean found = false;
    long[] idArray = ids.getIDs();

    for (int i = 0; i < idArray.length; i++) {

        if (idArray[i] == userId) {
            found = true;// w ww  .ja v  a 2 s  .  c  o m
            break;
        }
    }

    if (found) {
        return true;
    } else {
        return false;
    }

}

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;/*w  ww .j  a v  a2 s.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;//  w ww  . j  a v  a 2s.c  o 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.thiesen.startpath.data.TwitterExporter.java

License:Open Source License

private static void recurse(final PrintWriter outputStreamWriter, final Set<Long> seen,
        final IDs followersIDs) {
    for (final long followerId : followersIDs.getIDs()) {
        if (!seen.contains(Long.valueOf(followerId))) {
            seen.add(Long.valueOf(followerId));
            EXECUTOR.submit(new Runnable() {
                @Override//from w w  w  .  j  a v  a  2 s .c o m
                public void run() {
                    try {
                        findForUser(outputStreamWriter, followerId, seen);
                    } catch (final TwitterException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
}

From source file:org.thiesen.startpath.data.TwitterExporter.java

License:Open Source License

private static void printSource(final PrintWriter outputStreamWriter, final long id, final IDs friendsIDs) {
    for (final long friendId : friendsIDs.getIDs()) {
        printIds(outputStreamWriter, id, friendId);
    }/*from ww  w .j  a  v  a2s .  c  om*/
}

From source file:org.thiesen.startpath.data.TwitterExporter.java

License:Open Source License

private static void printTarget(final PrintWriter writer, final IDs followersIDs, final long id) {
    for (final long followerId : followersIDs.getIDs()) {
        printIds(writer, followerId, id);
    }//from  ww  w  . j  a  v  a  2  s.  c  o m
}

From source file:org.tweetalib.android.model.TwitterIds.java

License:Apache License

public void add(IDs ids) {
    long[] idsArray = ids.getIDs();
    for (long id : idsArray) {
        mIds.add(id);/*  w w w.  j  av a  2  s. com*/
    }
}

From source file:org.wso2.carbon.connector.twitter.TwitterGetFollowersIds.java

License:Open Source License

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    if (log.isDebugEnabled()) {
        log.info("executing twitter get user time line");
    }//  www. ja va2 s  . co  m

    try {

        String screenName = (TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME).isEmpty())

                        ? TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME)
                        : null;

        String userID = (TwitterUtils.lookupTemplateParamater(messageContext, USER_ID) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, USER_ID).isEmpty())

                        ? TwitterUtils.lookupTemplateParamater(messageContext, USER_ID)
                        : null;

        String count = (TwitterUtils.lookupTemplateParamater(messageContext, COUNT) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, COUNT).isEmpty())
                        ? TwitterUtils.lookupTemplateParamater(messageContext, COUNT)
                        : null;

        Twitter twitter = new TwitterClientLoader(messageContext).loadApiClient();

        IDs ids = null;

        if (screenName != null && !screenName.isEmpty()) {
            if (count != null && !count.isEmpty()) {
                ids = twitter.getFollowersIDs(screenName, Long.parseLong(count));
            } else {
                ids = twitter.getFollowersIDs(screenName, -1);
            }
        } else if (userID != null && !userID.isEmpty()) {
            if (count != null && !count.isEmpty()) {
                ids = twitter.getFollowersIDs(Long.parseLong(userID), Long.parseLong(count));
            } else {
                ids = twitter.getFollowersIDs(Long.parseLong(userID), -1);
            }
        }

        if (log.isDebugEnabled()) {
            log.error("Retrived result: " + ids.toString());
        }

        OMElement element = this.performSearchMessages(ids.getIDs());

        super.preparePayload(messageContext, element);

    } catch (TwitterException te) {
        log.error("Failed to search twitter : " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    } catch (Exception te) {
        log.error("Failed to search generic: " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    }
}

From source file:org.wso2.carbon.connector.twitter.TwitterGetFriendsIds.java

License:Open Source License

@Override
public void connect(MessageContext messageContext) throws ConnectException {
    if (log.isDebugEnabled()) {
        log.info("executing twitter get user time line");
    }//from   ww  w  . j  av  a2  s  . co  m

    try {

        String screenName = (TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME).isEmpty())

                        ? TwitterUtils.lookupTemplateParamater(messageContext, SCREEN_NAME)
                        : null;

        String userID = (TwitterUtils.lookupTemplateParamater(messageContext, USER_ID) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, USER_ID).isEmpty())

                        ? TwitterUtils.lookupTemplateParamater(messageContext, USER_ID)
                        : null;

        String count = (TwitterUtils.lookupTemplateParamater(messageContext, COUNT) != null
                && !TwitterUtils.lookupTemplateParamater(messageContext, COUNT).isEmpty())
                        ? TwitterUtils.lookupTemplateParamater(messageContext, COUNT)
                        : null;

        Twitter twitter = new TwitterClientLoader(messageContext).loadApiClient();

        IDs ids = null;

        if (screenName != null && !screenName.isEmpty()) {
            if (count != null && !count.isEmpty()) {
                ids = twitter.getFriendsIDs(screenName, Long.parseLong(count));
            } else {
                ids = twitter.getFriendsIDs(screenName, -1);
            }
        } else if (userID != null && !userID.isEmpty()) {
            if (count != null && !count.isEmpty()) {
                ids = twitter.getFriendsIDs(Long.parseLong(userID), Long.parseLong(count));
            } else {
                ids = twitter.getFriendsIDs(Long.parseLong(userID), -1);
            }
        }

        if (log.isDebugEnabled()) {
            log.error("Retrived result: " + ids.toString());
        }

        OMElement element = this.performSearchMessages(ids.getIDs());

        super.preparePayload(messageContext, element);

    } catch (TwitterException te) {
        log.error("Failed to search twitter : " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    } catch (Exception te) {
        log.error("Failed to search generic: " + te.getMessage(), te);
        TwitterUtils.storeErrorResponseStatus(messageContext, te);
    }
}