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:au.net.moon.tUtils.twitterAuthorise.java

License:Open Source License

/**
 * @param streamConnection/* w  ww .j  a  v  a  2 s  .  c om*/
 *            <CODE>True</CODE> if connection is to streamAPI,
 *            <CODE>False</CODE> otherwise
 */
public twitterAuthorise(Boolean streamConnection) {
    if (!streamConnection) {
        if (twitter == null) {
            System.out.println("TwitterAuthorise: Connecting to Twitter with OAuth");
            twitter = new TwitterFactory().getInstance();
            try {
                System.out.println("TwitterAuthorise: rate limit status: " + twitter.getRateLimitStatus());
            } catch (TwitterException e) {
                System.out.println("TwitterAuthorise: Failed to get rate limit status");
                e.printStackTrace();
            }
        }
    } else {
        if (twitterStream == null) {
            System.out.println("TwitterAuthorise: Connecting to Twitter Stream with OAuth");
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setUseSSL(true);
            Configuration conf = builder.build();
            twitterStream = new TwitterStreamFactory(conf).getInstance();
            System.out.println(
                    "TwitterAuthorise: Stream authorisation status: " + twitterStream.getAuthorization());

        }
    }
}

From source file:blg.bhdrkn.wicket.twitter.TwitterPage.java

License:Apache License

private void addTweetForm() {
    tweetForm = new Form<Void>("tweetForm");
    tweetArea = new TextArea<String>("tweetArea", new PropertyModel<String>(this, "tweetModel"));
    tweetArea.setOutputMarkupId(true);//from  w  ww.  j a va 2s.  c  o  m
    tweetForm.add(tweetArea);
    tweetForm.add(new AjaxButton("updateStatus") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            try {
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(SocialMedia.TWITTER_CONSUMER_KEY, SocialMedia.TWITTER_CONSUMER_SECRET);
                twitter.setOAuthAccessToken(token);
                twitter.updateStatus(tweetModel);
                target.appendJavaScript("alert('Status Updated: " + tweetModel + "');");
                tweetModel = "";
                target.add(tweetArea);
            } catch (TwitterException e) {
                logger.error("Error While Updating the Status");
                e.printStackTrace();
            }
        }

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            logger.error("Error Before Updating the Status");
        }
    });
    add(tweetForm);
}

From source file:blg.bhdrkn.wicket.twitter.TwitterPanel.java

License:Apache License

private void createTwitter() {
    try {//from   w  ww  . j  av  a 2 s . com
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(SocialMedia.TWITTER_CONSUMER_KEY, SocialMedia.TWITTER_CONSUMER_SECRET);
        requestToken = twitter.getOAuthRequestToken();
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}

From source file:bluevia.OAuth.java

License:Apache License

private String oAuthTwitter(HttpServletRequest req, String step) {
    String oAuthUrl = "/index.jsp";
    Properties prop = new Properties();

    prop.setProperty("oauth.consumerKey", TwitterOAuth.consumer_key);
    prop.setProperty("oauth.consumerSecret", TwitterOAuth.consumer_secret);

    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(TwitterOAuth.consumer_key, TwitterOAuth.consumer_secret);
    try {//from w w  w  . ja  v  a2  s.  co m

        if (step.compareTo("1") == 0) {

            RequestToken requestToken = twitter
                    .getOAuthRequestToken(Util.getCallbackDomain() + "/oauth?network=twitter&step=2");

            req.getSession().setAttribute("request_key", requestToken.getToken());
            req.getSession().setAttribute("request_secret", requestToken.getTokenSecret());

            oAuthUrl = requestToken.getAuthenticationURL();

        } else {

            String request_key = (String) req.getSession().getAttribute("request_key");
            String request_secret = (String) req.getSession().getAttribute("request_secret");

            String oauth_verifier = req.getParameter("oauth_verifier");

            RequestToken requestToken = new RequestToken(request_key, request_secret);
            AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, oauth_verifier);

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            Util.addNetworkAccount(user.getEmail(), "TwitterAccount", Util.TwitterOAuth.consumer_key,
                    Util.TwitterOAuth.consumer_secret, accessToken.getToken(), accessToken.getTokenSecret());

            req.getSession().removeAttribute("request_key");
            req.getSession().removeAttribute("request_secret");

            oAuthUrl = "/settings.jsp";
        }
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.severe(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        logger.severe(e.getMessage());
    }

    return oAuthUrl;
}

From source file:bluevia.SendSMS.java

License:Apache License

public static void setTwitterStatus(String userEmail, String tweet) {
    if (tweet != null) {
        try {/* w w w.  ja  v  a2  s  .  c o m*/
            Properties twitterAccount = Util.getNetworkAccount(userEmail, "TwitterAccount");

            if (twitterAccount != null) {
                String consumer_key = twitterAccount.getProperty("TwitterAccount.consumer_key");
                String consumer_secret = twitterAccount.getProperty("TwitterAccount.consumer_secret");
                String access_key = twitterAccount.getProperty("TwitterAccount.access_key");
                String access_secret = twitterAccount.getProperty("TwitterAccount.access_secret");

                Twitter twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumer_key, consumer_secret);
                twitter.setOAuthAccessToken(new AccessToken(access_key, access_secret));

                StatusUpdate status = new StatusUpdate(tweet);
                twitter.updateStatus(status);
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            log.severe(te.getMessage());
        } catch (Exception e) {
            log.severe(String.format("Error sending SMS: %s", e.getMessage()));
        }
    }
}

From source file:br.ufba.dcc.tagcloud.util.TwitterProvider.java

public void start(String user, int max) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
    AccessToken oathAccessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    twitter.setOAuthAccessToken(oathAccessToken);
    this.getTweets(twitter, user, max);
}

From source file:br.unisal.twitter.bean.TwitterBean.java

public void postingToTwitter() {
    try {/* w w w .  jav  a 2  s. c  o m*/
        TwitterFactory TwitterFactory = new TwitterFactory();
        Twitter twitter = TwitterFactory.getSingleton();

        String message = getTwitt();
        Status status = twitter.updateStatus(message);

        /*String s = "status.toString() = " + status.toString()
         + "status.getInReplyToScreenName() = " + status.getInReplyToScreenName()
         + "status.getSource() = " + status.getSource()
         + "status.getText() = " + status.getText()
         + "status.getContributors() = " + Arrays.toString(status.getContributors())
         + "status.getCreatedAt() = " + status.getCreatedAt()
         + "status.getCurrentUserRetweetId() = " + status.getCurrentUserRetweetId()
         + "status.getGeoLocation() = " + status.getGeoLocation()
         + "status.getId() = " + status.getId()
         + "status.getInReplyToStatusId() = " + status.getInReplyToStatusId()
         + "status.getInReplyToUserId() = " + status.getInReplyToUserId()
         + "status.getPlace() = " + status.getPlace()
         + "status.getRetweetCount() = " + status.getRetweetCount()
         + "status.getRetweetedStatus() = " + status.getRetweetedStatus()
         + "status.getUser() = " + status.getUser()
         + "status.getAccessLevel() = " + status.getAccessLevel()
         + "status.getHashtagEntities() = " + Arrays.toString(status.getHashtagEntities())
         + "status.getMediaEntities() = " + Arrays.toString(status.getMediaEntities())
         + "status.getURLEntities() = " + Arrays.toString(status.getURLEntities())
         + "status.getUserMentionEntities() = " + Arrays.toString(status.getUserMentionEntities());*/
        String s = "status.getId() = " + status.getId() + "\nstatus.getUser() = " + status.getUser().getName()
                + " - " + status.getUser().getScreenName() + "\nstatus.getGeoLocation() = "
                + status.getGeoLocation() + "\nstatus.getText() = " + status.getText();
        setTwittsResult(s);
        this.getUiMsg().setSubmittedValue("");
        this.getUiResultMsg().setSubmittedValue(getTwittsResult());
    } catch (TwitterException ex) {
        LOG.error("Erro no postingToTwitter() - " + ex.toString());
    }
}

From source file:br.unisal.twitter.bean.TwitterBean.java

public void consultarTweets() {
    TwitterFactory TwitterFactory = new TwitterFactory();
    Twitter twitter = TwitterFactory.getSingleton();
    List<Status> tweets = new ArrayList<>();
    Query query = new Query(getTwittQuery());
    try {/*from w  ww. j  ava2  s  . co  m*/
        QueryResult result = twitter.search(query);
        tweets.addAll(result.getTweets());
        StringBuilder builder = new StringBuilder();
        double lon = 0;
        double lat = 0;
        for (Status s : tweets) {
            if ((s.getGeoLocation() != null)) {
                lon = s.getGeoLocation().getLongitude();
                lat = s.getGeoLocation().getLatitude();
            }
            Tweet t = new Tweet(s.getUser().getName(), s.getUser().getLocation(), s.getText(), s.getCreatedAt(),
                    lat, lon);
            dao.insert(t);
            builder.append(t.toString());
        }
        this.getUiResultQuery().setSubmittedValue(builder.toString());
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
}

From source file:chillerbot.ChillTweet.java

private void setUpTwitter() throws TwitterException {
    twitter = new TwitterFactory().getInstance();
    if (loadKeys()) {
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

        AccessToken oathAccessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

        twitter.setOAuthAccessToken(oathAccessToken);
        this.homeTimeline = twitter.getHomeTimeline();
    } else {//from w  w  w .j ava 2  s.  co m
        System.out.println("Authentication failed, check keys.");
    }
}

From source file:Classes.TwitterPull.java

private void init() {
    this.twitterFactory = new TwitterFactory();
    this.twitter = this.twitterFactory.getInstance();
    this.twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    this.twitter.setOAuthAccessToken(new AccessToken(DEFAULT_ACCESS_TOKEN, DEFAULT_ACCESS_TOKEN_SECRET));
    retrievedTweets = new ArrayList<>();
}