Example usage for twitter4j Status getSource

List of usage examples for twitter4j Status getSource

Introduction

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

Prototype

String getSource();

Source Link

Document

Returns the source

Usage

From source file:TimeLine.java

private void btTwitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTwitActionPerformed
    // Tombol update status, show setelah update
    txtStatus.setText("");

    TwitterFactory tf = new TwitterFactory();
    Twitter twitter = tf.getInstance();/*  www .j ava  2  s  . c  o m*/

    try {
        Status status = twitter.updateStatus(txStatus.getText());
        JOptionPane.showMessageDialog(rootPane, "Twit \n [ " + status.getText() + " ]\nTerkirim!");
    } catch (TwitterException ex) {
        JOptionPane.showMessageDialog(rootPane, "Tidak bisa mengirim : " + ex.getMessage());
        Logger.getLogger(TimeLine.class.getName()).log(Level.SEVERE, null, ex);
    }

    List<Status> statuses = null;
    try {
        statuses = twitter.getUserTimeline();

        statuses.stream().forEach((status) -> {
            txtStatus.append(status.getUser().getName() + " : " + status.getText() + " - "
                    + status.getCreatedAt() + " \n Via : " + status.getSource() + "\n\n");
        });

        txStatus.setText(""); //reload field
    } catch (TwitterException te) {
        JOptionPane.showMessageDialog(rootPane, "Failed to Show Status!" + te.getMessage());
    }
}

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

License:Apache License

String extractSource(final Status status) {
    return Optional.ofNullable(status.getSource()).map(String::trim).filter(s -> !s.isEmpty())
            .map(SOURCE_PATTERN::matcher).filter(Matcher::matches).map(m -> m.group(1).trim()).orElse(null);
}

From source file:au.net.moon.tSearchArchiver.SearchArchiver.java

License:Open Source License

SearchArchiver() {

    Twitter twitter;//  ww  w  . j  av a  2s . c  o m
    int waitBetweenRequests = 2000;
    // 2 sec delay between requests to avoid maxing out the API.
    Status theTweet;
    Query query;
    QueryResult result;

    // String[] searches;
    ArrayList<String> searchQuery = new ArrayList<String>();
    ArrayList<Integer> searchId = new ArrayList<Integer>();

    int searchIndex;
    int totalTweets;
    SimpleDateFormat myFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");

    System.out.println("tSearchArchiver: Loading search queries...");

    // Set timezone to UTC for the Twitter created at dates
    myFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    twitterAuthorise twitterAuth = new twitterAuthorise(false);
    twitter = twitterAuth.getTwitter();

    // Open the old twitter_archive database
    openSQLDataBase();

    if (isDatabaseReady()) {

        // probably should have these in an object not separate arrays?
        try {
            rs = stmt.executeQuery("select * from searches where active = true");
            // perform each search
            while (rs.next()) {
                // if (searchQuery
                searchQuery.add(rs.getString("query"));
                searchId.add(rs.getInt("id"));
            }
            if (rs.wasNull()) {
                System.out.println("tSearchArchiver: No searches in the table \"searches\"");
                System.exit(30);
            } else {
                System.out.println("tSearchArchiver: Found " + searchQuery.size() + " searches.");
            }
        } catch (SQLException e) {
            System.out.println("tSearchArchiver: e:" + e.toString());
        }

        searchIndex = 0;
        totalTweets = 0;

        // set initial value of i to start from middle of search set
        while (searchIndex < searchQuery.size()) {

            query = new Query();
            query.setQuery(searchQuery.get(searchIndex));
            // check to see if their are any tweets already in the database for
            // this search
            //TODO: Change this to look in new raw data files for each search instead
            long max_tw_id = 0;
            try {
                rs = stmt.executeQuery("select max(tweet_id) as max_id from archive where search_id = "
                        + searchId.get(searchIndex));
                if (rs.next()) {
                    max_tw_id = rs.getLong("max_id");
                    // System.out.println("MaxID: " + max_tw_id);
                    query.setSinceId(max_tw_id);
                }
            } catch (SQLException e1) {
                System.err.println("tSearchArchiver: Error looking for maximum tweet_id for " + query.getQuery()
                        + " in archive");
                e1.printStackTrace();
            }
            // System.out.println("Starting searching for tweets for: " +
            // query.getQuery());

            // new style replacement for pagination
            //   Query query = new Query("whatEverYouWantToSearch"); 
            //   do { 
            //       result = twitter.search(query); 
            //       System.out.println(result); 
            // do something 
            //      } while ((query = result.nextQuery()) != null); 
            // TODO: check if twitter4j is doing all the backing off handling already

            int tweetCount = 0;
            Boolean searching = true;
            do {

                // delay waitBetweenRequests milliseconds before making request
                // to make sure not overloading API
                try {
                    Thread.sleep(waitBetweenRequests);
                } catch (InterruptedException e1) {
                    System.err.println("tSearchArchiver: Sleep between requests failed.");
                    e1.printStackTrace();
                }
                try {
                    result = twitter.search(query);
                } catch (TwitterException e) {
                    System.out.println(e.getStatusCode());
                    System.out.println(e.toString());
                    if (e.getStatusCode() == 503) {
                        // TODO use the Retry-After header value to delay & then
                        // retry the request
                        System.out
                                .println("tSearchArchiver: Delaying for 10 minutes before making new request");
                        try {
                            Thread.sleep(600000);
                        } catch (InterruptedException e1) {
                            System.err.println(
                                    "tSearchArchiver: Sleep for 10 minutes because of API load failed.");
                            e1.printStackTrace();
                        }
                    }
                    result = null;
                }

                if (result != null) {
                    List<Status> results = result.getTweets();
                    if (results.size() == 0) {
                        searching = false;
                    } else {
                        tweetCount += results.size();
                        for (int j = 0; j < results.size(); j++) {
                            theTweet = (Status) results.get(j);
                            String cleanText = theTweet.getText();
                            cleanText = cleanText.replaceAll("'", "&#39;");
                            cleanText = cleanText.replaceAll("\"", "&quot;");

                            try {
                                stmt.executeUpdate("insert into archive values (0, " + searchId.get(searchIndex)
                                        + ", '" + theTweet.getId() + "', now())");
                            } catch (SQLException e) {
                                System.err.println("tSearchArchiver: Insert into archive failed.");
                                System.err.println(searchId.get(searchIndex) + ", " + theTweet.getId());

                                e.printStackTrace();
                            }
                            // TODO: change to storing in file instead of database
                            try {
                                rs = stmt.executeQuery("select id from tweets where id = " + theTweet.getId());
                            } catch (SQLException e) {
                                System.err.println(
                                        "tSearchArchiver: checking for tweet in tweets archive failed.");
                                e.printStackTrace();
                            }
                            Boolean tweetNotInArchive = false;
                            try {
                                tweetNotInArchive = !rs.next();
                            } catch (SQLException e) {
                                System.err.println(
                                        "tSearchArchiver: checking for tweet in archive failed at rs.next().");
                                e.printStackTrace();
                            }
                            if (tweetNotInArchive) {
                                String tempLangCode = "";
                                // getIsoLanguageCode() has been removed from twitter4j
                                // looks like it might be added back in in the next version
                                //                           if (tweet.getIsoLanguageCode() != null) {
                                //                              if (tweet.getIsoLanguageCode().length() > 2) {
                                //                                 System.out
                                //                                 .println("tSearchArchiver Error: IsoLanguageCode too long: >"
                                //                                       + tweet.getIsoLanguageCode()
                                //                                       + "<");
                                //                                 tempLangCode = tweet
                                //                                       .getIsoLanguageCode()
                                //                                       .substring(0, 2);
                                //                              } else {
                                //                                 tempLangCode = tweet
                                //                                       .getIsoLanguageCode();
                                //                              }
                                //                           }
                                double myLatitude = 0;
                                double myLongitude = 0;
                                int hasGeoCode = 0;

                                if (theTweet.getGeoLocation() != null) {
                                    System.out.println("GeoLocation: " + theTweet.getGeoLocation().toString());
                                    myLatitude = theTweet.getGeoLocation().getLatitude();
                                    myLongitude = theTweet.getGeoLocation().getLongitude();
                                    hasGeoCode = 1;
                                }
                                Date tempCreatedAt = theTweet.getCreatedAt();
                                String myDate2 = myFormatter
                                        .format(tempCreatedAt, new StringBuffer(), new FieldPosition(0))
                                        .toString();
                                totalTweets++;
                                try {
                                    stmt.executeUpdate("insert into tweets values  (" + theTweet.getId() + ", '"
                                            + tempLangCode + "', '" + theTweet.getSource() + "', '" + cleanText
                                            + "', '" + myDate2 + "', '" + theTweet.getInReplyToUserId() + "', '"
                                            + theTweet.getInReplyToScreenName() + "', '"
                                            + theTweet.getUser().getId() + "', '"
                                            + theTweet.getUser().getScreenName() + "', '" + hasGeoCode + "',"
                                            + myLatitude + ", " + myLongitude + ", now())");
                                } catch (SQLException e) {
                                    System.err.println("tSearchArchiver: Insert into tweets failed.");
                                    System.err.println(theTweet.getId() + ", '" + tempLangCode + "', '"
                                            + theTweet.getSource() + "', '" + cleanText + "', '" + myDate2
                                            + "', '" + theTweet.getInReplyToUserId() + "', '"
                                            + theTweet.getInReplyToScreenName() + "', '"
                                            + theTweet.getUser().getId() + "', '"
                                            + theTweet.getUser().getScreenName());

                                    e.printStackTrace();
                                }
                            }

                        }
                    }
                }
            } while ((query = result.nextQuery()) != null && searching);

            if (tweetCount > 0) {
                System.out.println("tSearchArchiver: New Tweets Found for \"" + searchQuery.get(searchIndex)
                        + "\" = " + tweetCount);
            } else {
                // System.out.println("tSearchArchiver: No Tweets Found for \""
                // + searchQuery.get(searchIndex) + "\" = " + tweetCount);
            }
            try {

                stmt.executeUpdate("update searches SET lastFoundCount=" + tweetCount
                        + ", lastSearchDate=now() where id=" + searchId.get(searchIndex));
            } catch (SQLException e) {
                System.err.println("tSearchArchiver: failed to update searches with lastFoundCount="
                        + tweetCount + " and datetime for search: " + searchId.get(searchIndex));
                e.printStackTrace();
            }
            searchIndex++;
        }
        System.out.println("tSearchArchiver: Completed all " + searchQuery.size() + " searches");
        System.out.println("tSearchArchiver: Archived " + totalTweets + " new tweets");
    }
}

From source file:au.net.moon.tUtils.twitterFields.java

License:Open Source License

/**
 * Get a <CODE>HashMap</CODE> of tweet fields by parsing a twitter4j Status
 * /*from  www . ja v a  2 s .  c  o m*/
 * @param status
 *            the twitter4j Status object
 * @return the tweet fields as name, value pairs in a <CODE>HashMap</CODE>.
 */
public static HashMap<String, String> parseStatusObj(Status status) {
    HashMap<String, String> splitFields = new HashMap<String, String>();

    splitFields.put("createdAt", status.getCreatedAt().toString());
    splitFields.put("id", Long.toString(status.getId()));
    splitFields.put("text", status.getText());
    splitFields.put("source", status.getSource());
    splitFields.put("isTruncated", status.isTruncated() ? "1" : "0");
    splitFields.put("inReplyToStatusId", Long.toString(status.getInReplyToStatusId()));
    splitFields.put("inReplyToUserId", Long.toString(status.getInReplyToUserId()));
    splitFields.put("isFavorited", status.isFavorited() ? "1" : "0");
    splitFields.put("inReplyToScreenName", status.getInReplyToScreenName());
    if (status.getGeoLocation() != null) {
        splitFields.put("geoLocation", status.getGeoLocation().toString());
    } else {
        splitFields.put("geoLocation", "");
    }
    if (status.getPlace() != null) {
        splitFields.put("place", status.getPlace().toString());
    } else {
        splitFields.put("place", "");
    }
    splitFields.put("retweetCount", Long.toString(status.getRetweetCount()));
    splitFields.put("wasRetweetedByMe", status.isRetweetedByMe() ? "1" : "0");
    String contributors = "";
    if (status.getContributors() != null) {
        long[] tempContributors = status.getContributors();
        for (int i = 0; i < tempContributors.length; i++) {
            contributors += Long.toString(tempContributors[i]);
            if (i != tempContributors.length - 1) {
                contributors += ", ";
            }
        }
    }
    splitFields.put("contributors", contributors);
    splitFields.put("annotations", "");
    if (status.getRetweetedStatus() != null) {
        splitFields.put("retweetedStatus", "1");
    } else {
        splitFields.put("retweetedStatus", "0");
    }
    splitFields.put("userMentionEntities", status.getUserMentionEntities().toString());
    splitFields.put("urlEntities", status.getURLEntities().toString());
    splitFields.put("hashtagEntities", status.getHashtagEntities().toString());
    splitFields.put("user", status.getUser().toString());
    return splitFields;
}

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 {// w  w  w .  j  a  va  2s.  c o  m
        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:br.com.porcelli.hornetq.integration.twitter.support.TweetMessageConverterSupport.java

License:Apache License

public static ServerMessage buildMessage(final String queueName, final Status status) {
    final ServerMessage msg = new ServerMessageImpl(status.getId(),
            InternalTwitterConstants.INITIAL_MESSAGE_BUFFER_SIZE);
    msg.setAddress(new SimpleString(queueName));
    msg.setDurable(true);/*from  w w  w .  ja  v  a 2  s.c o  m*/

    msg.putStringProperty(TwitterConstants.KEY_MSG_TYPE, MessageType.TWEET.toString());
    msg.putStringProperty(TwitterConstants.KEY_CREATED_AT, read(status.getCreatedAt()));
    msg.putStringProperty(TwitterConstants.KEY_ID, read(status.getId()));

    msg.putStringProperty(TwitterConstants.KEY_TEXT, read(status.getText()));
    msg.putStringProperty(TwitterConstants.KEY_SOURCE, read(status.getSource()));
    msg.putStringProperty(TwitterConstants.KEY_TRUNCATED, read(status.isTruncated()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID, read(status.getInReplyToStatusId()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_USER_ID, read(status.getInReplyToUserId()));
    msg.putStringProperty(TwitterConstants.KEY_IN_REPLY_TO_SCREEN_NAME, read(status.getInReplyToScreenName()));

    msg.putStringProperty(TwitterConstants.KEY_RETWEET, read(status.isRetweet()));
    msg.putStringProperty(TwitterConstants.KEY_FAVORITED, read(status.isFavorited()));

    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_URLS_JSON, read(status.getURLEntities()));
    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_HASHTAGS_JSON, read(status.getHashtagEntities()));
    msg.putStringProperty(TwitterConstants.KEY_ENTITIES_MENTIONS_JSON, read(status.getUserMentionEntities()));

    msg.putStringProperty(TwitterConstants.KEY_CONTRIBUTORS_JSON, read(status.getContributors()));

    if (status.getUser() != null) {
        buildUserData("", status.getUser(), msg);
    }

    GeoLocation gl;
    if ((gl = status.getGeoLocation()) != null) {
        msg.putStringProperty(TwitterConstants.KEY_GEO_LATITUDE, read(gl.getLatitude()));
        msg.putStringProperty(TwitterConstants.KEY_GEO_LONGITUDE, read(gl.getLongitude()));
    }

    Place place;
    if ((place = status.getPlace()) != null) {
        msg.putStringProperty(TwitterConstants.KEY_PLACE_ID, read(place.getId()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_URL, read(place.getURL()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_NAME, read(place.getName()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_FULL_NAME, read(place.getFullName()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY_CODE, read(place.getCountryCode()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_COUNTRY, read(place.getCountry()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_STREET_ADDRESS, read(place.getStreetAddress()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_TYPE, read(place.getPlaceType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_GEO_TYPE, read(place.getGeometryType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_TYPE, read(place.getBoundingBoxType()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_COORDINATES_JSON,
                read(place.getBoundingBoxCoordinates().toString()));
        msg.putStringProperty(TwitterConstants.KEY_PLACE_BOUNDING_BOX_GEOMETRY_COORDINATES_JSON,
                read(place.getGeometryCoordinates().toString()));
    }

    msg.putStringProperty(TwitterConstants.KEY_RAW_JSON, status.toString());

    return msg;
}

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());
    }//ww w  .j  av  a2  s . c  om
    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());
    }/* ww w. j a v  a2 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.aremaitch.codestock2010.repository.TweetObj.java

License:Apache License

public static TweetObj createInstance(Status status) {
    TweetObj to = new TweetObj();
    to.setId(status.getId());//from   w  w w .j  a v a 2s  . c o m
    to.setText(status.getText());
    to.setToUserId(status.getInReplyToUserId());
    to.setToUser(status.getInReplyToScreenName());
    if (status.getUser() != null) {
        to.setFromUser(status.getUser().getScreenName());
        to.setFromUserId(status.getUser().getId());
        to.setIsoLanguageCode(status.getUser().getLang());
        to.setProfileImageUrl(status.getUser().getProfileBackgroundImageUrl());
    }
    to.setSource(status.getSource());
    to.setCreatedAt(status.getCreatedAt());
    if (status.getGeoLocation() != null) {
        to.setLatitude(status.getGeoLocation().getLatitude());
        to.setLongitude(status.getGeoLocation().getLongitude());
    }
    return to;

}

From source file:com.daemon.database.Transactor.java

License:Open Source License

/**
 * Saves a given tweet in the DB if that tweet is not already saved. Only saves
 * the tweet no other information!/*  ww  w.  j a  va 2s . co  m*/
 * 
 * @param tweet The tweet to be saved.
 * @throws SQLException
 */
private void saveTweet(Status tweet, RegressionSentimentClassifier sentimentClassifier) throws SQLException {
    // for reweet, save the original tweet first
    if (tweet.getRetweetedStatus() != null) {
        saveAllTransactionSafe(tweet.getRetweetedStatus(), null, sentimentClassifier);
    }
    // then, save the current tweet

    // 1: Set Tweet ID
    prepStatementTweet.setLong(1, tweet.getId());

    // 2 / 3: Set GeoLocation
    if (tweet.getGeoLocation() != null) {
        prepStatementTweet.setFloat(2, (float) tweet.getGeoLocation().getLatitude());
        prepStatementTweet.setFloat(3, (float) tweet.getGeoLocation().getLongitude());
    } else {
        prepStatementTweet.setNull(2, java.sql.Types.NULL);
        prepStatementTweet.setNull(3, java.sql.Types.NULL);
    }

    // 4: Set User ID
    prepStatementTweet.setLong(4, tweet.getUser().getId());

    // 5: Set Reply-Tweet ID
    if (tweet.getInReplyToStatusId() == -1) {
        prepStatementTweet.setNull(5, java.sql.Types.NULL);
    } else {
        prepStatementTweet.setLong(5, tweet.getInReplyToStatusId());
    }

    // 6: Set Retweet-ID
    if (tweet.getRetweetedStatus() != null) {
        prepStatementTweet.setLong(6, tweet.getRetweetedStatus().getId());
    } else {
        prepStatementTweet.setNull(6, java.sql.Types.NULL);
    }

    // 7: Set Creation Date of Tweet
    java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(tweet.getCreatedAt().getTime());
    prepStatementTweet.setTimestamp(7, sqlTimestamp);

    // 8-11: Other attributes:
    prepStatementTweet.setString(8, tweet.getSource());
    prepStatementTweet.setString(9, tweet.getText());
    prepStatementTweet.setString(10, tweet.getIsoLanguageCode());
    prepStatementTweet.setInt(11, tweet.getRetweetCount());

    // 12: Sentiment
    Float sentiment = sentimentClassifier.determineSentiment(tweet.getText(), tweet.getIsoLanguageCode());
    if (sentiment == null) {
        prepStatementTweet.setNull(12, java.sql.Types.NULL);
    } else {
        prepStatementTweet.setFloat(12,
                sentimentClassifier.determineSentiment(tweet.getText(), tweet.getIsoLanguageCode()));
    }

    // execute statement
    prepStatementTweet.addBatch();
}