com.sangupta.twittercli.command.Users.java Source code

Java tutorial

Introduction

Here is the source code for com.sangupta.twittercli.command.Users.java

Source

/**
 *
 * twittercli - Command line power tool for Twitter
 * Copyright (c) 2014, Sandeep Gupta
 * 
 * http://sangupta.com/projects/twittercli
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.sangupta.twittercli.command;

import io.airlift.command.Arguments;
import io.airlift.command.Command;

import java.util.List;

import com.google.gson.FieldNamingPolicy;
import com.sangupta.jerry.http.WebInvoker;
import com.sangupta.jerry.http.WebRequest;
import com.sangupta.jerry.http.WebRequestMethod;
import com.sangupta.jerry.http.WebResponse;
import com.sangupta.jerry.util.AssertUtils;
import com.sangupta.jerry.util.GsonUtils;
import com.sangupta.satya.user.impl.TwitterUserProfile;
import com.sangupta.twittercli.TwitterCommand;

@Command(name = "users", description = "Display profile info for given users")
public class Users extends TwitterCommand {

    @Arguments(description = "List of users to fetch profile for")
    public List<String> users;

    @Override
    public void doCommand() {
        if (AssertUtils.isEmpty(users)) {
            System.out.println("Atleast one user must be specified... exiting!");
            return;
        }

        String userStr = "";
        if (users.size() > 1) {
            for (int index = 0; index < users.size(); index++) {
                if (index > 0) {
                    userStr += ",";
                }
                userStr += users.get(index).trim();
            }
        } else {
            userStr = users.get(0);
        }

        WebRequest request = WebInvoker.getWebRequest(
                "https://api.twitter.com/1.1/users/lookup.json?screen_name=" + userStr, WebRequestMethod.GET);
        user.signRequest(request);
        WebResponse response = WebInvoker.executeSilently(request);
        TwitterUserProfile[] profiles = GsonUtils.getGson(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .fromJson(response.getContent(), TwitterUserProfile[].class);

        for (TwitterUserProfile profile : profiles) {
            System.out.println("ID: " + profile.getId());
            System.out.println("Since: " + profile.getCreatedAt());
            System.out.println("Screen Name: " + profile.getScreenName());
            System.out.println("Name: " + profile.getName());
            System.out.println("Followers: " + profile.getFollowersCount());
            System.out.println("Following: " + profile.getFriendsCount());
            System.out.println("Listed: " + profile.getListedCount());
            System.out.println("Favorites: " + profile.getFavouritesCount());
            System.out.println("Tweets: " + profile.getStatusesCount());
            System.out.println("Email: " + profile.getEmail());
            System.out.println("Bio: " + profile.getDescription());
            System.out.println("Location: " + profile.getLocation());
            System.out.println("URL: " + profile.getProfileLink());
            System.out.println();
        }
    }

}