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:com.happy_coding.viralo.twitter.FriendDiscoverer.java

License:Apache License

/**
 * Returns a list of contacts which follow the provided contact.
 *
 * @param forContact// ww  w.jav a 2  s. c  o  m
 * @return
 */
public List<Contact> findFollowers(Contact forContact) {
    List<Contact> contacts = new ArrayList<Contact>();
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        IDs list = twitter.getFollowersIDs(forContact.getUid(), -1);
        do {

            for (long id : list.getIDs()) {
                logger.debug("follower id: " + id);
                Contact contact = new Contact(id);
                contact.setActiveUid(twitter.getId());
                contacts.add(contact);
            }
        } while (list.hasNext());
    } catch (TwitterException e) {
        logger.error("can't find followers", e);
        return null;
    }
    return contacts;
}

From source file:com.happy_coding.viralo.twitter.FriendDiscoverer.java

License:Apache License

/**
 * Returns the current authorized user as Contact.
 *
 * @return/*from   w  ww .  j a  v  a2  s. c  om*/
 */
public Contact returnMyself() {
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        Contact me = new Contact(twitter.getId());
        me.setName(twitter.getScreenName());
        return me;
    } catch (TwitterException e) {
        logger.error("can't get myself", e);
        return null;
    }
}

From source file:com.happy_coding.viralo.twitter.FriendFollower.java

License:Apache License

/**
 * Follows the friends.//from  ww w  .  j a v a 2s . co  m
 *
 * @param myContact
 * @return
 */
public Boolean follow(Contact myContact) {
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        logger.debug("Create friendship for " + myContact.getUid());
        twitter.createFriendship(myContact.getUid());
    } catch (TwitterException e) {
        logger.error("Can't follow contact " + myContact.getUid(), e);
        return Boolean.FALSE;
    }
    return Boolean.TRUE;
}

From source file:com.happy_coding.viralo.twitter.FriendFollower.java

License:Apache License

/**
 * Unfollows the contact./*from w w w.  jav a  2 s.c  om*/
 *
 * @param myContact
 * @return
 */
public Boolean unfollow(Contact myContact) {

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        logger.debug("remove friendship for " + myContact.getUid());
        twitter.destroyFriendship(myContact.getUid());
    } catch (TwitterException e) {
        logger.error("can't unfollow contact " + myContact.getUid(), e);
        return Boolean.FALSE;
    }
    return Boolean.TRUE;
}

From source file:com.happy_coding.viralo.twitter.SmartPoster.java

License:Apache License

/**
 * Returns some kind of trends, which is worth writing about.
 *
 * @return//from w  w  w.j a  v  a2  s.c om
 */
public String retrieveTrend() {

    Twitter twitter = new TwitterFactory().getInstance();

    try {
        Trends trends = twitter.getLocationTrends(TREND_WOEID);
        Trend[] trendsArr = trends.getTrends();
        logger.debug("size: " + trendsArr.length);

        int lengthCheck = 0;
        String matchedtrend = "";
        for (int i = 0; i < trendsArr.length; i++) {

            logger.debug("Trend: " + trendsArr[i].getName());

            if (trendsArr[i].getName().length() >= lengthCheck) {
                matchedtrend = trendsArr[i].getName();
                lengthCheck = trendsArr[i].getName().length();
            }
        }

        return matchedtrend;

    } catch (TwitterException e) {
        logger.error("can't retrieve trend", e);
        return null;
    }
}

From source file:com.happy_coding.viralo.twitter.SmartPoster.java

License:Apache License

/**
 * Returns tweet for keywords.// w w  w. j a  v  a 2  s . c o m
 *
 * @param keywords
 * @return
 */
public ContactTweet getTweet(String keywords) {

    Query query = new Query(keywords);
    query.setPage(1);
    query.setRpp(TWEETS_PER_PAGE);

    Twitter twitter = new TwitterFactory().getInstance();

    QueryResult result = null;

    try {
        result = twitter.search(query);
    } catch (TwitterException e) {
        logger.error("can't load tweet", e);
        return null;
    }

    /*
    just get the first tweet.
     */
    Tweet firstTweet = result.getTweets().get(0);

    Contact contact = new Contact(firstTweet.getFromUserId());
    contact.setName(firstTweet.getFromUser());

    ContactTweet contactTweet = new ContactTweet(contact, firstTweet.getText());

    return contactTweet;
}

From source file:com.happy_coding.viralo.twitter.SmartPoster.java

License:Apache License

/**
 * Posts a tweet./*from w  w  w  .  ja  v  a 2  s. c  o m*/
 *
 * @param tweet
 */
public Boolean postTweet(String tweet) {

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        if (tweet.length() > MAX_TWEET_LENGTH) {
            tweet = tweet.substring(0, MAX_TWEET_LENGTH);
        }

        twitter.updateStatus(tweet);
        return Boolean.TRUE;
    } catch (TwitterException e) {
        logger.error("can't post tweet", e);
        return Boolean.FALSE;
    }
}

From source file:com.happy_coding.viralo.twitter.SmartPoster.java

License:Apache License

/**
 * Returns mentions for user.//from  w ww.  j  ava  2 s. c  o m
 *
 * @return
 */
public List<ContactTweet> getMentions() {

    Twitter twitter = new TwitterFactory().getInstance();

    try {

        ResponseList<Status> mentions = twitter.getMentions();
        List<ContactTweet> contactTweets = new ArrayList<ContactTweet>();

        for (Status status : mentions) {

            Contact contact = new Contact();
            contact.setUid(status.getUser().getId());
            contact.setName(status.getUser().getScreenName());

            ContactTweet contactTweet = new ContactTweet(contact, status.getText());
            contactTweet.setTweetID(status.getId());
            contactTweets.add(contactTweet);
        }
        return contactTweets;

    } catch (TwitterException e) {
        logger.error("can't load mentions", e);
        return null;
    }
}

From source file:com.hyperkode.friendshare.activities.LoginActivity.java

License:Open Source License

private void authTwitter() {
    // Twitter mTwitter and RequestToken mRequestToken
    // are private members of this activity
    mTwitter = new TwitterFactory().getInstance();
    mRequestToken = null;//from  ww w . j  av  a  2 s. c om
    mTwitter.setOAuthConsumer(getString(R.string.TWITTER_CONSUMER_KEY),
            getString(R.string.TWITTER_CONSUMER_SECRET));
    String callbackURL = getString(R.string.TWITTER_CALLBACK_URL);
    try {
        mRequestToken = mTwitter.getOAuthRequestToken(callbackURL);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
    Intent i = new Intent(mThis, TwitterWebViewActivity.class);
    i.putExtra("URL", mRequestToken.getAuthenticationURL());
    startActivityForResult(i, TwitterWebViewActivity.TWITTER_AUTH);
}

From source file:com.hyperkode.friendshare.activities.TwitterList.java

License:Open Source License

private void setupTwitter() {
    mSettings = getSharedPreferences(PREFS_NAME, 0);
    accessToken = mSettings.getString("twitter_access_token", null);
    accessTokenSecret = mSettings.getString("twitter_access_token_secret", null);
    // Get Access Token and persist it  
    AccessToken at = new AccessToken(accessToken, accessTokenSecret);
    // initialize Twitter4J  
    twitter4j = new TwitterFactory().getInstance();
    twitter4j.setOAuthConsumer(getString(R.string.TWITTER_CONSUMER_KEY),
            getString(R.string.TWITTER_CONSUMER_SECRET));
    twitter4j.setOAuthAccessToken(at);/*  w  w  w . j a  v  a 2 s  .c om*/

}