Example usage for twitter4j Twitter createFriendship

List of usage examples for twitter4j Twitter createFriendship

Introduction

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

Prototype

User createFriendship(long userId) throws TwitterException;

Source Link

Document

Allows the authenticating users to follow the user specified in the ID parameter.
Returns the befriended user in the requested format when successful.

Usage

From source file:kerguelenpetrel.FriendSomeoneServlet.java

License:Apache License

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    User friend = null;/*from  ww w  .  j  av  a 2 s. c  o m*/
    resp.setContentType("text/plain; charset=UTF-8");
    try {
        //Get the Twitter object
        Twitter twit = TwitterFactory.getSingleton();

        //Find a friend of a follower to bother
        long[] followerIDs = twit.getFollowersIDs(twit.getId(), cursor, 30).getIDs();

        if (followerIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers \n");
            return;
        }

        //Load the potential victim IDs
        long[] friendIDs = twit.getFollowersIDs(followerIDs[r.nextInt(followerIDs.length)], cursor).getIDs();

        if (friendIDs.length == 0) {
            resp.getWriter().println("Cannot find any followers to bother \n");
            return;
        }

        //Get a new friend
        friend = twit.showUser(friendIDs[r.nextInt(friendIDs.length)]);
        twit.createFriendship(friend.getId());
        resp.getWriter().println("Made a new friend with @" + friend.getScreenName());

        //Write to our new friend
        StatusUpdate status = new StatusUpdate(writeToFriend(friend.getScreenName(), resp));
        twit.updateStatus(status);
        resp.getWriter().println("Tweet posted: " + status.getStatus());
    } catch (TwitterException e) {
        resp.getWriter().println("Problem with Twitter \n");
        e.printStackTrace(resp.getWriter());
    }
}

From source file:twitter4j.examples.friendship.CreateFriendship.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendship.CreateFriendship [screen name]
 *
 * @param args message/*  w ww . j a  v  a  2s .c o  m*/
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.friendship.CreateFriendship [screen name]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.createFriendship(args[0]);
        System.out.println("Successfully followed [" + args[0] + "].");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to follow: " + te.getMessage());
        System.exit(-1);
    }
}