Example usage for twitter4j Status getLang

List of usage examples for twitter4j Status getLang

Introduction

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

Prototype

String getLang();

Source Link

Document

Returns the lang of the status text if available.

Usage

From source file:GetEmoContent.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    startup();//from  w w  w . jav  a 2s. c  om
    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:ac.simons.tweetarchive.tweets.TweetStorageService.java

License:Apache License

@Transactional
public TweetEntity store(final Status status, final String rawContent) {
    final Optional<TweetEntity> existingTweet = this.tweetRepository.findOne(status.getId());
    if (existingTweet.isPresent()) {
        final TweetEntity rv = existingTweet.get();
        log.warn("Tweet with status {} already existed...", rv.getId());
        return rv;
    }/*from www.ja  v a 2 s  .  c  om*/

    final TweetEntity tweet = new TweetEntity(status.getId(), status.getUser().getId(),
            status.getUser().getScreenName(), status.getCreatedAt().toInstant().atZone(ZoneId.of("UTC")),
            extractContent(status), extractSource(status), rawContent);
    tweet.setCountryCode(Optional.ofNullable(status.getPlace()).map(Place::getCountryCode).orElse(null));
    if (status.getInReplyToStatusId() != -1L && status.getInReplyToUserId() != -1L
            && status.getInReplyToScreenName() != null) {
        tweet.setInReplyTo(new InReplyTo(status.getInReplyToStatusId(), status.getInReplyToScreenName(),
                status.getInReplyToUserId()));
    }
    tweet.setLang(status.getLang());
    tweet.setLocation(Optional.ofNullable(status.getGeoLocation())
            .map(g -> new TweetEntity.Location(g.getLatitude(), g.getLongitude())).orElse(null));
    // TODO Handle quoted tweets
    return this.tweetRepository.save(tweet);
}

From source file:at.illecker.storm.commons.util.io.JsonUtils.java

License:Apache License

public static List<Status> readTweets(InputStream tweetsFile, String filterLanguage) {
    List<Status> tweets = new ArrayList<Status>();
    InputStreamReader isr = null;
    BufferedReader br = null;/*from ww w .  j  ava 2s.  c  o  m*/
    try {
        isr = new InputStreamReader(tweetsFile, "UTF-8");
        br = new BufferedReader(isr);
        String rawJSON = "";
        while ((rawJSON = br.readLine()) != null) {
            // rawJSON may include multiple status objects within one line
            String regex = "\"created_at\":";
            String[] rawJSONTweets = rawJSON.split("\\}\\{" + regex);

            if (rawJSONTweets.length == 0) { // only one object
                try {
                    Status status = TwitterObjectFactory.createStatus(rawJSON);
                    tweets.add(status);
                    // LOG.info("@" + status.getUser().getScreenName() + " - "
                    // + status.getText());
                } catch (TwitterException twe) {
                    LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                }

            } else { // read multiple objects
                for (int j = 0; j < rawJSONTweets.length; j++) {
                    if (j == 0) {
                        rawJSONTweets[j] = rawJSONTweets[j] + "}";
                    } else if (j == rawJSONTweets.length) {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j];
                    } else {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j] + "}";
                    }
                    try {
                        Status status = TwitterObjectFactory.createStatus(rawJSONTweets[j]);
                        if (filterLanguage != null) {
                            if (status.getLang().equals(filterLanguage)) {
                                tweets.add(status);
                            }
                        } else {
                            tweets.add(status);
                        }
                        // LOG.info("@" + status.getUser().getScreenName() + " - "
                        // + status.getText());
                    } catch (TwitterException twe) {
                        LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ignore) {
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ignore) {
            }
        }
    }
    LOG.info("Loaded " + tweets.size() + " tweets");
    return tweets;
}

From source file:bditac.TwitterCaptura.java

public static void main(String[] args) throws TwitterException {

    int criid, apiid, capid;

    if (args.length == 3) {
        criid = Integer.parseInt(args[0]);
        apiid = Integer.parseInt(args[1]);
        capid = Integer.parseInt(args[2]);
    } else {//ww  w  .  j  a  va 2s.  c  om
        criid = 1;
        apiid = 1;
        capid = 1;
    }

    CriseJpaController crijpa = new CriseJpaController(factory);

    Crise crise = crijpa.findCrise(criid);

    CriseApiJpaController criapijpa = new CriseApiJpaController(factory);

    CriseApi criapi = criapijpa.findCriseApi(capid);

    ApiJpaController apijpa = new ApiJpaController(factory);

    Api api = apijpa.findApi(apiid);

    CidadeJpaController cidjpa = new CidadeJpaController(factory);

    Cidade cidade = cidjpa.findCidade(crise.getCidId());

    String coords[] = crise.getCriRegiao().split(",");

    TwitterGeo geo = new TwitterGeo(coords, cidade.getCidNome(), crise.getCriGeotipo());

    Thread threadGeo = new Thread(geo);

    TwitterMens mens = new TwitterMens(crise.getCrtId());

    Thread threadMens = new Thread(mens);

    TwitterGravar gravar = new TwitterGravar();

    Thread threadGravar = new Thread(gravar);

    threadGeo.setName("ThreadGeo");
    threadGeo.start();

    threadMens.setName("ThreadMens");
    threadMens.start();

    threadGravar.setName("ThreadGravar");
    threadGravar.start();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(criapi.getCapKey())
            .setOAuthConsumerSecret(criapi.getCapSecret()).setOAuthAccessToken(criapi.getCapToken())
            .setOAuthAccessTokenSecret(criapi.getCapTokenSecret());

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

    String texto = "[\\xF0\\x9F]";

    StatusListener listener;
    listener = new StatusListener() {
        int cont = 0;

        @Override
        public void onStatus(Status status) {
            if (!(status.getText().contains(texto) || status.getText().isEmpty())) {
                Ocorrencia ocor = new Ocorrencia();
                ocor.setApiId(apiid);
                ocor.setCriId(criid);
                ocor.setCapId(capid);

                ocor.setOcrIdApi(status.getId());
                ocor.setOcrCriacao(status.getCreatedAt());
                ocor.setOcrTexto(status.getText());
                //                    System.out.println(ocor.getOcrTexto());
                ocor.setOcrUsuId(status.getUser().getId());
                ocor.setOcrUsuNome(status.getUser().getName());
                ocor.setOcrUsuScreenNome(status.getUser().getScreenName());
                ocor.setOcrFonte(status.getSource());
                ocor.setOcrLingua(status.getLang());
                ocor.setOcrFavorite(status.getFavoriteCount());
                ocor.setOcrRetweet(status.getRetweetCount());
                String coords = "";
                if (status.getPlace() == null) {
                    ocor.setOcrPaisCodigo("");
                    ocor.setOcrPais("");
                    ocor.setOcrLocal("");
                } else {
                    ocor.setOcrPaisCodigo(status.getPlace().getCountryCode());
                    ocor.setOcrPais(status.getPlace().getCountry());
                    ocor.setOcrLocal(status.getPlace().getFullName());
                    GeoLocation locs[][] = status.getPlace().getBoundingBoxCoordinates();
                    for (int x = 0; x < locs.length; x++) {
                        for (int y = 0; y < locs[x].length; y++) {
                            coords += "[" + locs[x][y].getLongitude() + "," + locs[x][y].getLatitude() + "]";
                            if (!(x == locs.length - 1 && y == locs[x].length - 1)) {
                                coords += ",";
                            }
                        }
                    }
                }

                ocor.setOcrCoordenadas(coords);
                ocor.setOcrGeo('0');
                ocor.setOcrIdentificacao('0');
                ocor.setOcrIdenper(0.0f);
                ocor.setOcrGravado('0');
                ocor.setOcrSentimento('0');
                ocor.setOcrTempo('0');

                boolean add = ocors.add(ocor);

                cont++;

                if (ocors.size() > 1000) {
                    Limpar();
                }

                //                    System.out.println(cont+" - "+status.getId() + " - " + status.getCreatedAt() + status.getPlace().getFullName());
            }
        }

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

        private void Limpar() {
            while (!threadGeo.isInterrupted()) {
                threadGeo.interrupt();
            }
            while (!threadMens.isInterrupted()) {
                threadMens.interrupt();
            }
            while (!threadGravar.isInterrupted()) {
                threadGravar.interrupt();
            }
            boolean achou = true;
            int x = 0;
            System.out.println("Removendo: " + ocors.size());
            while (x < ocors.size()) {
                if (ocors.get(x).getOcrGravado() != '0') {
                    ocors.remove(x);
                } else {
                    x++;
                }
            }
            System.out.println("Final: " + ocors.size());
            if (!threadGeo.isAlive()) {
                threadGeo.start();
            }
            if (!threadMens.isAlive()) {
                threadMens.start();
            }
            if (!threadGravar.isAlive()) {
                threadGravar.start();
            }
        }
    };

    FilterQuery filter = new FilterQuery();

    double[][] location = new double[2][2];

    location[0][0] = Double.parseDouble(coords[0]);
    location[0][1] = Double.parseDouble(coords[1]);
    location[1][0] = Double.parseDouble(coords[4]);
    location[1][1] = Double.parseDouble(coords[5]);

    filter.locations(location);

    twitterStream.addListener(listener);

    twitterStream.filter(filter);
}

From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.StreamReaderService.java

public void readTwitterFeed() {

    TwitterStream stream = TwitterStreamBuilderUtil.getStream();

    StatusListener listener = new StatusListener() {

        @Override//from  w  w w  .j  a v a 2 s  . c om
        public void onException(Exception e) {
        }

        @Override
        public void onTrackLimitationNotice(int n) {
        }

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("en")) {
                System.out.println(status.getText());
            }
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }
    };

    stream.addListener(listener);
    FilterQuery f = new FilterQuery();
    f.language(new String[] { "en" });
    f.follow(new long[] { 816653 });
    stream.filter(f);

    try {
        Thread.sleep(60000);
    } catch (InterruptedException ex) {
        Logger.getLogger(StreamReaderService.class.getName()).log(Level.SEVERE, null, ex);
    }
    stream.shutdown();
}

From source file:cats.twitter.collect.Collect.java

@Override
public boolean runCollect() {

    dateEnd = Calendar.getInstance();
    dateEnd.add(Calendar.DAY_OF_YEAR, duree);
    cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//from  w w w.java  2 s . co m
    cb.setOAuthConsumerKey(user.getConsumerKey());
    System.out.println("CONSUMER KEY " + user.getConsumerKey());
    cb.setOAuthConsumerSecret(user.getConsumerSecret());
    System.out.printf("CONSUMER SECRET " + user.getConsumerSecret());
    cb.setOAuthAccessToken(user.getToken());
    System.out.printf("TOKEN" + user.getToken());
    cb.setOAuthAccessTokenSecret(user.getTokenSecret());
    System.out.printf("TOKEN SECRET " + user.getTokenSecret());

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

    setStatus(State.WAITING_FOR_CONNECTION);

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            if (!corpus.getState().equals(State.INPROGRESS)) {
                setStatus(State.INPROGRESS);
            }
            if (status.getCreatedAt().after(dateEnd.getTime())) {
                shutdown();

            } else if (corpus.getLang() == null || corpus.getLang().equals(status.getLang())) {
                Tweet t = new Tweet();
                t.setText(status.getText().replace("\r", "\n"));
                t.setAuthor(status.getUser().getId());
                t.setId(status.getId());
                t.setDate(status.getCreatedAt());
                if (status.getGeoLocation() != null)
                    t.setLocation(status.getGeoLocation().toString());
                t.setName(status.getUser().getName());
                t.setDescriptionAuthor(status.getUser().getDescription());
                t.setLang(status.getLang());
                t.setCorpus(corpus);
                if (tweetRepository != null)
                    tweetRepository.save(t);
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
            System.out.println(sdn);
        }

        @Override
        public void onTrackLimitationNotice(int i) {
            corpus.setLimitationNotice(i);
            corpus = corpusRepository.save(corpus);

        }

        @Override
        public void onScrubGeo(long l, long l1) {
            System.out.println(l + "" + l1);
        }

        @Override
        public void onStallWarning(StallWarning sw) {
            System.out.println(sw);
        }

        @Override
        public void onException(Exception excptn) {
            corpus.setErrorMessage(excptn.getMessage());
            setStatus(State.ERROR);
            excptn.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.filter(filter);

    return false;
}

From source file:ch.schrimpf.core.TwitterCrawler.java

License:Open Source License

/**
 * Performs a single crawl step according to the previous initialized query
 * until the specified limit is reached. Received tweets are stored in the
 * *.csv specified in the easyTwitterCrawler.properties file.
 * <p/>//  w  w w. j  a v a2 s.co m
 * TODO make selecting values flexible
 *
 * @param limit to stop on
 */
public void crwal(int limit) {
    LOG.info("receiving tweets...");
    int i = 0;
    while (i < limit && running) {
        try {
            QueryResult res = twitter.search(query);
            if (res.getMaxId() > last) {
                for (Status status : res.getTweets()) {
                    String[] line = { String.valueOf(status.getId()), String.valueOf(status.getCreatedAt()),
                            status.getText(), String.valueOf(status.getUser()),
                            String.valueOf(status.getPlace()), status.getLang() };
                    csv.writeResult(Arrays.asList(line));
                    i++;
                }
                last = res.getMaxId();
            } else {
                break;
            }
        } catch (TwitterException e) {
            LOG.warning("could not process tweets");
        }
    }
    tweets += i;
    LOG.info(i + " tweets received in this crawl");
    LOG.info("totally " + tweets + " received");
}

From source file:co.cask.cdap.template.etl.realtime.source.TwitterSource.java

License:Apache License

private StructuredRecord convertTweet(Status tweet) {
    StructuredRecord.Builder recordBuilder = StructuredRecord.builder(this.schema);
    recordBuilder.set(ID, tweet.getId());
    recordBuilder.set(MSG, tweet.getText());
    recordBuilder.set(LANG, tweet.getLang());
    Date tweetDate = tweet.getCreatedAt();
    if (tweetDate != null) {
        recordBuilder.set(TIME, tweetDate.getTime());
    }//from   www. jav a  2 s . c  o m
    recordBuilder.set(FAVC, tweet.getFavoriteCount());
    recordBuilder.set(RTC, tweet.getRetweetCount());
    recordBuilder.set(SRC, tweet.getSource());
    if (tweet.getGeoLocation() != null) {
        recordBuilder.set(GLAT, tweet.getGeoLocation().getLatitude());
        recordBuilder.set(GLNG, tweet.getGeoLocation().getLongitude());
    }
    recordBuilder.set(ISRT, tweet.isRetweet());
    return recordBuilder.build();
}

From source file:co.cask.hydrator.plugin.realtime.source.TwitterSource.java

License:Apache License

private StructuredRecord convertTweet(Status tweet) {
    StructuredRecord.Builder recordBuilder = StructuredRecord.builder(SCHEMA);
    recordBuilder.set(ID, tweet.getId());
    recordBuilder.set(MSG, tweet.getText());
    recordBuilder.set(LANG, tweet.getLang());
    Date tweetDate = tweet.getCreatedAt();
    if (tweetDate != null) {
        recordBuilder.set(TIME, tweetDate.getTime());
    }//from ww w  . java 2  s  .  c  o  m
    recordBuilder.set(FAVC, tweet.getFavoriteCount());
    recordBuilder.set(RTC, tweet.getRetweetCount());
    recordBuilder.set(SRC, tweet.getSource());
    if (tweet.getGeoLocation() != null) {
        recordBuilder.set(GLAT, tweet.getGeoLocation().getLatitude());
        recordBuilder.set(GLNG, tweet.getGeoLocation().getLongitude());
    }
    recordBuilder.set(ISRT, tweet.isRetweet());
    return recordBuilder.build();
}

From source file:com.ebay.pulsar.twittersample.channel.TwitterSampleChannel.java

License:GNU General Public License

@Override
public void open() throws EventException {
    super.open();
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(false);//from  w w  w  .j  a va2  s  . c  o m

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {
        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

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

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
        }

        @Override
        public void onStallWarning(StallWarning warning) {
        }

        @Override
        public void onStatus(Status status) {
            HashtagEntity[] hashtagEntities = status.getHashtagEntities();

            JetstreamEvent event = new JetstreamEvent();
            event.setEventType("TwitterSample");

            Place place = status.getPlace();
            if (place != null) {
                event.put("country", place.getCountry());
            }
            event.put("ct", status.getCreatedAt().getTime());
            event.put("text", status.getText());
            event.put("lang", status.getLang());
            event.put("user", status.getUser().getName());
            if (hashtagEntities != null && hashtagEntities.length > 0) {
                StringBuilder s = new StringBuilder();
                s.append(hashtagEntities[0].getText());

                for (int i = 1; i < hashtagEntities.length; i++) {
                    s.append(",");
                    s.append(hashtagEntities[i].getText());
                }

                event.put("hashtag", s.toString());
            }

            fireSendEvent(event);
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample();
}