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:GetEmoContent.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    startup();//from  w  w w  .  ja v a  2s  .  c  o  m
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            if (status.getLang().equals("en"))
            //if(status.getText().toLowerCase().contains("trump")|| status.getText().toLowerCase().contains("drumpf"))
            {

                int score = scoreTweet(status.getText());

                System.out
                        .println("score: " + score + " language " + status.getLang() + " " + status.getText());

                //add this to the database
                try {
                    Statement stmt = c.createStatement();
                    String sql = "INSERT INTO tbl1(f1, f2) " + "VALUES ('\""
                            + status.getText().replaceAll("'", "''") + "\"', '" + score + "')";

                    stmt.executeUpdate(sql);
                    stmt.close();
                    //c.commit();
                } catch (Exception e) {
                    System.err.println(e.getClass().getName() + ": " + e.getMessage());
                    System.exit(0);
                }
            }
        }

        @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:PrintFirehoseStream.java

License:Apache License

/**
 * Main entry of this application.//from  w  w w  .j av  a  2  s.c om
 *
 * @param args arguments doesn't take effect with this example
 * @throws TwitterException when Twitter service or network is unavailable
 */
public static void main(String[] args) throws TwitterException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("");
    cb.setOAuthConsumerSecret("");
    cb.setOAuthAccessToken("");
    cb.setOAuthAccessTokenSecret("");

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }

        @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.firehose(0);
}

From source file:GetLiveMedia.java

License:Open Source License

public void TweetCollect(String searchString) throws TwitterException {
    tXCoord = 0;/*from ww  w .  j  a v  a  2  s . c om*/
    tYCoord = 0;
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("0c1v1wqqNATaVcWpINaHoW3o5")
            .setOAuthConsumerSecret("a6sEAJ1NLY3cXhkMTrMZF62bIfww6qLvlEetDAU9O09Mp04lyh")
            .setOAuthAccessToken("330427831-RJig6B6YycJL3xQVoEjjyRaujwnr1xHEQGtSHOOc")
            .setOAuthAccessTokenSecret("Da71qk4SN6Nu88mLkugDDR6CftVcgOcz3QB8dHNw4hVbD");
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter = tf.getInstance();
    query = new Query("#" + searchString);
    result = twitter.search(query);
    for (Status status : result.getTweets()) {
        try {
            System.out.println(status.getGeoLocation());
            System.out.println(status.getCreatedAt());
            tXCoord = status.getGeoLocation().getLatitude();
            tYCoord = status.getGeoLocation().getLongitude();
            twitterString = ("@" + status.getUser().getScreenName() + ": " + status.getText() + " : " + "("
                    + tXCoord + ", " + tYCoord + ")");
            twitterURL = "https://twitter.com/" + status.getUser().getScreenName() + "/status/"
                    + status.getId();
        } catch (Exception e) {
        }
    }
}

From source file:TwitterSearchVine.java

License:Open Source License

public void newTweets(Vector<Status> tweets) {
    for (Status tweet : tweets) {

        if (!tweet.isRetweet()) {
            Vector<String> urls = Utils.parseUrls(tweet.getText());

            for (String url : urls) {

                try {
                    byte[] rawBytes = loadBytes(url);
                    String html = new String(rawBytes, "UTF-8");

                    Vine vine = Utils.parseVine(html);

                    if (vine != null) {
                        rawBytes = loadBytes(vine.getImgURL());
                        saveBytes("data/media/" + vine.getId() + ".jpg", rawBytes);

                        rawBytes = loadBytes(vine.getVidURL());
                        saveBytes("data/media/" + vine.getId() + ".mp4", rawBytes);

                        vines.add(vine);
                    }//from w  w w .ja v  a  2 s  .c o m
                } catch (UnsupportedEncodingException exc) {
                }
            }
        }
    }
}

From source file:Parser.java

public void setData(LinkedList<Status> data) {
    for (Status s : data) {
        for (int i = 0; i < countries.length; i++) {
            if (s.getPlace().getCountry().equalsIgnoreCase(countries[i])) {
                messages.get(i).add(s.getText());
            }//w w w . j a  v  a  2  s  . c  om
        }
    }
}

From source file:CollectAndPredict.java

public static void main(String[] args) {

    //StreamingExamples.setStreamingLogLevels();
    // Set logging level if log4j not configured (override by adding log4j.properties to classpath)
    if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
        Logger.getRootLogger().setLevel(Level.WARN);
    }//from w ww. j a  v a  2 s.  c  om

    String OAuthConsumerKey = "QxeynbXmN93DuNiZKkYfZcS2o";
    String OAuthConsumerSecret = "2rAzjerHeW6sIgeDim0A77iGaRn9O683m0DrTbBhaoIuRRq7oU";
    String OAuthAccessToken = "825094416935297025-jCegwA25yj3QxF2rHeJ5hRoVu86AfaY";
    String OAuthAccessTokenSecret = "CwfNmGcWHoL8qvr5dWDdknYM4k4KvAZc7XlGZuYl2DcR8";
    String[] filters = Arrays.copyOfRange(args, 0, args.length);

    // Set the system properties so that Twitter4j library used by Twitter stream
    // can use them to generate OAuth credentials
    System.setProperty("twitter4j.oauth.consumerKey", OAuthConsumerKey);
    System.setProperty("twitter4j.oauth.consumerSecret", OAuthConsumerSecret);
    System.setProperty("twitter4j.oauth.accessToken", OAuthAccessToken);
    System.setProperty("twitter4j.oauth.accessTokenSecret", OAuthAccessTokenSecret);

    SparkConf sparkConf = new SparkConf().setAppName("JavaTwitterHashTagJoinSentiments");

    // check Spark configuration for master URL, set it to local if not configured
    if (!sparkConf.contains("spark.master")) {
        sparkConf.setMaster("local[2]");
    }
    SparkSession spark = SparkSession.builder().appName("teste2").config(sparkConf).getOrCreate();
    JavaStreamingContext jssc = new JavaStreamingContext(new JavaSparkContext(spark.sparkContext()),
            Seconds.apply(30));

    TokenizerFactory tokFactory = TwitterTokenizerFactory.getTokFactory();

    NaiveBayesModel model = NaiveBayesModel.load(spark.sparkContext(), "Docker/myNaiveBayesModel");
    HashingTF hashingTF = new HashingTF(1000);
    JavaReceiverInputDStream<Status> stream = TwitterUtils.createStream(jssc, filters);
    JavaDStream<Tweet> statuses = stream.map((Status status) -> new Tweet()
            .addUser(new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName())
                    .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt())
                    .addCountTweets(status.getUser().getStatusesCount())
                    .addCountFavorites(status.getUser().getFavouritesCount())
                    .addCountFriends(status.getUser().getFriendsCount())
                    .addCountFollowers(status.getUser().getFollowersCount()))
            .addText(status.getText()).addID(status.getId()).addDate(status.getCreatedAt())
            .addLatitude(
                    status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() : Double.MAX_VALUE)
            .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude()
                    : Double.MAX_VALUE));

    statuses.foreachRDD(new VoidFunction2<JavaRDD<Tweet>, Time>() {
        long numTweetsCollected = 0;
        long numTweetsToCollect = 200;

        @Override
        public void call(JavaRDD<Tweet> t1, Time t2) throws Exception {
            List<Tweet> collect = t1.collect();

            long count = collect.size();
            if (count > 0) {
                for (Tweet tweet : collect) {
                    String textoSemUrl = URLRemove.remove(tweet.getText());
                    Vector v = hashingTF.transform(Arrays.asList(tokFactory
                            .tokenizer(textoSemUrl.toCharArray(), 0, textoSemUrl.length()).tokenize()));
                    double predict = model.predict(v);
                    if (predict == 1) {
                        tweet.setClassifier("POSITIVE");
                    } else {
                        tweet.setClassifier("NEGATIVE");
                    }
                }
                ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
                try {
                    ow.writeValue(
                            new FileOutputStream(new File("Docker/Twitter" + t2.milliseconds() + ".json")),
                            collect);
                } catch (Exception ex) {
                    spark.log().error(ex.getMessage(), ex);
                }
                numTweetsCollected += count;
                spark.log().info("coletou :" + numTweetsCollected + " tweets");
                if (numTweetsCollected > numTweetsToCollect) {
                    System.exit(0);
                }
            }
        }
    });
    //        statuses.foreachRDD(new VoidFunction2<JavaRDD<String>, Time>() {
    //            long numTweetsCollected = 0;
    //            long numTweetsToCollect = 200;
    //
    //            @Override
    //            public void call(JavaRDD<String> rdd, Time time) throws Exception {
    //                long count = rdd.count();
    //                if (count > 0) {
    //                    JavaRDD<String> outputRDD = rdd.repartition(10);
    //                    outputRDD.saveAsTextFile("/Docker/tweets_" + time.milliseconds());
    //                    numTweetsCollected += count;
    //                    if (numTweetsCollected > numTweetsToCollect) {
    //                        System.exit(0);
    //                    }
    //                }
    //            }
    //        });
    //        JavaDStream<String> words = stream.flatMap(new FlatMapFunction<Status, String>() {
    //
    //            @Override
    //            public Iterable<String> call(Status t) throws Exception {
    //                return Arrays.asList(t.getText().split(" "));
    //            }
    //        });
    //
    //        JavaDStream<String> hashTags = words.filter(new Function<String, Boolean>() {
    //            @Override
    //            public Boolean call(String word) {
    //                return word.startsWith("#");
    //            }
    //        });
    //
    //        // Read in the word-sentiment list and create a static RDD from it
    //        String wordSentimentFilePath = "streaming-twitter/examples/data/AFINN-111.txt";
    //        final JavaPairRDD<String, Double> wordSentiments = jssc.sparkContext()
    //                .textFile(wordSentimentFilePath)
    //                .mapToPair(new PairFunction<String, String, Double>() {
    //                    @Override
    //                    public Tuple2<String, Double> call(String line) {
    //                        String[] columns = line.split("\t");
    //                        return new Tuple2<>(columns[0], Double.parseDouble(columns[1]));
    //                    }
    //                });
    //
    //        JavaPairDStream<String, Integer> hashTagCount = hashTags.mapToPair(
    //                new PairFunction<String, String, Integer>() {
    //                    @Override
    //                    public Tuple2<String, Integer> call(String s) {
    //                        // leave out the # character
    //                        return new Tuple2<>(s.substring(1), 1);
    //                    }
    //                });
    //
    //        JavaPairDStream<String, Integer> hashTagTotals = hashTagCount.reduceByKeyAndWindow(
    //                new Function2<Integer, Integer, Integer>() {
    //                    @Override
    //                    public Integer call(Integer a, Integer b) {
    //                        return a + b;
    //                    }
    //                }, new Duration(10000));
    //
    //        // Determine the hash tags with the highest sentiment values by joining the streaming RDD
    //        // with the static RDD inside the transform() method and then multiplying
    //        // the frequency of the hash tag by its sentiment value
    //        JavaPairDStream<String, Tuple2<Double, Integer>> joinedTuples
    //                = hashTagTotals.transformToPair(new Function<JavaPairRDD<String, Integer>, JavaPairRDD<String, Tuple2<Double, Integer>>>() {
    //                    @Override
    //                    public JavaPairRDD<String, Tuple2<Double, Integer>> call(
    //                            JavaPairRDD<String, Integer> topicCount) {
    //                                return wordSentiments.join(topicCount);
    //                            }
    //                });
    //
    //        JavaPairDStream<String, Double> topicHappiness = joinedTuples.mapToPair(
    //                new PairFunction<Tuple2<String, Tuple2<Double, Integer>>, String, Double>() {
    //                    @Override
    //                    public Tuple2<String, Double> call(Tuple2<String, Tuple2<Double, Integer>> topicAndTuplePair) {
    //                        Tuple2<Double, Integer> happinessAndCount = topicAndTuplePair._2();
    //                        return new Tuple2<>(topicAndTuplePair._1(),
    //                                happinessAndCount._1() * happinessAndCount._2());
    //                    }
    //                });
    //
    //        JavaPairDStream<Double, String> happinessTopicPairs = topicHappiness.mapToPair(
    //                new PairFunction<Tuple2<String, Double>, Double, String>() {
    //                    @Override
    //                    public Tuple2<Double, String> call(Tuple2<String, Double> topicHappiness) {
    //                        return new Tuple2<>(topicHappiness._2(),
    //                                topicHappiness._1());
    //                    }
    //                });
    //
    //        JavaPairDStream<Double, String> happiest10 = happinessTopicPairs.transformToPair(
    //                new Function<JavaPairRDD<Double, String>, JavaPairRDD<Double, String>>() {
    //                    @Override
    //                    public JavaPairRDD<Double, String> call(
    //                            JavaPairRDD<Double, String> happinessAndTopics) {
    //                                return happinessAndTopics.sortByKey(false);
    //                            }
    //                }
    //        );
    //
    //        // Print hash tags with the most positive sentiment values
    //        happiest10.foreachRDD(new VoidFunction<JavaPairRDD<Double, String>>() {
    //            @Override
    //            public void call(JavaPairRDD<Double, String> happinessTopicPairs) {
    //                List<Tuple2<Double, String>> topList = happinessTopicPairs.take(10);
    //                System.out.println(
    //                        String.format("\nHappiest topics in last 10 seconds (%s total):",
    //                                happinessTopicPairs.count()));
    //                for (Tuple2<Double, String> pair : topList) {
    //                    System.out.println(
    //                            String.format("%s (%s happiness)", pair._2(), pair._1()));
    //                }
    //            }
    //        });

    jssc.start();

    try {
        jssc.awaitTermination();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:TwitterHashTagCount.java

License:Apache License

private static void twitterStreaming(int window, int slide) {

    // Create the context with a 1 second batch size
    SparkConf sparkConf = new SparkConf().setAppName("JavaNetworkWordCount");
    JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(1));
    JavaReceiverInputDStream<twitter4j.Status> stream = TwitterUtils.createStream(ssc);

    FlatMapFunction<twitter4j.Status, String> mapFunc = new FlatMapFunction<twitter4j.Status, String>() {
        @Override/*  w  w  w  .  j  a  v a  2s  .  co  m*/
        public Iterable<String> call(twitter4j.Status status) {
            ArrayList<String> hashTag = new ArrayList<String>();
            Pattern p = Pattern.compile("#(\\w+)\\b");
            Matcher m = p.matcher(status.getText());
            while (m.find()) {
                hashTag.add(m.group(1));
            }
            return hashTag;
        }
    };

    VoidFunction<JavaPairRDD<Integer, String>> outFunc = new VoidFunction<JavaPairRDD<Integer, String>>() {
        @Override
        public void call(JavaPairRDD<Integer, String> rdd) {
            List<Tuple2<Integer, String>> list = rdd.take(10);
            Iterator<Tuple2<Integer, String>> ite = list.iterator();
            System.out.println("-------------------------");
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
            System.out.println("   " + timeStamp);
            System.out.println("-------------------------");
            while (ite.hasNext()) {
                Tuple2<Integer, String> tag = ite.next();
                System.out.println(tag.toString());
            }
        }
    };

    stream.flatMap(mapFunc).mapToPair((String s) -> {
        return new Tuple2<String, Integer>(s, 1);
    }).reduceByKeyAndWindow((Integer i1, Integer i2) -> {
        return i1 + i2;
    }, Durations.seconds(window), Durations.seconds(slide)).mapToPair((Tuple2<String, Integer> item) -> {
        return item.swap();
    }).transformToPair((JavaPairRDD<Integer, String> rdd) -> {
        return rdd.sortByKey(false);
    }).foreachRDD(outFunc);

    ssc.start();
    ssc.awaitTermination();
}

From source file:ac.simons.tweetarchive.tweets.TweetStorageService.java

License:Apache License

String extractContent(final Status status) {
    // TODO Handle quoted tweets
    final Status workStatus;
    if (status.isRetweet()) {
        workStatus = status.getRetweetedStatus();
    } else {/*from  www  .  j  av a2 s . c  o  m*/
        workStatus = status;
    }

    final StringBuilder rv = new StringBuilder();

    final String text = workStatus.getText();
    int pos = 0;
    for (URLEntity urlEntity : workStatus.getURLEntities()) {
        rv.append(text.substring(pos, urlEntity.getStart()));
        rv.append(urlEntity.getExpandedURL());
        pos = urlEntity.getEnd();

    }
    if (pos <= text.length()) {
        rv.append(text.substring(pos, text.length()));
    }

    if (status.isRetweet()) {
        rv.insert(0, String.format("RT @%s: ", workStatus.getUser().getScreenName()));
    }
    return rv.toString();
}

From source file:adapter.TwitterAllAdapter.java

License:Apache License

@Override
public void run() {

    while (true) {

        try {//w w w.j  av a2 s . c  om
            Status status = this.messageQueue.take();

            Event event = new Event();

            int rtCount = 0;
            int fvCount = 0;

            if (status.isRetweet()) {

                rtCount = status.getRetweetedStatus().getRetweetCount();
                fvCount = status.getRetweetedStatus().getFavoriteCount();
            }

            Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(),
                    status.getUser().getScreenName(), status.getUser().getLang(),
                    status.getUser().getFollowersCount(), status.getUser().getFriendsCount(),
                    status.getHashtagEntities(), fvCount, rtCount, status.getGeoLocation());
            //status.getRetweetedStatus().getRetweetCount(), status.getGeoLocation());

            eventCount++;
            // cantReplicas: Cantidad de PEs que se quieren generar para el proximo operador
            // Nota: recuerden que la topologa no necesariamente deba ser de este estilo
            // tambin poda ser por un hash
            int cantReplicas = 10;
            event.put("levelTweet", Integer.class, eventCount % cantReplicas);

            event.put("id", Integer.class, eventCount);
            event.put("tweet", Tweet.class, tweet);

            getRemoteStream().put(event);

        } catch (Exception e) {
            logger.error("Error: " + e);
            logger.error("Error al crear evento");
        }

    }

}

From source file:adapter.TwitterKeywordsAdapter.java

License:Apache License

@Override
public void run() {

    while (true) {
        try {//from   w ww  . ja  v a  2s.c o  m

            Status status = this.messageQueue.take();

            Event event = new Event();

            Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(),
                    status.getUser().getScreenName(), status.getUser().getLang(),
                    status.getUser().getFollowersCount(), status.getUser().getFriendsCount(),
                    status.getHashtagEntities(), status.getFavoriteCount(), status.getRetweetCount(),
                    status.getGeoLocation());

            eventCount++;
            // cantReplicas: Cantidad de PEs que se quieren generar para el proximo operador
            // Nota: recuerden que la topologa no necesariamente deba ser de este estilo
            // tambin poda ser por un hash
            int cantReplicas = 10;
            event.put("levelTweet", Integer.class, eventCount % cantReplicas);
            event.put("id", Integer.class, eventCount);
            event.put("tweet", Tweet.class, tweet);

            getRemoteStream().put(event);

        } catch (Exception e) {
            logger.error("Error: " + e);
            logger.error("Error al crear evento");
        }

    }
}