Example usage for twitter4j Twitter lookupUsers

List of usage examples for twitter4j Twitter lookupUsers

Introduction

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

Prototype

ResponseList<User> lookupUsers(long... ids) throws TwitterException;

Source Link

Document

Return up to 100 users worth of extended information, specified by either ID, screen name, or combination of the two.

Usage

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

License:Apache License

@Override
public void run() throws Throwable {
    // Twitter??//w  ww.j a v a2s. com
    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:profiles.FilesThreaderParser.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException,
        FileNotFoundException, UnsupportedEncodingException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length < 5)) {
        System.err.println("5 Parameters are required to launch a Job.");
        System.err.println("First: String 'INPUT: /input/path/'");
        System.err.println("Second: String 'OUTPUT: /output/path/'");
        System.err.println("Third: (int) Total Number Of Jobs");
        System.err.println("Fourth: (int) This Job Number");
        System.err.println("Fifth: (int) Number of seconds to pause");
        System.err.println("Example: fileToRun /input/path/ /output/path/ " + "10 1 3");
        System.exit(-1);//  w  ww. jav  a  2 s .  co m
    }

    // TODO documentation for command line
    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/users/lookup";

    String inputPath = null;
    try {
        inputPath = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument " + args[0] + " must be an String.");
        System.exit(-1);
    }

    String outputPath = null;
    try {
        outputPath = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument " + args[1] + " must be an String.");
        System.exit(-1);
    }

    int TOTAL_JOBS = 0;
    try {
        TOTAL_JOBS = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
        System.err.println("Argument " + args[2] + " must be an integer.");
        System.exit(1);
    }

    int JOB_NO = 0;
    try {
        JOB_NO = Integer.parseInt(args[3]);
    } catch (NumberFormatException e) {
        System.err.println("Argument " + args[3] + " must be an integer.");
        System.exit(1);
    }

    int secondsToPause = 0;
    try {
        secondsToPause = Integer.parseInt(args[4]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[4] + " must be an integer.");
        System.exit(-1);
    }

    try {

        int TotalWorkLoad = 0;
        ArrayList<String> allFiles = null;
        try {
            final File folder = new File(inputPath);
            allFiles = helpers.listFilesForSingleFolder(folder);
            TotalWorkLoad = allFiles.size();
        } catch (Exception e) {

            System.err.println("Input folder is not exists: " + e.getMessage());
            System.exit(-1);
        }

        System.out.println("Total Workload is: " + TotalWorkLoad);

        if (TotalWorkLoad < 1) {
            System.err.println("No targeted user file exists in: " + inputPath);
            System.exit(-1);
        }

        if (TOTAL_JOBS > TotalWorkLoad) {
            System.err.println("Number of jobs are more than total work"
                    + " load. Please reduce 'Number of jobs' to launch.");
            System.exit(-1);
        }

        float TotalWorkLoadf = TotalWorkLoad;
        float TOTAL_JOBSf = TOTAL_JOBS;
        float res = (TotalWorkLoadf / TOTAL_JOBSf);

        int chunkSize = (int) Math.ceil(res);
        int offSet = JOB_NO * chunkSize;
        int chunkSizeToGet = (JOB_NO + 1) * chunkSize;

        System.out.println("My Share is " + chunkSize);
        System.out.println("My offSet is " + offSet);
        System.out.println("My chunkSizeToGet is " + chunkSizeToGet);
        System.out.println();

        // Load OAuh User
        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls;
        int RemainingCallsCounter = 0;
        System.out.println("First Time OAuth Remianing Calls: " + RemainingCalls);

        String Screen_name = AppOAuths.screen_name;
        System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name);
        System.out.println();

        System.out.println("Going to get Profiles.");

        if (JOB_NO + 1 == TOTAL_JOBS) {
            chunkSizeToGet = TotalWorkLoad;
        }

        secondsToPause = (TOTAL_JOBS * secondsToPause) - (JOB_NO * secondsToPause);
        System.out.println("secondsToPause: " + secondsToPause);
        helpers.pause(secondsToPause);

        // to write output in a file
        System.out.flush();

        List<String> fileNamesShare = allFiles.subList(offSet, chunkSizeToGet);

        for (String fileName : fileNamesShare) {

            System.out.println("Going to parse file: " + fileName);

            try {

                // open file to write all profiles
                String filesPath = outputPath + "/";
                PrintWriter writer = new PrintWriter(filesPath + "/" + fileName, "UTF-8");

                try (BufferedReader br = new BufferedReader(new FileReader(inputPath + "/" + fileName))) {
                    String jsonString;
                    while ((jsonString = br.readLine()) != null) {
                        // process the line.

                        List<String> fileContent = new ArrayList<>();
                        fileContent.add(jsonString);

                        String[] strarray = fileContent.toArray(new String[0]);

                        String ss = Arrays.toString(strarray);

                        JSONArray obj = new JSONArray(ss);
                        JSONObject obj4 = (JSONObject) obj.get(0);

                        JSONArray idsS = (JSONArray) obj4.get("ids");

                        ArrayList<String> Idslist = new ArrayList<String>();
                        if (idsS != null) {
                            int len = idsS.length();
                            for (int i = 0; i < len; i++) {
                                Idslist.add(idsS.get(i).toString());
                            }
                        }

                        for (int start = 0; start < Idslist.size(); start += 100) {
                            int end = Math.min(start + 100, Idslist.size());
                            List<String> sublist = Idslist.subList(start, end);

                            long[] idsdata = new long[sublist.size()];
                            for (int i = 0; i < sublist.size(); i++) {
                                idsdata[i] = Long.valueOf(sublist.get(i));
                            }
                            ResponseList<User> profiles = null;

                            while (true) {

                                try {
                                    profiles = twitter.lookupUsers(idsdata);

                                    if (profiles.size() > 0) {
                                        for (User user : profiles) {
                                            String rawJSON = TwitterObjectFactory.getRawJSON(user);

                                            // put profilesJSON in a file
                                            try {
                                                writer.println(rawJSON);
                                            } catch (Exception e) {
                                                System.err.println(e.getMessage());
                                                System.exit(0);
                                            }
                                        }

                                        break;
                                    }

                                } catch (TwitterException te) {

                                    // If rate limit reached then switch Auth
                                    // user
                                    RemainingCallsCounter++;
                                    if (RemainingCallsCounter >= RemainingCalls) {

                                        // Load OAuth user
                                        tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                                        twitter = tf.getInstance();

                                        System.out.println("New User Loaded" + " OAuth Screen_name: "
                                                + AppOAuths.screen_name);

                                        RemainingCalls = AppOAuths.RemainingCalls - 2;
                                        RemainingCallsCounter = 0;

                                        System.out.println("New Remianing Calls: " + RemainingCalls);
                                    }

                                }

                            } // while get profiles

                            // If rate limit reached then switch Auth user
                            RemainingCallsCounter++;
                            if (RemainingCallsCounter >= RemainingCalls) {

                                // Load OAuth user
                                tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
                                twitter = tf.getInstance();

                                System.out.println(
                                        "New User Loaded OAuth " + "Screen_name: " + AppOAuths.screen_name);

                                RemainingCalls = AppOAuths.RemainingCalls - 2;
                                RemainingCallsCounter = 0;

                                System.out.println("New Remianing Calls: " + RemainingCalls);
                            }

                        }

                    }

                }
                writer.close();
            } // read my single file
            catch (IOException e) {
                System.err.println("Failed to read lines from " + fileName);
            }

            // delete file if processed
            File fileToDelete = new File(inputPath + "/" + "/" + fileName);
            fileToDelete.delete();

            // to write output in a file
            System.out.flush();
        } // all my files

        // If rate limit reached then switch Auth user
        RemainingCallsCounter++;
        if (RemainingCallsCounter >= RemainingCalls) {

            // load auth user
            tf = AppOAuths.loadOAuthUser(endpoint, TOTAL_JOBS, JOB_NO);
            twitter = tf.getInstance();

            System.out.println("New Loaded OAuth User Screen_name: " + AppOAuths.screen_name);

            RemainingCalls = AppOAuths.RemainingCalls;
            RemainingCallsCounter = 0;

            System.out.println("New OAuth Remianing Calls: " + RemainingCalls);
        }

    } catch (TwitterException te) {
        // te.printStackTrace();
        System.err.println("Failed to get Profiles: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");

    // Close System.out for this thread which will
    // flush and close this thread.
    System.out.close();
}

From source file:twitter4j.examples.user.LookupUsers.java

License:Apache License

/**
 * Usage: java twitter4j.examples.user.LookupUsers [screen name[,screen name..]]
 *
 * @param args message/*from   ww w. j ava 2  s  .  c o m*/
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.user.LookupUsers [screen name[,screen name..]]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        ResponseList<User> users = twitter.lookupUsers(args[0].split(","));
        for (User user : users) {
            if (user.getStatus() != null) {
                System.out.println("@" + user.getScreenName() + " - " + user.getStatus().getText());
            } else {
                // the user is protected
                System.out.println("@" + user.getScreenName());
            }
        }
        System.out.println("Successfully looked up users [" + args[0] + "].");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to lookup users: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:twitterlab.TwitterLab.java

/**
 * @param args the command line arguments
 *///  w  w  w. ja va 2 s. co m
public static void main(String[] args) {
    // TODO code application logic here
    Users = new ArrayList();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("yBSAPlE6wiAQU6CyIiXHSapwN");
    cb.setOAuthConsumerSecret("3CjbEkYyIZvNZ07KYpC55v9vpqxoNmMs1IkC48Wqr5tKXjUnZk");
    cb.setOAuthAccessToken("858371720-0RASIMJeaMbWr2YPmCPyh5OgCP6Kesgz0WvdsLz7");
    cb.setOAuthAccessTokenSecret("cfi5JDGLTUOKHNj7yFIdivg0Hr1bCml3VlQXj5XCOyoFr");

    try {
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter tw = tf.getInstance();
        IDs ids = tw.getFriendsIDs(-1);
        long[] ID;
        if (ids.getIDs().length > 10) {
            ID = new long[10];
            System.arraycopy(ids.getIDs(), 0, ID, 0, 10);
        } else {
            ID = ids.getIDs();
        }
        ResponseList<User> Friends = tw.lookupUsers(ID);
        User[] A = new User[10];
        if (Friends.toArray().length > 0) {
            Users.add(new ArrayList(Arrays.asList(Friends.toArray(A))));
        }
        for (int i = 0; i < ID.length; i++) {
            IDs idsFriend = tw.getFriendsIDs(ID[i], -1);
            long[] IDfriend;
            if (idsFriend.getIDs().length > 10) {
                IDfriend = new long[10];
                System.arraycopy(idsFriend.getIDs(), 0, IDfriend, 0, 10);
            } else {
                IDfriend = idsFriend.getIDs();
            }
            ResponseList<User> FriendsFriend = tw.lookupUsers(IDfriend);
            A = new User[10];
            if (FriendsFriend.toArray().length > 0) {
                Users.add(new ArrayList(Arrays.asList(FriendsFriend.toArray(A))));
            }
            //System.out.println("Sleep?");
            TimeUnit.SECONDS.sleep(60);
        }

        for (int i = 0; i < Users.size(); i++) {
            for (int j = 0; j < Users.get(0).size(); j++) {
                System.out.println(Users.get(i).get(j).getScreenName());
            }
        }

    } catch (Exception e) {
        // Thread.currentThread().interrupt();
        e.printStackTrace();
    }

}