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: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;/*from  www . java  2s. c  om*/

    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();
}

From source file:com.dwdesign.tweetings.loader.UserFriendsLoader.java

License:Open Source License

@Override
public IDs getIDs() throws TwitterException {
    final Twitter twitter = getTwitter();
    if (twitter == null)
        return null;
    if (mUserId > 0)
        return twitter.getFriendsIDs(mUserId, -1);
    else if (mScreenName != null)
        return twitter.getFriendsIDs(mScreenName, -1);
    return null;/*from  w  w w.j av a  2s  .  c o  m*/
}

From source file:com.happy_coding.viralo.twitter.FriendDiscoverer.java

License:Apache License

/**
 * Returns the friends for the provided friend.
 *
 * @param forContact/*w w  w. j  av  a2s.  c o m*/
 * @return
 */
public List<Contact> findFriends(Contact forContact) {

    List<Contact> contacts = new ArrayList<Contact>();
    Twitter twitter = new TwitterFactory().getInstance();

    try {
        IDs list = twitter.getFriendsIDs(forContact.getUid(), -1);
        do {

            for (long id : list.getIDs()) {
                Contact contact = new Contact(id);
                contact.setActiveUid(twitter.getId());
                contacts.add(contact);
            }
        } while (list.hasNext());

    } catch (TwitterException e) {
        logger.error("can't find friends for contact", e);
        return null;
    }
    return contacts;
}

From source file:com.klinker.android.twitter.services.TalonPullNotificationService.java

License:Apache License

@Override
public void onCreate() {
    super.onCreate();

    if (TalonPullNotificationService.isRunning) {
        stopSelf();/*from  w ww.j  a v  a  2s.co  m*/
        return;
    }

    TalonPullNotificationService.isRunning = true;

    settings = AppSettings.getInstance(this);

    mCache = App.getInstance(this).getBitmapCache();

    sharedPreferences = getSharedPreferences("com.klinker.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    pullUnread = sharedPreferences.getInt("pull_unread", 0);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Intent stop = new Intent(this, StopPull.class);
    PendingIntent stopPending = PendingIntent.getService(this, 0, stop, 0);

    Intent popup = new Intent(this, RedirectToPopup.class);
    popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    popup.putExtra("from_notification", true);
    PendingIntent popupPending = PendingIntent.getActivity(this, 0, popup, 0);

    Intent compose = new Intent(this, WidgetCompose.class);
    popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent composePending = PendingIntent.getActivity(this, 0, compose, 0);

    String text;

    int count = 0;

    if (sharedPreferences.getBoolean("is_logged_in_1", false)) {
        count++;
    }
    if (sharedPreferences.getBoolean("is_logged_in_2", false)) {
        count++;
    }

    boolean multAcc = false;
    if (count == 2) {
        multAcc = true;
    }

    if (settings.liveStreaming && settings.timelineNot) {
        text = getResources().getString(R.string.new_tweets_upper) + ": " + pullUnread;
    } else {
        text = getResources().getString(R.string.listening_for_mentions) + "...";
    }

    mBuilder = new NotificationCompat.Builder(this).setSmallIcon(android.R.color.transparent)
            .setContentTitle(getResources().getString(R.string.talon_pull)
                    + (multAcc ? " - @" + settings.myScreenName : ""))
            .setContentText(text).setOngoing(true)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_stat_icon));

    if (getApplicationContext().getResources().getBoolean(R.bool.expNotifications)) {
        mBuilder.addAction(R.drawable.ic_cancel_dark,
                getApplicationContext().getResources().getString(R.string.stop), stopPending);
        mBuilder.addAction(R.drawable.ic_popup, getResources().getString(R.string.popup), popupPending);
        mBuilder.addAction(R.drawable.ic_send_dark, getResources().getString(R.string.tweet), composePending);
    }

    try {
        mBuilder.setWhen(0);
    } catch (Exception e) {
    }

    mBuilder.setContentIntent(pendingIntent);

    // priority flag is only available on api level 16 and above
    if (getResources().getBoolean(R.bool.expNotifications)) {
        mBuilder.setPriority(Notification.PRIORITY_MIN);
    }

    mContext = getApplicationContext();

    IntentFilter filter = new IntentFilter();
    filter.addAction("com.klinker.android.twitter.STOP_PUSH");
    registerReceiver(stopPush, filter);

    filter = new IntentFilter();
    filter.addAction("com.klinker.android.twitter.START_PUSH");
    registerReceiver(startPush, filter);

    filter = new IntentFilter();
    filter.addAction("com.klinker.android.twitter.STOP_PUSH_SERVICE");
    registerReceiver(stopService, filter);

    if (settings.liveStreaming && settings.timelineNot) {
        filter = new IntentFilter();
        filter.addAction("com.klinker.android.twitter.UPDATE_NOTIF");
        registerReceiver(updateNotification, filter);

        filter = new IntentFilter();
        filter.addAction("com.klinker.android.twitter.NEW_TWEET");
        registerReceiver(updateNotification, filter);

        filter = new IntentFilter();
        filter.addAction("com.klinker.android.twitter.CLEAR_PULL_UNREAD");
        registerReceiver(clearPullUnread, filter);
    }

    Thread start = new Thread(new Runnable() {
        @Override
        public void run() {
            // get the ids of everyone you follow
            try {
                Log.v("getting_ids", "started getting ids, mine: " + settings.myId);
                Twitter twitter = Utils.getTwitter(mContext, settings);
                long currCursor = -1;
                IDs idObject;
                int rep = 0;

                do {
                    idObject = twitter.getFriendsIDs(settings.myId, currCursor);

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

                    rep++;
                } while ((currCursor = idObject.getNextCursor()) != 0 && rep < 3);

                ids.add(settings.myId);

                idsLoaded = true;

                startForeground(FOREGROUND_SERVICE_ID, mBuilder.build());

                mContext.sendBroadcast(new Intent("com.klinker.android.twitter.START_PUSH"));
            } catch (Exception e) {
                e.printStackTrace();
                TalonPullNotificationService.isRunning = false;

                pullUnread = 0;

                Thread stop = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        TalonPullNotificationService.shuttingDown = true;
                        try {
                            //pushStream.removeListener(userStream);
                        } catch (Exception x) {

                        }
                        try {
                            pushStream.cleanUp();
                            pushStream.shutdown();
                            Log.v("twitter_stream_push", "stopping push notifications");
                        } catch (Exception e) {
                            // it isn't running
                            e.printStackTrace();
                            // try twice to shut it down i guess
                            try {
                                Thread.sleep(2000);
                                pushStream.cleanUp();
                                pushStream.shutdown();
                                Log.v("twitter_stream_push", "stopping push notifications");
                            } catch (Exception x) {
                                // it isn't running
                                x.printStackTrace();
                            }
                        }

                        TalonPullNotificationService.shuttingDown = false;
                    }
                });

                stop.setPriority(Thread.MAX_PRIORITY);
                stop.start();

                stopSelf();
            } catch (OutOfMemoryError e) {
                TalonPullNotificationService.isRunning = false;

                Thread stop = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        TalonPullNotificationService.shuttingDown = true;
                        try {
                            //pushStream.removeListener(userStream);
                        } catch (Exception x) {

                        }
                        try {
                            pushStream.cleanUp();
                            pushStream.shutdown();
                            Log.v("twitter_stream_push", "stopping push notifications");
                        } catch (Exception e) {
                            // it isn't running
                            e.printStackTrace();
                            // try twice to shut it down i guess
                            try {
                                Thread.sleep(2000);
                                pushStream.cleanUp();
                                pushStream.shutdown();
                                Log.v("twitter_stream_push", "stopping push notifications");
                            } catch (Exception x) {
                                // it isn't running
                                x.printStackTrace();
                            }
                        }

                        TalonPullNotificationService.shuttingDown = false;
                    }
                });

                stop.setPriority(Thread.MAX_PRIORITY);
                stop.start();

                pullUnread = 0;

                stopSelf();
            }

        }
    });

    start.setPriority(Thread.MAX_PRIORITY - 1);
    start.start();

}

From source file:com.klinker.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;//from  ww w . 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);

                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();
}

From source file:com.SocialAccess.TwitterSearch.java

public static void searchWhomUserFollow() {

    List<String[]> data = CSVutil.readCSV(Constants.CSV_FILE_NAME);
    String[] people = data.get(0);
    System.out.print(people[0]);//from  ww w .  j a v a 2 s  .com

    try {
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing following ids.");
        do {

            ids = twitter.getFriendsIDs("derekmizak", 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);
    }
}

From source file:friendsandfollowers.DBFriendsIDs.java

License:Apache License

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

    // Check arguments that passed in
    if ((args == null) || (args.length == 0)) {
        System.err.println("2 Parameters are required plus one optional " + "parameter to launch a Job.");
        System.err.println("First: String 'OUTPUT: /output/path/'");
        System.err.println("Second: (int) Number of ids to fetch. " + "Provide number which increment by 5000 "
                + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids.");
        System.err.println("Third (optional): 'screen_name / user_id_str'");
        System.err.println("If 3rd argument not provided then provide" + " Twitter users through database.");
        System.exit(-1);/*from   www  .j av a 2  s  .c  o  m*/
    }

    MysqlDB DB = new MysqlDB();
    AppOAuth AppOAuths = new AppOAuth();
    Misc helpers = new Misc();
    String endpoint = "/friends/ids";

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

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

    int IDS_TO_FETCH = 0;
    if (IDS_TO_FETCH_INT > 5000) {

        float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000;
        IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F);
    } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) {
        IDS_TO_FETCH = 1;
    }

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

    try {

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint);
        Twitter twitter = tf.getInstance();

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

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

        IDs ids;
        System.out.println("Listing friends ids.");

        // if targetedUser not provided by argument, then look into database.
        if (StringUtils.isEmpty(targetedUser)) {

            String selectQuery = "SELECT * FROM `followings_parent` WHERE " + "`targeteduser` != '' AND "
                    + "`nextcursor` != '0' AND " + "`nextcursor` != '2'";

            ResultSet results = DB.selectQ(selectQuery);

            int numRows = DB.numRows(results);
            if (numRows < 1) {
                System.err.println("No User in database to get friendsIDS");
                System.exit(-1);
            }

            OUTERMOST: while (results.next()) {

                int following_parent_id = results.getInt("id");
                targetedUser = results.getString("targeteduser");
                long cursor = results.getLong("nextcursor");
                System.out.println("Targeted User: " + targetedUser);

                int idsLoopCounter = 0;
                int totalIDs = 0;

                // put idsJSON in a file
                PrintWriter writer = new PrintWriter(OutputDirPath + "/" + targetedUser, "UTF-8");

                // call different functions for screen_name and id_str
                Boolean chckedNumaric = helpers.isNumeric(targetedUser);

                do {
                    ids = null;
                    try {

                        if (chckedNumaric) {

                            long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                            ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor);
                        } else {
                            ids = twitter.getFriendsIDs(targetedUser, cursor);
                        }

                    } catch (TwitterException te) {

                        // do not throw if user has protected tweets, 
                        // or if they deleted their account
                        if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                                || te.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                            System.out.println(targetedUser + " is protected or account is deleted");
                        } else {
                            System.out.println("Friends Get Exception: " + te.getMessage());
                        }

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

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

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

                            RemainingCalls = AppOAuths.RemainingCalls;
                            RemainingCallsCounter = 0;

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

                        // update cursor in "followings_parent"
                        String fieldValues = "`nextcursor` = 2";
                        String where = "id = " + following_parent_id;
                        DB.Update("`followings_parent`", fieldValues, where);

                        // If error then switch to next user
                        continue OUTERMOST;
                    }

                    if (ids.getIDs().length > 0) {

                        idsLoopCounter++;
                        totalIDs += ids.getIDs().length;
                        System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length);

                        JSONObject responseDetailsJson = new JSONObject();
                        JSONArray jsonArray = new JSONArray();
                        for (long id : ids.getIDs()) {
                            jsonArray.put(id);
                        }
                        Object idsJSON = responseDetailsJson.put("ids", jsonArray);

                        writer.println(idsJSON);
                    }

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

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

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

                        RemainingCalls = AppOAuths.RemainingCalls;
                        RemainingCallsCounter = 0;

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

                    if (IDS_TO_FETCH_INT != -1) {
                        if (idsLoopCounter == IDS_TO_FETCH) {
                            break;
                        }
                    }

                } while ((cursor = ids.getNextCursor()) != 0);
                writer.close();
                System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs);
                System.out.println();

                // update cursor in "followings_parent"
                String fieldValues = "`nextcursor` = " + cursor;
                String where = "id = " + following_parent_id;
                DB.Update("`followings_parent`", fieldValues, where);

            } // loop through every result found in db
        } else {

            // Second Argument Sets, so we are here.
            System.out.println("screen_name / user_id_str " + "passed by argument");

            int idsLoopCounter = 0;
            int totalIDs = 0;

            // put idsJSON in a file
            PrintWriter writer = new PrintWriter(
                    OutputDirPath + "/" + targetedUser + "_ids_" + helpers.getUnixTimeStamp(), "UTF-8");

            // call different functions for screen_name and id_str
            Boolean chckedNumaric = helpers.isNumeric(targetedUser);
            long cursor = -1;

            do {
                ids = null;
                try {

                    if (chckedNumaric) {

                        long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                        ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor);
                    } else {
                        ids = twitter.getFriendsIDs(targetedUser, cursor);
                    }

                } catch (TwitterException te) {

                    // do not throw if user has protected tweets, 
                    // or if they deleted their account
                    if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                            || te.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                        System.out.println(targetedUser + " is protected or account is deleted");
                    } else {
                        System.out.println("Friends Get Exception: " + te.getMessage());
                    }
                    System.exit(-1);
                }

                if (ids.getIDs().length > 0) {

                    idsLoopCounter++;
                    totalIDs += ids.getIDs().length;
                    System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length);

                    JSONObject responseDetailsJson = new JSONObject();
                    JSONArray jsonArray = new JSONArray();
                    for (long id : ids.getIDs()) {
                        jsonArray.put(id);
                    }
                    Object idsJSON = responseDetailsJson.put("ids", jsonArray);

                    writer.println(idsJSON);

                }

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

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

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

                    RemainingCalls = AppOAuths.RemainingCalls;
                    RemainingCallsCounter = 0;

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

                if (IDS_TO_FETCH_INT != -1) {
                    if (idsLoopCounter == IDS_TO_FETCH) {
                        break;
                    }
                }

            } while ((cursor = ids.getNextCursor()) != 0);
            writer.close();
            System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs);
            System.out.println();

        }

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

From source file:friendsandfollowers.FilesThreaderFriendsIDsParser.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  plus one optional " + "parameter to launch a Job.");
        System.err.println("First: String 'INPUT: DB or /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("Sixth: (int) Number of ids to fetch" + "Provide number which increment by 5000 "
                + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids.");
        System.err.println("Example: fileToRun /input/path/ " + "/output/path/ 10 1 3 75000");
        System.exit(-1);//  ww w  .ja v a2  s .  c om
    }

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

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

    int IDS_TO_FETCH_INT = -1;
    if (args.length == 6) {
        try {
            IDS_TO_FETCH_INT = Integer.parseInt(args[5]);
        } catch (NumberFormatException e) {
            System.err.println("Argument" + args[5] + " must be an integer.");
            System.exit(-1);
        }
    }

    int IDS_TO_FETCH = 0;
    if (IDS_TO_FETCH_INT > 5000) {

        float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000;
        IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F);
    } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) {
        IDS_TO_FETCH = 1;
    }

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

    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 screen names 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();

        // 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();

        IDs ids;
        System.out.println("Going to get friends ids.");

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

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

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

        for (String myFile : myFilesShare) {
            System.out.println("Going to parse file: " + myFile);

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

                    System.out.println("Going to get friends ids of Screen-name / user_id: " + line);
                    System.out.println();

                    String targetedUser = line.trim(); // tmp
                    long cursor = -1;
                    int idsLoopCounter = 0;
                    int totalIDs = 0;

                    PrintWriter writer = new PrintWriter(outputPath + "/" + targetedUser, "UTF-8");

                    // call different functions for screen_name and id_str
                    Boolean chckedNumaric = helpers.isNumeric(targetedUser);

                    do {
                        ids = null;
                        try {

                            if (chckedNumaric) {

                                long LongValueTargetedUser = Long.valueOf(targetedUser).longValue();

                                ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor);
                            } else {
                                ids = twitter.getFriendsIDs(targetedUser, cursor);
                            }

                        } catch (TwitterException te) {

                            // do not throw if user has protected tweets, or
                            // if they deleted their account
                            if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                                    || te.getStatusCode() == HttpResponseCode.NOT_FOUND) {

                                System.out.println(targetedUser + " is protected or account is deleted");
                            } else {
                                System.out.println("Friends Get Exception: " + te.getMessage());
                            }

                            // 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);
                            }

                            // Remove file if ids not found
                            if (totalIDs == 0) {

                                System.out.println("No ids fetched so removing " + "file " + targetedUser);

                                File fileToDelete = new File(outputPath + "/" + targetedUser);
                                fileToDelete.delete();
                            }
                            System.out.println();

                            // If error then switch to next user
                            continue OUTERMOST;
                        }

                        if (ids.getIDs().length > 0) {

                            idsLoopCounter++;
                            totalIDs += ids.getIDs().length;
                            System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length);

                            JSONObject responseDetailsJson = new JSONObject();
                            JSONArray jsonArray = new JSONArray();
                            for (long id : ids.getIDs()) {
                                jsonArray.put(id);
                            }
                            Object idsJSON = responseDetailsJson.put("ids", jsonArray);

                            writer.println(idsJSON);

                        }

                        // 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);
                        }

                        if (IDS_TO_FETCH_INT != -1) {
                            if (idsLoopCounter == IDS_TO_FETCH) {
                                break;
                            }
                        }

                    } while ((cursor = ids.getNextCursor()) != 0);

                    writer.close();
                    System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs);

                    // Remove file if ids not found
                    if (totalIDs == 0) {

                        System.out.println("No ids fetched so removing " + "file " + targetedUser);

                        File fileToDelete = new File(outputPath + "/" + targetedUser);
                        fileToDelete.delete();
                    }
                    System.out.println();

                } // while get records from single file
            } // read my single file
            catch (IOException e) {
                System.err.println("Failed to read lines from " + myFile);
            }

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

    } catch (TwitterException te) {
        // te.printStackTrace();
        System.err.println("Failed to get friends' ids: " + 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:gui.project2.v1.FXMLDocumentController.java

@FXML
public void searchloaction() throws InterruptedException {
    int maxFollowerCount = 0;
    userslocations = new ArrayList<>();
    nodes = new ArrayList<>();

    GraphicsContext gc = graph.getGraphicsContext2D();
    gc.setFill(Color.GAINSBORO);/*from   w  w  w . j av  a  2s .  c o m*/
    gc.fillRect(0, 0, 308, 308);
    Twitter twitter;
    twitter = tf.getInstance();
    ArrayList<User> users = new ArrayList<>();
    try {
        Query query = new Query("");

        GeoLocation location;
        location = new GeoLocation(parseDouble(latitude.getText()), parseDouble(longitude.getText()));
        Query.Unit unit = Query.KILOMETERS;
        query.setGeoCode(location, parseDouble(radius.getText()), unit);
        QueryResult result;

        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                boolean q = false;
                if (userslocations != null && !userslocations.isEmpty()) {
                    for (int i = 0; i < userslocations.size(); i++) {
                        if (userslocations.get(i).getName().equals(tweet.getUser().getScreenName())) {
                            q = true;
                            break;
                        }
                    }
                }

                if (!q && tweet.getGeoLocation() != null) {
                    pair n;
                    String latString = "";
                    String lonString = "";
                    int la = 0;
                    int lo = 0;
                    String geoString = tweet.getGeoLocation().toString();
                    for (int i = 0; i < geoString.length(); i++) {
                        if (geoString.charAt(i) == '=') {
                            if (la == 0) {
                                la = 1;
                            } else if (la == -1) {
                                lo = 1;
                            }
                        } else if (geoString.charAt(i) == ',') {
                            la = -1;
                        } else if (geoString.charAt(i) == '}') {
                            lo = -1;
                        } else if (la == 1) {
                            latString = latString + geoString.charAt(i);
                        } else if (lo == 1) {
                            lonString = lonString + geoString.charAt(i);
                        }
                    }
                    User thisUser;
                    thisUser = tweet.getUser();
                    double lat = parseDouble(latString);
                    double lon = parseDouble(lonString);
                    System.out.println(tweet.getGeoLocation().toString());
                    n = new pair(tweet.getUser().getScreenName(), lat, lon);
                    userslocations.add(n);
                    users.add(thisUser);
                    if (thisUser.getFollowersCount() > maxFollowerCount) {
                        maxFollowerCount = thisUser.getFollowersCount();
                    }
                }

            }
        } while ((query = result.nextQuery()) != null);
        for (int i = 0; i < users.size(); i++) {
            if (i % 14 == 0 && i != 0) {
                Thread.sleep(1000 * 60 * 15 + 30);
            }
            IDs friends;
            friends = twitter.getFriendsIDs(users.get(i).getId(), -1);
            for (long j : friends.getIDs()) {
                for (int k = i + 1; k < users.size(); k++) {
                    if (users.get(k).getId() == j) {
                        nodes.add(users.get(i).getScreenName() + ":" + users.get(k).getScreenName());
                    }
                }
            }
        }

    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    xmin = userslocations.get(0).getA();
    xmax = userslocations.get(0).getA();
    ymin = userslocations.get(0).getB();
    ymax = userslocations.get(0).getB();
    for (int i = 1; i < userslocations.size(); i++) {
        if (xmin > userslocations.get(i).getA()) {
            xmin = userslocations.get(i).getA();
        }
        if (xmax < userslocations.get(i).getA()) {
            xmax = userslocations.get(i).getA();
        }
        if (ymin > userslocations.get(i).getB()) {
            ymin = userslocations.get(i).getB();
        }
        if (ymax < userslocations.get(i).getB()) {
            ymax = userslocations.get(i).getB();
        }
    }
    for (int i = 0; i < userslocations.size(); i++) {
        if (userslocations.get(i).getA() - xmin >= 0 && userslocations.get(i).getB() - ymin >= 0) {
            gc.setLineWidth(users.get(i).getFollowersCount() / maxFollowerCount * 3 + 1);
            gc.strokeOval((userslocations.get(i).getA() - xmin) / (xmax - xmin) * 300 + 4,
                    (userslocations.get(i).getB() - ymin) / (ymax - ymin) * 300 + 4, 4, 4);
        }

    }
    ObservableList<String> usersLeftList = FXCollections.observableArrayList();
    for (int i = 0; i < users.size() - 1; i++) {
        User k = null;
        for (int j = i + 1; j < users.size(); j++) {
            if (users.get(j).getFollowersCount() > users.get(i).getFollowersCount()) {
                k = users.get(i);
                users.set(i, users.get(j));
                users.set(j, k);
            }
        }
    }
    for (int i = 0; i < users.size() / 5; i++) {
        usersLeftList.add(users.get(i).getScreenName() + " " + users.get(i).getFollowersCount());
    }
    listView.setItems(usersLeftList);
    gc.setLineWidth(1);
    gc.setFill(Color.BLUE);
    for (int i = 0; i < nodes.size(); i++) {
        String user1 = "";
        String user2 = "";
        int p = 0;
        double x1 = 0;
        double x2 = 0;
        double y1 = 0;
        double y2 = 0;
        for (int j = 0; j < nodes.get(i).length(); j++) {
            if (nodes.get(i).charAt(j) == ':') {
                p = 1;
            } else if (p == 0) {
                user1 = user1 + nodes.get(i).charAt(j);
            } else if (p == 1) {
                user2 = user2 + nodes.get(i).charAt(j);
            }
        }
        for (int j = 0; j < userslocations.size(); j++) {
            if (userslocations.get(j).getName().equals(user1)) {
                x1 = (userslocations.get(j).getA() - xmin) / (xmax - xmin) * 300 + 6;
                y1 = (userslocations.get(j).getB() - ymin) / (ymax - ymin) * 300 + 6;
            } else if (userslocations.get(j).getName().equals(user2)) {
                x2 = (userslocations.get(j).getA() - xmin) / (xmax - xmin) * 300 + 6;
                y2 = (userslocations.get(j).getB() - ymin) / (ymax - ymin) * 300 + 6;
            }
        }
        gc.strokeLine(x1, y1, x2, y2);
        gc.fillOval(x1 - 2, y1 - 2, 4, 4);
        gc.fillOval(x2 - 2, y2 - 2, 4, 4);
    }
}

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);
    }/*  w  w w .  j a  va 2 s. co m*/

    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;
}