Manage Twitter List Subscription - Java Social Media

Java examples for Social Media:Twitter

Description

Manage Twitter List Subscription

Demo Code

import twitter4j.*;
import twitter4j.auth.AccessToken;

public class ManageListSubscription {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.exit(-1);/*w w  w.j a va  2  s  . c o  m*/
        }
        Twitter twitter = new TwitterFactory().getInstance();
        String consumerKey = "a";
        String consumerSecret = "a";
        String accessToken = "a";
        String accessTokenSecret = "a";

        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken,
                accessTokenSecret));

        String screenName = args[0];

        try {
            if (args.length == 3) {
                String command = args[1];
                int listId = Integer.parseInt(args[2]);
                if ("subscribe".equals(command)) {
                    twitter.subscribeUserList(screenName, listId);
                    System.out.println("id:" + listId);
                } else if ("unsubscribe".equals(command)) {
                    twitter.unsubscribeUserList(screenName, listId);
                    System.out.println("id:" + listId);
                }
            }
            System.out.println("@" + screenName);
            PagableResponseList<UserList> lists = twitter.getUserLists(
                    screenName, -1l);
            for (UserList list : lists) {
                boolean subscribing = false;
                try {
                    twitter.checkUserListSubscription(screenName,
                            list.getId(), twitter.getId());
                    subscribing = true;
                } catch (TwitterException ignore) {
                }
                System.out.print(subscribing);
                System.out.println(list.getName() + " - "
                        + list.getDescription() + "(" + list.getId() + ")");
            }
        } catch (TwitterException te) {
            System.out.println(te.getMessage());
        }
    }
}

Related Tutorials