Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

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

Prototype

public TwitterFactory(String configTreePath) 

Source Link

Document

Creates a TwitterFactory with a specified config tree

Usage

From source file:com.aremaitch.codestock2010.library.TwitterLib.java

License:Apache License

private void buildObject(String consumerKey, String consumerSecret, String accessToken,
        String accessTokenSecret) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey).setDebugEnabled(true).setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);

    Configuration config = cb.build();
    t = new TwitterFactory(config).getInstance();
}

From source file:com.arihant15.ActionServlet.java

@RequestMapping("/login.arihant15")
public void doLogin(HttpServletRequest req, HttpServletResponse res) {
    try {// w  w  w. j  a v  a 2 s  .  c o m
        ConfigurationBuilder configuration = new ConfigurationBuilder();
        configuration.setOAuthConsumerKey(ConsumerKey);
        configuration.setOAuthConsumerSecret(ConsumerSecret);
        TwitterFactory twitterfactory = new TwitterFactory(configuration.build());
        Twitter twitter = twitterfactory.getInstance();
        req.getSession().setAttribute("t", twitter);
        RequestToken requestToken = twitter.getOAuthRequestToken();
        req.getSession().setAttribute("rToken", requestToken);
        res.sendRedirect(requestToken.getAuthenticationURL());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.avishkar.NewGetFollowersIDs.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendsandfollowers.GetFollowersIDs
 * [screen name]/*from   w w w.  j a v a  2s  .  co m*/
 *
 * @param args
 *            message
 * @throws TwitterException
 * @throws InterruptedException
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static void main(String[] args)
        throws TwitterException, InterruptedException, IOException, ClassNotFoundException {
    Twitter twitter = new TwitterFactory(AccessTokenUtil.getConfig()).getInstance();
    DBAccess.setDbName("suhasn");

    HashSet<Long> fetchedIds = new HashSet<>();
    HashSet<Long> pendingIds = new HashSet<>();
    HashSet<Long> currentLevel = new HashSet<>();
    File fileExistsTestHandle = new File(pendingIdListFile);
    currentLevel.add((long) 39703979);
    for (int level = 1; level <= 2; level++) {
        System.out.println("Iteration " + level + ".users in level:" + currentLevel.size());
        pendingIds.clear();
        for (Long id : currentLevel) {
            List<Long> filteredUserListOnStatusCount = getUserFollwers(id);
            fetchedIds.add(id);
            writeHashSet(retrivedIdFile, fetchedIds);
            if (filteredUserListOnStatusCount == null)
                continue;
            System.out
                    .println("No of followers whose follwers < 100 : " + filteredUserListOnStatusCount.size());
            for (Long filteredId : filteredUserListOnStatusCount) {
                if (!fetchedIds.contains(filteredId))
                    pendingIds.add(filteredId);
                writeHashSet(pendingIdListFile, pendingIds);
            }
        }
        currentLevel.clear();
        currentLevel.addAll(pendingIds);
        System.out.println("Pending Ids Size:" + pendingIds.size());
        System.out.println("Fetched Ids Size:" + fetchedIds.size());
    }
}

From source file:com.avishkar.NewGetFollowersIDs.java

License:Apache License

private static List<Long> getUserFollwers(long twitterUserId)
        throws UnknownHostException, TwitterException, InterruptedException {
    Twitter twitter = new TwitterFactory(AccessTokenUtil.getConfig()).getInstance();
    List<Long> filteredUserListOnFollowerCount = new ArrayList<Long>();
    final Gson gson = new Gson();
    try {/*from  ww w . j a  v a  2 s  .c  o  m*/

        checkRateLimit("/followers/ids", twitter);
        System.out.println("Listing followers's Follower for ID:" + twitterUserId + System.lineSeparator());
        IDs followerIDs = twitter.getFollowersIDs(twitterUserId, -1);
        JsonObject followerJSON = new JsonObject();

        followerJSON.addProperty("id", twitterUserId);
        followerJSON.addProperty("followers", gson.toJson(followerIDs.getIDs()));
        DBAccess.insert(gson.toJson(followerJSON));

        System.out.println("Current Followers Fetched Size:" + followerIDs.getIDs().length);
        // Filtering for influential user
        if (followerIDs.getIDs().length > 1000) {
            System.out.println("User assumed as Influential");
            return null;
        }
        int from = 0;
        int to = 99;
        int limit = checkRateLimit("/users/lookup", twitter);
        while (from < followerIDs.getIDs().length) {
            if (to > followerIDs.getIDs().length)
                to = followerIDs.getIDs().length - 1;
            if (limit == 0)
                checkRateLimit("/users/lookup", twitter);
            ResponseList<User> followers = twitter
                    .lookupUsers(Arrays.copyOfRange(followerIDs.getIDs(), from, to));
            System.out.println("Recieved User count:" + followers.size());
            for (User user : followers) {
                DBAccess.insertUser(gson.toJson(user));
                if (user.getFollowersCount() < 1000) {
                    // if(user.getStatusesCount()>0 &&
                    // user.getStatus()!=null &&
                    // sevenDaysAgo.after(user.getStatus().getCreatedAt()))
                    filteredUserListOnFollowerCount.add(user.getId());
                    getStatuses(user.getScreenName(), twitter);
                } else
                    System.out.println("User " + user.getScreenName()
                            + " is pruned for Influential or over subscription." + " Follower count:"
                            + user.getFollowersCount() + " Friends Count:" + user.getFriendsCount());
            }

            from += 100;
            to += 100;
            limit--;
        }
    } catch (TwitterException te) {
        if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED
                || te.getStatusCode() == HttpResponseCode.NOT_FOUND) {
            System.out.println("Encountered locked profile. Skipping " + twitterUserId);
            return null;

            // log something here
        }
        te.printStackTrace();
        System.out.println("Failed to get followers' Follower: " + te.getMessage());
        // System.exit(-1);
    }

    return filteredUserListOnFollowerCount;
}

From source file:com.bordengrammar.bordengrammarapp.TwitterActivity.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twitter);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setTranslucentStatus(true);//  w  ww .j a  v a 2s.  c  om
    }
    SystemBarTintManager tintManager = new SystemBarTintManager(this);
    tintManager.setStatusBarTintEnabled(true);
    tintManager.setStatusBarTintResource(R.color.bordenpurple);
    FeedbackSettings feedbackSettings = new FeedbackSettings();
    feedbackSettings.setCancelButtonText("Cancel");
    feedbackSettings.setSendButtonText("Send");
    feedbackSettings.setText("Send feedback to improve the app");
    feedbackSettings.setTitle("Feedback");
    feedbackSettings.setToast("We value your feedback");
    feedBack = new FeedbackDialog(this, "AF-186C1F794D93-1A", feedbackSettings);
    ActionBar actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(true);
    PACKAGE_NAME = getApplicationContext().getPackageName();

    //Now lets get down into the twitter

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Toqp03fcUErG5P8e9nhfsw")
            .setOAuthConsumerSecret("SEqktstO9h7SqSm7zmcuWlH3bOtElJm1Ds2TFSwFBc")
            .setOAuthAccessToken("2245935685-U5LMfl4oEcOv6Khw58JZqRdcH2PlABEeUP2JeXj")
            .setOAuthAccessTokenSecret("uFcGRpCx8aGXdv3AiAkfVImnoLrlNNCUnZ2UtE76Zbnpa");

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    List<Status> statuses = null;
    String user;
    user = "bordengrammar";
    try {
        statuses = twitter.getUserTimeline(user);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
    assert statuses != null;
    twitter4j.Status status1 = statuses.get(0);
    Format formatter = new SimpleDateFormat("MMM d, K:mm");
    savePrefs("twitter", status1.getText());
    savePrefs("twittertime", formatter.format(status1.getCreatedAt()));
    twitter4j.Status status2 = statuses.get(1);
    twitter4j.Status status3 = statuses.get(2);
    twitter4j.Status status4 = statuses.get(3);
    twitter4j.Status status5 = statuses.get(4);
    twitter4j.Status status6 = statuses.get(5);
    twitter4j.Status status7 = statuses.get(6);
    twitter4j.Status status8 = statuses.get(7);
    twitter4j.Status status9 = statuses.get(8);
    twitter4j.Status status10 = statuses.get(9);
    TextView time1 = (TextView) findViewById(R.id.time1);
    TextView time2 = (TextView) findViewById(R.id.time2);
    TextView time3 = (TextView) findViewById(R.id.time3);
    TextView time4 = (TextView) findViewById(R.id.time4);
    TextView time5 = (TextView) findViewById(R.id.time5);
    TextView time6 = (TextView) findViewById(R.id.time6);
    TextView time7 = (TextView) findViewById(R.id.time7);
    TextView time8 = (TextView) findViewById(R.id.time8);
    TextView time9 = (TextView) findViewById(R.id.time9);
    TextView time10 = (TextView) findViewById(R.id.time10);
    time1.setText(formatter.format(status1.getCreatedAt()));
    time2.setText(formatter.format(status2.getCreatedAt()));
    time3.setText(formatter.format(status3.getCreatedAt()));
    time4.setText(formatter.format(status4.getCreatedAt()));
    time5.setText(formatter.format(status5.getCreatedAt()));
    time6.setText(formatter.format(status6.getCreatedAt()));
    time7.setText(formatter.format(status7.getCreatedAt()));
    time8.setText(formatter.format(status8.getCreatedAt()));
    time9.setText(formatter.format(status9.getCreatedAt()));
    time10.setText(formatter.format(status10.getCreatedAt()));
    TextView text1 = (TextView) findViewById(R.id.text1);
    TextView text2 = (TextView) findViewById(R.id.text2);
    TextView text3 = (TextView) findViewById(R.id.text3);
    TextView text4 = (TextView) findViewById(R.id.text4);
    TextView text5 = (TextView) findViewById(R.id.text5);
    TextView text6 = (TextView) findViewById(R.id.text6);
    TextView text7 = (TextView) findViewById(R.id.text7);
    TextView text8 = (TextView) findViewById(R.id.text8);
    TextView text9 = (TextView) findViewById(R.id.text9);
    TextView text10 = (TextView) findViewById(R.id.text10);
    text1.setText(readPrefs("twitter"));
    text2.setText(status2.getText());
    text3.setText(status3.getText());
    text4.setText(status4.getText());
    text5.setText(status5.getText());
    text6.setText(status6.getText());
    text7.setText(status7.getText());
    text8.setText(status8.getText());
    text9.setText(status9.getText());
    text10.setText(status10.getText());
    /* i think i could have used a for loop... oh well i typed it all already. */
}

From source file:com.buddycloud.friendfinder.provider.Twitter.java

License:Apache License

@Override
public ContactProfile getProfile(String accessToken, String accessTokenSecret) throws Exception {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(getProperties().getProperty("twitter.consumerKey"))
            .setOAuthConsumerSecret(getProperties().getProperty("twitter.consumerSecret"))
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter4j.Twitter twitter = tf.getInstance();

    Long myId = twitter.getId();/*from w ww. j av  a2  s  . c o  m*/
    ContactProfile contactProfile = new ContactProfile(HashUtils.encodeSHA256(PROVIDER_NAME, myId.toString()));
    long nextCursor = -1;
    while (true) {
        IDs friendsIDs = twitter.getFollowersIDs(nextCursor);
        for (Long friendId : friendsIDs.getIDs()) {
            contactProfile.addFriendHash(HashUtils.encodeSHA256(PROVIDER_NAME, friendId.toString()));
        }
        if (!friendsIDs.hasNext()) {
            break;
        }
        nextCursor = friendsIDs.getNextCursor();
    }
    return contactProfile;
}

From source file:com.cruta.hecas.twitter.MyTwitter.java

public void Tweet() {
    try {//from  w w w .j  av  a 2  s.  com

        System.out.println("voy a enviar");
        Twitter twitter;
        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setDebugEnabled(true).setOAuthConsumerKey("4W5InvFtHgtZAQBa6QQeQFWlA")
                .setOAuthConsumerSecret("t3nCC1RKhAcqjZPoHmF4Osa2VzY9kTBt0d4XAn00H49jXHtfgT")
                .setOAuthAccessToken("15616431-NXjouNhzTBYQYQqRHPJnMKakh0RyZSHvhss7RYrTm")
                .setOAuthAccessTokenSecret("gC1hIbqXOU8QYjjcyYKL8P2uW2wVzSu3X5TfBO3iMqdo2");
        twitter = new TwitterFactory(cb.build()).getInstance();

        Paging pagina = new Paging();

        Status tweetEscrito = twitter.updateStatus("Holas desde hecas");
        System.out.println("twett enviado");
    } catch (Exception ex) {
        JSFUtil.errorDialog("Tweet()", ex.getLocalizedMessage());
        System.out.println("error() " + ex.getLocalizedMessage());
    }
}

From source file:com.cruta.hecas.twitter.MyTwitter.java

public void Tweet(String mensaje) {
    try {//w w  w.  j  ava2 s . c  o m

        Twitter twitter;
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("4W5InvFtHgtZAQBa6QQeQFWlA")
                .setOAuthConsumerSecret("t3nCC1RKhAcqjZPoHmF4Osa2VzY9kTBt0d4XAn00H49jXHtfgT")
                .setOAuthAccessToken("15616431-NXjouNhzTBYQYQqRHPJnMKakh0RyZSHvhss7RYrTm")
                .setOAuthAccessTokenSecret("gC1hIbqXOU8QYjjcyYKL8P2uW2wVzSu3X5TfBO3iMqdo2");
        twitter = new TwitterFactory(cb.build()).getInstance();

        Paging pagina = new Paging();

        Status tweetEscrito = twitter.updateStatus(mensaje);
        System.out.println("twett enviado");
    } catch (Exception ex) {
        JSFUtil.errorDialog("Tweet()", ex.getLocalizedMessage());
        System.out.println("tweet() " + ex.getLocalizedMessage());
    }
}

From source file:com.daemon.TwitterProfile.java

License:Open Source License

public TwitterProfile(String name, String screenName, String consumerKey, String consumerSecret,
        String accessToken, String accessTokenSecret, DaemonProperties properties) {
    _profileName = name;/*ww  w . j  av a 2  s.co  m*/
    _screenName = screenName;
    _consumerKey = consumerKey;
    _consumerSecret = consumerSecret;
    _accessToken = accessToken;
    _accessTokenSecret = accessTokenSecret;
    _props = properties;

    // Create Twitter object
    ConfigurationBuilder conf = new ConfigurationBuilder();
    conf.setOAuthAccessToken(getAccessToken()).setOAuthAccessTokenSecret(getAccessTokenSecret())
            .setOAuthConsumerKey(getConsumerKey()).setOAuthConsumerSecret(getConsumerSecret());
    _twitter = new TwitterFactory(conf.build()).getInstance();
}

From source file:com.daiv.android.twitter.utils.Utils.java

License:Apache License

public static Twitter getTwitter(Context context, AppSettings settings) {
    if (settings == null) {
        settings = AppSettings.getInstance(context);
    }//from www. j av  a  2  s .  co m
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(AppSettings.TWITTER_CONSUMER_KEY)
            .setOAuthConsumerSecret(AppSettings.TWITTER_CONSUMER_SECRET)
            .setOAuthAccessToken(settings.authenticationToken)
            .setOAuthAccessTokenSecret(settings.authenticationTokenSecret);
    TwitterFactory tf = new TwitterFactory(cb.build());
    return tf.getInstance();
}