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:nl.b3p.viewer.stripes.TwitterActionBean.java

License:Open Source License

public Resolution create() throws JSONException {
    JSONObject json = new JSONObject();

    json.put("success", Boolean.FALSE);
    String error = null;/*from   ww  w . j ava2 s . co  m*/

    try {
        // The factory instance is re-useable and thread safe.
        Twitter twitter = new TwitterFactory().getInstance();
        Query query = new Query(term);
        if (latestId != null) {
            Long longVal = Long.valueOf(latestId);
            query.setSinceId(longVal);
        }

        QueryResult result = twitter.search(query);
        JSONArray tweets = new JSONArray();
        for (Status tweet : result.getTweets()) {

            //System.out.println(tweet.getFromUser() + ":" + tweet.getText());
            JSONObject t = new JSONObject();
            t.put("id_str", String.valueOf(tweet.getId()));
            t.put("text", tweet.getText());
            t.put("user_from", tweet.getUser().getScreenName());
            t.put("img_url", tweet.getUser().getProfileImageURL());

            JSONObject geo = new JSONObject();
            if (tweet.getGeoLocation() != null) {
                geo.put("lat", tweet.getGeoLocation().getLatitude());
                geo.put("lon", tweet.getGeoLocation().getLongitude());
            }
            t.put("geo", geo);
            tweets.put(t);
        }
        json.put("tweets", tweets);
        if (tweets.length() > 0) {
            json.put("maxId", String.valueOf(result.getMaxId()));
        } else {
            json.put("maxId", String.valueOf(latestId));
        }
        json.put("success", Boolean.TRUE);
    } catch (Exception e) {

        error = e.toString();
        if (e.getCause() != null) {
            error += "; cause: " + e.getCause().toString();
        }
    }

    if (error != null) {
        json.put("error", error);
    }

    return new StreamingResolution("application/json", new StringReader(json.toString()));
}

From source file:nl.isaac.dotcms.twitter.pojo.CustomStatus.java

License:Creative Commons License

public CustomStatus(Status status) {
    this.createdAt = status.getCreatedAt();
    this.id = status.getId();
    this.id_str = String.valueOf(status.getId());
    this.text = status.getText();
    this.source = status.getSource();
    this.isTruncated = status.isTruncated();
    this.inReplyToStatusId = status.getInReplyToStatusId();
    this.inReplyToUserId = status.getInReplyToUserId();
    this.isFavorited = status.isFavorited();
    this.inReplyToScreenName = status.getInReplyToScreenName();
    this.geoLocation = status.getGeoLocation();
    this.place = status.getPlace();
    this.retweetCount = status.getRetweetCount();
    this.isPossiblySensitive = status.isPossiblySensitive();

    this.contributorsIDs = status.getContributors();

    this.retweetedStatus = status.getRetweetedStatus();
    this.userMentionEntities = status.getUserMentionEntities();
    this.urlEntities = status.getURLEntities();
    this.hashtagEntities = status.getHashtagEntities();
    this.mediaEntities = status.getMediaEntities();
    this.currentUserRetweetId = status.getCurrentUserRetweetId();

    this.isRetweet = status.isRetweet();
    this.isRetweetedByMe = status.isRetweetedByMe();

    this.rateLimitStatus = status.getRateLimitStatus();
    this.accessLevel = status.getAccessLevel();
    this.user = status.getUser();
}

From source file:nl.utwente.bigdata.bolts.NormalizerBolt.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    Status tweet;
    tweet = (Status) tuple.getValueByField("tweet");

    // from: http://stackoverflow.com/questions/1008802/converting-symbols-accent-letters-to-english-alphabet
    Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    String nfdNormalizedString = "";
    nfdNormalizedString = Normalizer.normalize(tweet.getText(), Normalizer.Form.NFD);

    String normalizedTweet = (String) pattern.matcher(nfdNormalizedString.toLowerCase()).replaceAll("")
            .replace("\n", "").replace("\r", "");
    // Also remove prefixed with rt
    if (!normalizedTweet.startsWith("rt")) {
        collector.emit(new Values(tweet, normalizedTweet, tweet.getLang()));
    }// w  w w.ja  va2  s . c  o  m
}

From source file:nl.utwente.bigdata.bolts.PrinterSentiment.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    String icon;// w w w. j  av  a 2  s. c  o  m

    if ((Integer) tuple.getValueByField("sentiment") > 0) {
        icon = ":-D";
    } else if ((Integer) tuple.getValueByField("sentiment") < 0) {
        icon = ">:[";
    } else {
        icon = ":-)";
    }
    Status tweet = (Status) tuple.getValueByField("tweet");
    String tweetText = tweet.getText();
    System.out.println(Emoji.replaceFlagInText(this.language) + " " + Emoji.replaceInText(icon) + " " + "DATE: "
            + tweet.getCreatedAt().toGMTString() + " " + "MATCH: " + tuple.getValueByField("home") + "-"
            + tuple.getValueByField("away") + " - " + tweetText + " " + tuple.getValueByField("sentiment"));

    collector.emit(new Values(this.language, tuple.getStringByField("normalized_text"),
            tweet.getCreatedAt().toGMTString(), tuple.getStringByField("home"), tuple.getValueByField("away"),
            tuple.getValueByField("sentiment")));
}

From source file:nlptexthatespeechdetection.dataCollection.GetTwitterDoc2VecTrainingData.java

public static void main(String[] args) throws FileNotFoundException, IOException {
    File dir = new File(folderName);
    if (!dir.exists())
        dir.mkdir();//from   www  . ja va  2s .  c o  m
    if (!dir.isDirectory()) {
        System.out.println(folderName + " is not a directory");
        return;
    }

    System.out.println("number of tweets required: ");
    int numTweetsRequired = (new Scanner(System.in)).nextInt();

    String path = folderName + "/" + fileName;
    File file = new File(path);
    if (!file.exists())
        file.createNewFile();
    FileWriter writer = new FileWriter(path, true);

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener = new StatusListener() {
        int numTweets = 0;

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("in")) {
                try {
                    String statusText = status.getText();
                    writer.write("\n");
                    writer.write(statusText);
                    numTweets++;
                    System.out.println("numTweets: " + numTweets);

                    if (numTweets >= numTweetsRequired) {
                        writer.close();
                        System.exit(0);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(GetTwitterDoc2VecTrainingData.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };

    twitterStream.addListener(listener);

    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(new String[] { "a", "i", "u", "e", "o" });
    filterQuery.language("in");
    twitterStream.filter(filterQuery);

}

From source file:nlptexthatespeechdetection.dataCollection.TwitterStreamingAnnotator.java

public static void main(String[] args) throws NotDirectoryException {
    Scanner sc = new Scanner(System.in);
    System.out.println("Nama Anda (sebagai anotator): ");
    String namaAnotator = sc.nextLine();
    AnnotatedDataFolder annotatedDataFolder = new AnnotatedDataFolder(dataFolderName);

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener = new StatusListener() {
        @Override/*  www .j  a v  a2 s .c om*/
        public void onStatus(Status status) {
            if (status.getLang().equals("in")) {
                System.out.println();
                System.out.println();
                System.out.println("=======ANOTASI=======");
                System.out.println("status: " + status.getText());
                System.out.println();
                System.out.println("is this a hate speech?(y/n. any other if you do not know)");
                String annotatorResponse = sc.nextLine().trim().toLowerCase();

                Date date = new Date();
                String dateString = dateFormat.format(date);

                try {
                    if (annotatorResponse.equals("y")) {
                        String filePath = annotatedDataFolder.saveHateSpeechString(namaAnotator, dateString,
                                status.getText());
                        System.out.println("Saved data to: " + filePath);
                    } else if (annotatorResponse.equals("n")) {
                        String filePath = annotatedDataFolder.saveNotHateSpeechString(namaAnotator, dateString,
                                status.getText());
                        System.out.println("Saved data to: " + filePath);
                    }
                    System.out.println("thank you!");
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamingAnnotator.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else {
                System.out.println("ignoring non-indonesian tweet");
            }
            //                if (status.getGeoLocation() != null) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString());
            //                }
            //                if (status.getLang().equals("id")) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString()));
            //                }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };

    twitterStream.addListener(listener);

    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(new String[] { "a", "i", "u", "e", "o" });
    filterQuery.language("in");
    twitterStream.filter(filterQuery);
}

From source file:nlptexthatespeechdetection.NLPTextHateSpeechDetection.java

/**
 * @param args the command line arguments
 *//*from   ww  w .j  av  a2 s. co m*/
public static void main(String[] args) throws TwitterException, NotDirectoryException, IOException {
    HateSpeechClassifier1 classifier = new HateSpeechClassifier1();
    AnnotatedDataFolder data = new AnnotatedDataFolder("data");
    boolean overSampling = false;
    classifier.train(data.getDateSortedLabeledData(overSampling));

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener = new StatusListener() {
        int numHateSpeech = 0;
        int numTweets = 0;

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("in")) {
                numTweets++;
                if (classifier.isHateSpeech(status.getText(), 0.5)) {
                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * "
                            + status.getId() + " # " + status.getLang() + " $ "
                            + (status.getGeoLocation() == null ? "NULLGEO"
                                    : status.getGeoLocation().toString()));
                    System.out.println();
                    System.out.println("lang: " + status.getLang());
                    System.out.println("number of detected hate speech: " + numHateSpeech);
                    System.out.println("total number of streamed tweets: " + numTweets);
                    System.out.println();
                    System.out.println();
                    numHateSpeech++;
                }
            } else {
                System.out.println("ignoring non-Indonesian tweet");
            }
            //                if (status.getGeoLocation() != null) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString());
            //                }
            //                if (status.getLang().equals("id")) {
            //                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString()));
            //                }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };

    twitterStream.addListener(listener);

    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(new String[] { "a", "i", "u", "e", "o" });
    filterQuery.language("in");
    twitterStream.filter(filterQuery);

    twitterStream.sample();
}

From source file:nselive.NSELive.java

/**
 * @param args the command line arguments
 *///from  w  w  w  .j  a  v a  2  s  . co  m
public static void main(String[] args) {
    try {
        String url = "http://www.nellydata.com/CapitalFM/livedata.asp";
        //fetch data
        String docString = Jsoup.connect(url).get().toString();
        String[] tBodyArray = docString.split("<tbody>");
        String[] tableArray = tBodyArray[1].split("</tbody>");
        String tableContent = tableArray[0].trim();
        //delete header rows
        String[] headerlessContent = tableContent.split("Low</strong> </td>");
        String[] rowArray = headerlessContent[1].split("<tr>");
        //skip rowArray[0] which has string "</tr>
        for (int i = 1; i <= rowArray.length - 1; i++) {
            String rowContent = rowArray[i];
            String[] cellArray = rowContent.split("</td>");
            Stock stock = new Stock();
            String[] idArray = cellArray[0].split("mycell\">");
            stock.setId(Integer.parseInt(idArray[1]));
            String[] stockNameArray1 = cellArray[1].split("<strong>");
            String[] stockNameArray2 = stockNameArray1[1].split("</strong>");
            stock.setName(stockNameArray2[0]);
            String[] priceYesterdayArray = cellArray[2].split("mycell\">");
            stock.setPriceYesterday(Double.parseDouble(priceYesterdayArray[1].replace(",", "")));
            String[] currentPriceArray = cellArray[3].split("style2\">");
            stock.setCurrentPrice(Double.parseDouble(currentPriceArray[1].replace(",", "")));
            if (stock.getCurrentPrice() != stock.getPriceYesterday()) {
                String tweet = "";
                //TODO: Change to hourly updates
                if (stock.getCurrentPrice() > stock.getPriceYesterday()) {
                    tweet = stock.getName().toUpperCase() + " has RISEN to " + stock.getCurrentPrice()
                            + " from " + stock.getPriceYesterday() + " yesterday";
                } else if (stock.getCurrentPrice() < stock.getPriceYesterday()) {
                    tweet = stock.getName().toUpperCase() + " has FALLEN to " + stock.getCurrentPrice()
                            + " from " + stock.getPriceYesterday() + " yesterday";
                }
                //get the following from your twitter account
                String consumerKey = "yourConsumerKey";
                String consumerSecret = "yourConsumerSecret";
                String accessToken = "yourAccessToken";
                String accessSecret = "yourAccessSecret";

                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
                        .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessSecret);

                try {
                    TwitterFactory factory = new TwitterFactory(cb.build());
                    Twitter twitter = factory.getInstance();

                    Status status = twitter.updateStatus(tweet);
                    System.out.println("NEW TWEET: " + status.getText());
                } catch (TwitterException te) {
                    te.printStackTrace();
                    System.exit(-1);
                }
            }
        }
    } catch (IOException ex) {
        System.out.println("Ooops! No data this time. Our connection timed out :(");
        Logger.getLogger(NSELive.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:nyu.twitter.lg.FentchTwitter.java

License:Open Source License

public static void invoke() throws Exception {
    init();/*ww  w. jav a2 s .c o m*/
    // Create table if it does not exist yet
    if (Tables.doesTableExist(dynamoDB, tableName)) {
        System.out.println("Table " + tableName + " is already ACTIVE");
    } else {
        // Create a table with a primary hash key named 'name', which holds
        // a string
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id")
                        .withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));
        TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest)
                .getTableDescription();
        System.out.println("Created Table: " + createdTableDescription);
        // Wait for it to become active
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
        Tables.waitForTableToBecomeActive(dynamoDB, tableName);
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("Emwo2pG").setOAuthConsumerSecret("RM9B7fske5T")
            .setOAuthAccessToken("19ubQOirq").setOAuthAccessTokenSecret("Lbg3C");

    final TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            if (status.getGeoLocation() != null && status.getPlace() != null) {

                //               if (count == 0) {
                //                  count++;
                //               }
                //               
                latitude = status.getGeoLocation().getLatitude();
                longtitude = status.getGeoLocation().getLongitude();
                place = status.getPlace().getCountry() + "," + status.getPlace().getFullName();
                date = status.getCreatedAt().toString();
                id = Integer.toString(count);
                name = status.getUser().getScreenName();
                message = status.getText();
                System.out.println("---------------------------");
                System.out.println("ID:" + count);
                System.out.println("latitude:" + latitude);
                System.out.println("longtitude:" + longtitude);
                System.out.println("place:" + place);
                System.out.println("name:" + name);
                System.out.println("message:" + message);
                System.out.println("data:" + date);
                System.out.println("-------------8-------------");

                insertDB(id, count, name, longtitude, latitude, place, message, date);

                if (++count > 100) {
                    twitterStream.shutdown();
                    System.out.println("Information Collection Completed");
                }
                //      count = (count+1) % 101;
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //            System.out.println("Got a status deletion notice id:"
            //                  + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            //            System.out.println("Got track limitation notice:"
            //                  + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            //            System.out.println("Got scrub_geo event userId:" + userId
            //                  + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            //            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample();
}

From source file:nz.net.speakman.android.dreamintweets.twitterstream.TwitterStreamAdapter.java

License:Apache License

private Spanned getTweetText(Status tweet) {
    String text = tweet.getText();

    for (URLEntity urlEntity : tweet.getURLEntities()) {
        text = text.replace(urlEntity.getURL(), getClickableUrl(urlEntity));
    }/*  w  w w .  j  ava 2s .  co  m*/

    for (MediaEntity mediaEntity : tweet.getMediaEntities()) {
        // TODO Optionally load images into stream
        text = text.replace(mediaEntity.getURL(), getClickableMedia(mediaEntity));
    }

    //        for (HashtagEntity hashtagEntity : tweet.getHashtagEntities()) {
    //            // TODO Make clickable
    //        }
    return Html.fromHtml(text);
}