Example usage for twitter4j Twitter getFriendsIDs

List of usage examples for twitter4j Twitter getFriendsIDs

Introduction

In this page you can find the example usage for twitter4j Twitter getFriendsIDs.

Prototype

IDs getFriendsIDs(long userId, long cursor) throws TwitterException;

Source Link

Document

Returns an array of numeric IDs for every user the specified user is following.

Usage

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

License:Open Source License

private static void findForUser(final PrintWriter outputStreamWriter, final long id, final Set<Long> seen)
        throws TwitterException {
    final Twitter twitter = new TwitterFactory().getInstance();
    final IDs followersIDs = twitter.getFollowersIDs(id, -1);
    printTarget(outputStreamWriter, followersIDs, id);
    final IDs friendsIDs = twitter.getFriendsIDs(id, -1);
    printSource(outputStreamWriter, id, friendsIDs);

    recurse(outputStreamWriter, seen, followersIDs);
    recurse(outputStreamWriter, seen, friendsIDs);
}

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 a  2  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);
    }
}

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

License:Apache License

@Override
public void run() throws Throwable {
    // Twitter??/*ww w  .  j a v a  2 s.  co 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:twitter4j.examples.friendsandfollowers.GetFriendsIDs.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendsandfollowers.GetFriendsIDs [screen name]
 *
 * @param args message/*from  w  ww .j  a v a  2s.co  m*/
 */
public static void main(String[] args) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing following ids.");
        do {
            if (0 < args.length) {
                ids = twitter.getFriendsIDs(args[0], cursor);
            } else {
                ids = twitter.getFriendsIDs(cursor);
            }
            for (long id : ids.getIDs()) {
                System.out.println(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get friends' ids: " + te.getMessage());
        System.exit(-1);
    }
}