Example usage for twitter4j Status getText

List of usage examples for twitter4j Status getText

Introduction

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

Prototype

String getText();

Source Link

Document

Returns the text of the status

Usage

From source file:es.upm.oeg.entity.extractor.extractor.gate.TwitterCorpus.java

public void createCorpus() {

    repository = new FarolasRepo();

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();/*from   ww  w  .j  a v  a2s. c om*/
    try {
        corpus = Factory.newCorpus("tweetcorpus");
        Query query = new Query(queryString); //"oddfarolas"
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            Document doc = Factory.newDocument(tweet.getText());
            doc.setName(String.valueOf(tweet.getId()));
            corpus.add(doc);

            logger.info(tweet.getId() + "  @" + tweet.getUser().getScreenName() + " - " + tweet.getText() + " -"
                    + tweet.getGeoLocation());
            repository.instanciateNew(String.valueOf(tweet.getId()), tweet.getUser().getScreenName(),
                    tweet.getText(), tweet.getGeoLocation());

        }

    } catch (TwitterException te) {
        logger.error(te);
        logger.error("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    } catch (ResourceInstantiationException ex) {
        logger.error(ex);
    }
    logger.info("corpus size" + corpus.size());

}

From source file:es.upm.oeg.examples.watson.servlets.TwitterAnalysisServlet.java

License:Apache License

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *//*  w  w  w  .j  av a  2  s  .c  om*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String twitterUsername = request.getParameter("twitter_name");
    // The factory instance is re-useable and thread safe.
    Twitter twitter = new TwitterFactory().getInstance();
    env = System.getenv();
    consumer_key = env.get("TWITTER_CONSUMER_KEY");
    consumer_secret = env.get("TWITTER_CONSUMER_SECRET");
    access_token = env.get("TWITTER_ACCESS_TOKEN");
    access_key = env.get("TWITTER_ACCESS_KEY");

    AccessToken accessToken = new AccessToken(access_token, access_key);

    try {
        twitter.setOAuthConsumer(consumer_key, consumer_secret);
        twitter.setOAuthAccessToken(accessToken);

        twitter4j.User a_name = twitter.showUser(twitterUsername);
        int followerCount = a_name.getFollowersCount();
        List<Status> retweets = twitter.getUserTimeline(twitterUsername, new Paging(1, 20)); // get the first twenty tweets
        int retweetCount = 0;
        List<String> langs = new ArrayList<>();
        List<String> translated = new ArrayList<>();

        StringBuilder aggregatedTextBuilder = new StringBuilder();
        String personalityInsights = null;

        for (Status tweet : retweets) {
            String tweetText = tweet.getText();
            try {
                String lang = languageIdentification.getLang(tweetText);
                langs.add(lang);
                String englishText;
                //TODO do the same for french and portuguese
                if (LanguageIdentificationService.ES_ES.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.ES_TO_EN);
                } else if (LanguageIdentificationService.FR_FR.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.FR_TO_EN);
                } else if (LanguageIdentificationService.PT_BR.equals(lang)) {
                    englishText = machineTranslation.translate(tweetText, MachineTranslationService.PT_TO_EN);
                } else {
                    englishText = tweetText;
                }
                translated.add(englishText);
                aggregatedTextBuilder.append(englishText);

                personalityInsights = personalityInsightsService.analyse(aggregatedTextBuilder.toString());

            } catch (Exception e) {
                // Log something and return an error message
                logger.log(Level.SEVERE, "got error: " + e.getMessage(), e);
                request.setAttribute("error", e.getMessage());
            }

        }

        request.setAttribute("t_name", twitterUsername);
        request.setAttribute("rtweets", retweets);
        request.setAttribute("langs", langs);
        request.setAttribute("translated", translated);
        request.setAttribute("personalityInsights", personalityInsights);
        request.setAttribute("aggregatedText", aggregatedTextBuilder.toString());

        request.getRequestDispatcher("/myTweets.jsp").forward(request, response);
    } catch (TwitterException e) {

        e.printStackTrace();
        if (e.getErrorCode() == 215 || e.getErrorCode() == 32) {
            response.sendRedirect("../index.html?message=errorcode215");
        } else if (e.getErrorCode() == -1 || e.getErrorCode() == 34) {
            response.sendRedirect("../index.html?message=errorcode-1");
        } else {
            response.sendRedirect("../index.html?message=errorcode99");
        }
        //throw new ServletException("Encountered a problem fetching data from Twitter - " + e.getErrorMessage());
    }
}

From source file:essex.bigessexnew.TwitterStreamHandler.java

@Override
public void run() {
    StatusListener sl = new StatusListener() {
        @Override//  www .  j  av a2  s.c  o m
        public void onStatus(Status status) {
            /*String s = TwitterObjectFactory.getRawJSON(status);
            System.out.println(s);*/
            BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
            documentBuilder.add("content", status.getText());
            collection.insert(documentBuilder.get());

            /*DBObject dbObject = (DBObject) JSON.parse(json);
            collection.insert(dbObject);*/
            //System.out.println(json.toString());

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

        }

        @Override
        public void onScrubGeo(long l, long l2) {

        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {

        }

        @Override
        public void onException(Exception ex) {

        }
    };
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(sl);
    twitterStream.filter(Params.getProperty("hashtag"));
}

From source file:examencodrecu.MetodosTwitter.java

public void verLineaTiempo() throws TwitterException {

    List<Status> statuses = twitter.getHomeTimeline();
    System.out.println("Showing home timeline.");
    for (Status status : statuses) {
        System.out.println(status.getUser().getName() + ":" + status.getText());
    }//from www  . ja  v a2s.c  om
}

From source file:examencodrecu.MetodosTwitter.java

public void buscarTuit(String busqueda) throws TwitterException {

    Query query = new Query(busqueda);
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }//from  ww  w.jav  a2s. c  om

}

From source file:examencodrecu.MetodosTwitter.java

public void twittear(String tweet) throws TwitterException {
    Status status = twitter.updateStatus(tweet);
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
}

From source file:examencodtwitter.Metodos.java

/**
         //from   ww w  .  j  a v a 2  s.  co  m
 *
 * @throws TwitterException
 */
public void verTimeLine() throws TwitterException {

    List<Status> statuses = twitter.getHomeTimeline();
    System.out.println("Mostrando su Timeline.");
    for (Status status : statuses) {
        System.out.println(status.getUser().getName() + ":" + status.getText());
    }
}

From source file:examencodtwitter.Metodos.java

/**
         /*www  .  java 2 s  .  c  o  m*/
 *
 * @param busqueda
 * @throws TwitterException
 */
public void buscar(String busqueda) throws TwitterException {

    Query query = new Query(busqueda);
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
}

From source file:examencodtwitter.Metodos.java

/**
 *
 *
 * @param tweet * @throws TwitterException
 *///from  w  w w  . j  a  v a2 s.  co m
public void twitear(String tweet) throws TwitterException {

    Status status = twitter.updateStatus(tweet);
    System.out.println("Has twiteado el siguiente mensaje [" + status.getText() + "].");

}

From source file:examendetwitter.metodostwitter.java

/**
 * Nos da nuestro timeLine/*from  w w w.j av a  2s . co  m*/
 */
public void verTL() throws TwitterException {

    List<Status> statuses = twitter.getHomeTimeline();
    System.out.println("Showing home timeline.");
    for (Status status : statuses) {
        System.out.println(status.getUser().getName() + ":" + status.getText());
    }

}