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

Source Link

Document

Creates a TwitterFactory with the root configuration.

Usage

From source file:DestroyFriendship.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendship.DestroyFriendship [screen name]
 *
 * @param args message//w w w  .j  a  v a  2  s  . co m
 */
public void destroyFriendship() {

    ShowFriendshi df = new ShowFriendshi();

    ArrayList<Long> getFilteredtarget = df.showFriendship();

    try {
        Twitter twitter = new TwitterFactory().getInstance();
        User user;
        String get;
        DefaultListModel m1 = new DefaultListModel();
        for (Long target : getFilteredtarget) {

            user = twitter.showUser(target);
            get = user.getScreenName();

            if (!get.equalsIgnoreCase("prometheanBrain") && !get.equalsIgnoreCase("tweetrackdevs")) {
                m1.addElement(get);
                twitter.destroyFriendship(target);
            }

            System.out.println("Successfully unfollowed [" + target + "].");
        }
        MainActivity.deletedList.setModel(m1);

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to unfollow: " + te.getMessage());

    }
}

From source file:GetFriendsIDs.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendsandfollowers.GetFriendsIDs [screen name]
 *
 * @param args message/*from  w  w  w.  j  av  a  2 s. c o m*/
 */
public ArrayList<Long> getFriendsId() {
    ArrayList<Long> friendsid = new ArrayList<Long>();
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing following ids.");

        do {

            ids = twitter.getFriendsIDs(cursor);

            for (long id : ids.getIDs()) {

                friendsid.add(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.out.println("done");
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get friends' ids: " + te.getMessage());

    }

    return friendsid;
}

From source file:TwitterModel.java

public TwitterModel() {
    twitter = new TwitterFactory().getInstance();
}

From source file:ShowFriendshi.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendship.ShowFriendship  [source screen name] [target screen name]
 *
 * @param args message//from  www. j  a v a2 s  .c o m
 */
public ArrayList<Long> showFriendship() {
    GetFriendsIDs getFriendsId = new GetFriendsIDs();
    ArrayList<Long> target = getFriendsId.getFriendsId();
    ArrayList<Long> filteredTarget = new ArrayList<Long>();
    Collections.shuffle(target);
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        int authenticatedUserId = (int) twitter.getId();
        if (target.size() < 50) {

            for (int i = 0; i < target.size(); i++) {
                Relationship relationship = twitter.showFriendship(authenticatedUserId, target.get(i));
                if (!relationship.isSourceFollowedByTarget()) {

                    filteredTarget.add(target.get(i));
                }

            }

        } else {
            for (int i = 0; i < 50; i++) {
                Relationship relationship = twitter.showFriendship(authenticatedUserId, target.get(i));
                if (!relationship.isSourceFollowedByTarget()) {

                    filteredTarget.add(target.get(i));
                }

            }

        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to show friendship: " + te.getMessage());

    }

    return filteredTarget;
}

From source file:au.com.infiniterecursion.hashqanda.MainActivity.java

private void showTweetDialog() {

    app.setShowing_tweet_dialog(true);/*from   w w w.j a  va2 s  . com*/

    // Launch Tweet Edit View
    LayoutInflater inflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final View tweet_dialog_view = inflater.inflate(R.layout.tweet_dialog, null);

    final EditText tweet_edittext = (EditText) tweet_dialog_view.findViewById(R.id.EditTextTweet);

    final Dialog d = new Dialog(this);
    Window w = d.getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    d.setTitle(R.string.tweet_dialog_title);
    // the edit layout defined in an xml file (in res/layout)
    d.setContentView(tweet_dialog_view);
    // Cancel
    Button cbutton = (Button) d.findViewById(R.id.button2Cancel);
    cbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            app.setShowing_tweet_dialog(false);
            d.dismiss();
        }
    });
    // Edit
    Button ebutton = (Button) d.findViewById(R.id.button1Edit);
    ebutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // save title and description to DB.
            String tweet = tweet_edittext.getText().toString();

            int end = (tweet.length() > 133) ? 133 : tweet.length();
            String real_tweet = "#qanda " + tweet.substring(0, end);
            Log.d(TAG, "New tweet is " + tweet);
            Log.d(TAG, "real tweet is " + real_tweet);

            String twitterToken, twitterTokenSecret;
            String[] toks = twitterTokens();
            twitterToken = toks[0];
            twitterTokenSecret = toks[1];

            if (twitterToken != null && twitterTokenSecret != null) {

                // Ok, now we can tweet this URL
                AccessToken a = new AccessToken(twitterToken, twitterTokenSecret);
                Twitter twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(TwitterOAuthActivity.consumerKey, TwitterOAuthActivity.consumerSecret);
                twitter.setOAuthAccessToken(a);

                try {
                    twitter.updateStatus(real_tweet);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(MainActivity.this, R.string.tweet_posted, Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (TwitterException e) {
                    // 
                    e.printStackTrace();
                    Log.e(TAG, " twittering failed " + e.getMessage());
                }
            } else {

                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(MainActivity.this, R.string.tweet_not_posted, Toast.LENGTH_LONG).show();
                    }
                });

            }

            app.setShowing_tweet_dialog(false);
            d.dismiss();
        }
    });
    d.show();
}

From source file:au.com.infiniterecursion.vidiom.activity.MainActivity.java

public void finishedUploading(boolean success) {
    // not uploading anymore.

    // Auto twittering.
    ////w  w  w . j  av  a 2s .  c  om
    if (twitterPreference && success) {

        new Thread(new Runnable() {
            public void run() {

                // Check there is a hosted URL for a start..

                // XXX get which URL from user if more than one!!

                String[] hosted_urls_to_tweet = db_utils
                        .getHostedURLsFromID(new String[] { Long.toString(latestsdrecord_id) });

                Log.d(TAG, " checking " + hosted_urls_to_tweet[0] + " in auto twitter publishing");

                if (hosted_urls_to_tweet != null && hosted_urls_to_tweet[0] != null
                        && hosted_urls_to_tweet[0].length() > 0) {

                    // Check there is a valid twitter OAuth tokens.
                    String twitterToken = prefs.getString("twitterToken", null);
                    String twitterTokenSecret = prefs.getString("twitterTokenSecret", null);

                    if (twitterToken != null && twitterTokenSecret != null) {

                        // Ok, now we can tweet this URL
                        AccessToken a = new AccessToken(twitterToken, twitterTokenSecret);
                        Twitter twitter = new TwitterFactory().getInstance();
                        twitter.setOAuthConsumer(TwitterOAuthActivity.consumerKey,
                                TwitterOAuthActivity.consumerSecret);
                        twitter.setOAuthAccessToken(a);

                        String status = "New video:" + hosted_urls_to_tweet[0];
                        try {
                            twitter.updateStatus(status);
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    Toast.makeText(MainActivity.this, R.string.tweeted_ok, Toast.LENGTH_LONG)
                                            .show();
                                }
                            });
                        } catch (TwitterException e) {
                            //
                            e.printStackTrace();
                            Log.e(TAG, "Auto twittering failed " + e.getMessage());
                        }
                    }

                }

            }
        }).start();

    }

    mainapp.setNotUploading();
}

From source file:au.com.tyo.services.sn.twitter.SNTwitter.java

License:Apache License

public void createInstance() {
    synchronized (this) {
        try {//from  w w w  .  j a  v  a 2  s . co  m
            if (hasSecret()) {
                AccessToken accessToken = new AccessToken(secretOAuth.getToken().getToken(),
                        secretOAuth.getToken().getSecret());

                Configuration conf = new ConfigurationBuilder().setOAuthConsumerKey(consumerKey)
                        .setOAuthConsumerSecret(consumerKeySecret).setOAuthAccessToken(accessToken.getToken())
                        .setOAuthAccessTokenSecret(accessToken.getTokenSecret()).build();

                OAuthAuthorization auth = new OAuthAuthorization(conf);
                twitter = new TwitterFactory().getInstance(auth);
                authenticated = true;
            }
        } catch (Exception ex) {
            authenticated = false;
            twitter = null;
        }

        try {
            /*
             * it is better to use the id, because people would change their name
             */
            long sourceId = Integer.valueOf(secretOAuth.getId().getToken());
            user = twitter.showUser(sourceId);
            //              user = twitter.showUser(userInfo.getName());

            secretOAuth.getId().setToken(String.valueOf(user.getId()));
            secrets.save(secretOAuth.getId());

            userInfo.setName(user.getScreenName());
            alias.setName(user.getName());

            saveUserInfo();
            saveAlias();
            userProfileImageUrl = getUserAvatarUrl();
        } catch (Exception ex) {
        }
    }
}

From source file:au.com.tyo.services.sn.twitter.SNTwitter.java

License:Apache License

private Twitter getTwitter() {
    if (twitter == null)
        twitter = new TwitterFactory().getInstance();
    return twitter;
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Authenticate User to Twitter//  w w  w  .  j  ava 2 s. c  om
 * @param userToken
 * @param userSecret
 * @return OAuth authenticated instance of Twitter object
 */
public Twitter getTwitterAuthForUser(String userToken, String userSecret) {
    if (StringUtils.isBlank(userToken) || StringUtils.isBlank(userSecret)) {
        return null;
    }

    String cacheKey = userToken + "|" + userSecret;
    //String cacheKey = userToken;

    Element element = twitterCache.get(cacheKey);

    // caching authenticated oauth objects
    if (element == null) {
        synchronized (twitterCache) {
            // if it is still null after acquiring lock
            element = twitterCache.get(cacheKey);

            if (element == null) {
                // log.debug("cache miss: getting authorized instance for " + userToken);
                Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey, consumerSecret,
                        new AccessToken(userToken, userSecret));

                element = new Element(cacheKey, twitter);
                twitterCache.put(element);
            }
        }
        // } else {
        //     log.debug("cache hit: getting authorized instance for " + userToken);
    }

    return (Twitter) element.getObjectValue();
}

From source file:au.edu.anu.portal.portlets.tweetal.logic.TwitterLogic.java

License:Apache License

/**
 * Set consumer key and secret, and get OAuth authorized instance
 *//*  www. jav  a  2  s .c o  m*/
public void setOAuthConsumer(String consumerKey, String consumerSecret) {
    this.consumerKey = consumerKey;
    this.consumerSecret = consumerSecret;

    if (StringUtils.isEmpty(consumerKey) || StringUtils.isEmpty(consumerSecret)) {
        // no key in session, no access
        log.error("Empty consumer secret and key");
        twitterConsumer = null;
        return;
    }

    twitterConsumer = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey, consumerSecret);
}