Example usage for twitter4j Status isRetweet

List of usage examples for twitter4j Status isRetweet

Introduction

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

Prototype

boolean isRetweet();

Source Link

Usage

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.jav  a2 s  .c  o m*/
                } catch (UnsupportedEncodingException exc) {
                }
            }
        }
    }
}

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 {//w ww.ja  v a  2 s  . co  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 {//from w ww .ja v a2 s . com
            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:aic2013.extractor.TwitterExtractor.java

public static void main(String[] args) throws Exception {
    EntityManagerFactory emf = null;/*ww  w . j a v  a  2 s  .  c  om*/
    MongoClient mongoClient = null;
    Twitter twitter = null;
    Neo4jConnection neo4j = null;

    try {
        // Database configuration
        Properties prop = new Properties();
        prop.load(new FileInputStream("hibernate.properties"));
        // prop.load(TwitterExtractor.class.getClassLoader().getResourceAsStream("hibernate.properties"));

        emf = Persistence.createEntityManagerFactory("twitterdb", prop);
        final String mongoServer = "localhost";
        mongoClient = new MongoClient(mongoServer);
        twitter = TwitterFactory.getSingleton();

        //         final String neo4jJdbc = "jdbc:neo4j:ec2-54-217-131-208.eu-west-1.compute.amazonaws.com:7474";
        final String neo4jJdbc = "jdbc:neo4j://localhost:7474";
        neo4j = new Driver().connect(neo4jJdbc, new Properties());
        neo4j.setAutoCommit(true);//false);

        final TwitterDataAccess twitterDataAccess = new TwitterDataAccess(twitter);
        final MongoDataAccess mongoDataAccess = new MongoDataAccess(mongoClient);
        final Neo4jService neo4jService = new Neo4jService(neo4j);
        final EntityManager em = emf.createEntityManager();
        final UserDataAccess userDataAcces = new UserDataAccess(em);

        final TopicExtractionCoordinator extractionCoordinator = new TopicExtractionCoordinatorImpl();

        //         final ConcurrentLinkedQueue<E>
        final UserService userService = new UserService(em);
        final Processor<Status> mongoProcessor = new Processor<Status>() {

            @Override
            public void process(final Status status) {
                final TwitterUser user = new TwitterUser(status.getUser());
                userService.persist(user);
                neo4jService.transactional(new Neo4jUnitOfWork() {

                    @Override
                    public void process() throws SQLException {
                        neo4jService.createPersonIfAbsent(user);
                        // HashtagEntity[] topics =
                        // status.getHashtagEntities();
                        Status originalStatus = status;
                        if (status.isRetweet()) {
                            originalStatus = status.getRetweetedStatus();
                        }

                        extractionCoordinator.doExtraction(originalStatus, new TopicExtractionCallback() {

                            @Override
                            public void handleExtractionResult(Set<Topic> extractedTopics) {
                                /* TODO: comment out outputs */
                                //                              System.out.println(status.getText());
                                //                              for (Topic t : extractedTopics) {
                                //                                 System.out.println(t.getName());
                                //                              }
                                try {
                                    /* add hash tags to topics */
                                    for (HashtagEntity tag : status.getHashtagEntities()) {
                                        extractedTopics.add(new Topic(new String[] { tag.getText() }));
                                    }
                                    for (Topic topic : extractedTopics) {
                                        neo4jService.createTopicIfAbsent(topic);
                                        if (status.isRetweet()) {
                                            neo4jService.createRelationIfAbsent("RETWEETS", user, topic);
                                        } else {
                                            neo4jService.createRelationIfAbsent("TWEETS", user, topic);
                                        }
                                    }
                                } catch (SQLException e) {
                                    throw new RuntimeException(e);
                                }

                            }

                            @Override
                            public void handleExtractionError(ExtractionException e) {
                                throw new RuntimeException(e);
                            }
                        });
                    }
                });
            }
        };
        final Processor<TwitterUser> userProcessor = new Processor<TwitterUser>() {

            @Override
            public void process(final TwitterUser user) {
                try {
                    twitterDataAccess.forAllFollowers(user, new Processor<User>() {

                        @Override
                        public void process(User u) {
                            final TwitterUser follower = em.find(TwitterUser.class, u.getId());

                            if (follower != null) {
                                neo4jService.transactional(new Neo4jUnitOfWork() {

                                    @Override
                                    public void process() throws SQLException {
                                        neo4jService.createUniqueRelation("FOLLOWS", user, follower);
                                    }
                                });
                            }
                        }
                    });
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        };

        // invoke the processor for every entry in mongo
        mongoDataAccess.forAll(mongoProcessor);
        extractionCoordinator.awaitTermination(-1, TimeUnit.SECONDS);
        // invoke the processor for every entry in the rdbms
        //         userDataAcces.forAll(userProcessor);
    } finally {
        if (emf != null && emf.isOpen()) {
            emf.close();
        }
        if (mongoClient != null) {
            mongoClient.close();
        }
        if (twitter != null) {
            twitter.shutdown();
        }
        if (neo4j != null) {
            neo4j.close();
        }
    }
}

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. java2s.  com*/

    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());
    }//from w  ww . j a va  2s.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   www.  ja v  a  2  s.  co 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.thehotnews.lambda.TheHotNewsSpeechlet.java

License:Open Source License

private SpeechletResponse getCurrentStatus(String name, String screenName) {
    List<Status> statuses = null;
    try {/*from   w w  w  . j  a  va 2s .  c o m*/
        Twitter twitter = twitterFactory.getInstance();
        statuses = twitter.getUserTimeline(screenName);

    } catch (Exception e) {
        log.error("Problem getting current status for " + screenName, e);
        return getErrorResponse();
    }

    Status status = statuses.get(0);

    String statusText = ResponseUtils.removeUrl(status.getText());
    if (statusText == null || statusText.trim().length() == 0) {
        // assume there was only a link in the tweet.
        statusText = " a link. ";
    }

    String verb = status.isRetweet() ? " retweeted: " : " tweeted: ";
    String speechText = name + verb + statusText;
    String cardText = name + verb + status.getText();
    String mediaURl = null;
    if (status.getMediaEntities() != null && status.getMediaEntities().length > 0) {
        mediaURl = status.getMediaEntities()[0].getMediaURLHttps();
    }

    StandardCard card = new StandardCard();
    card.setTitle(APP_NAME);
    card.setText(cardText);
    if (mediaURl != null) {
        Image image = new Image();
        image.setLargeImageUrl(mediaURl);
        card.setImage(image);
    }

    // Create the plain text output.
    PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
    speech.setText(speechText);

    return SpeechletResponse.newTellResponse(speech, card);
}

From source file:collector.TwitterCollector.java

public LinkedHashSet<Tweet> userSearchData(String userName, int maxResults) {

    LinkedHashSet<Tweet> out = new LinkedHashSet<>();
    Paging paging = new Paging(1, 180);
    int numberOfTweets = maxResults;//512;
    long lastID = Long.MAX_VALUE;
    ArrayList<Status> status = new ArrayList<>();
    while (status.size() < numberOfTweets) {
        if (numberOfTweets - status.size() > 180) {//100) {
            paging.setCount(180);//100);
        } else {/* www.ja v a2s . c o  m*/
            paging.setCount(numberOfTweets - status.size());
        }
        try {
            List<Status> timeLine = twitter.getUserTimeline(userName, paging);
            status.addAll(timeLine);
            for (Status t : status) {
                if (t.getId() < lastID) {
                    lastID = t.getId();
                }
            }
        } catch (TwitterException ex) {
            System.err.println(ex.getMessage());
        }
        paging.setMaxId(lastID - 1);
    }

    //armazenar os atributos interessantes a analise dos tweets
    int qtdretweet = 0;
    for (Status sta : status) {
        String text = sta.getText();
        if (!sta.isRetweet() && !sta.isRetweeted() && !text.startsWith("RT")) { //&& !TweetMediaDetect.detect(text)) {
            TwitterUser user;
            user = new TwitterUser().addID(sta.getUser().getId()).addName(sta.getUser().getName())
                    .addLocation(sta.getUser().getLocation()).addDateSignin(sta.getUser().getCreatedAt())
                    .addCountTweets(sta.getUser().getStatusesCount())
                    .addCountFavorites(sta.getUser().getFavouritesCount())
                    .addCountFriends(sta.getUser().getFriendsCount())
                    .addCountFollowers(sta.getUser().getFollowersCount());
            Tweet tweet = new Tweet().addUser(user).addText(sta.getText()).addID(sta.getId())
                    .addDate(sta.getCreatedAt())
                    .addLatitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLatitude()
                            : Double.MAX_VALUE)
                    .addLongitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLongitude()
                            : Double.MAX_VALUE);
            out.add(tweet);
        } else {
            qtdretweet++;
        }
    }

    return out;
}

From source file:com.alainesp.fan.sanderson.SummaryFragment.java

License:Open Source License

protected int doWork() {
    int state = DownloadParseSaveTask.STATE_SUCCESS;

    try {/* w  w w  . j a v a 2 s .c o  m*/
        List<twitter4j.Status> tweets;

        // Twitter client configuration
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(TwitterAPISecrets.CONSUMER_KEY)
                .setOAuthConsumerSecret(TwitterAPISecrets.CONSUMER_SECRET);
        // TODO: Use guest authentication instead of application
        builder.setApplicationOnlyAuthEnabled(true).setDebugEnabled(false).setGZIPEnabled(true);

        Twitter twitter = new TwitterFactory(builder.build()).getInstance();
        twitter.getOAuth2Token();

        // Get the tweets
        long lastTweetID = DB.Tweet.getLastTweetID();
        if (lastTweetID <= 0)
            tweets = twitter.getUserTimeline("BrandSanderson");
        else
            tweets = twitter.getUserTimeline("BrandSanderson", new Paging(lastTweetID));

        if (tweets != null) {
            long brandonID = 28187205;
            Hashtable<Long, DB.Tweet> findTweet = new Hashtable<>(tweets.size() * 16, 0.25f);
            List<DB.Tweet> dbTweets = new ArrayList<>(tweets.size());
            for (twitter4j.Status tweet : tweets) {
                // Create the tweet
                if (tweet.isRetweet())
                    tweet = tweet.getRetweetedStatus();

                long tweetID = tweet.getId();
                // TODO: Include the tweets in the DB
                if (findTweet.get(tweetID) == null)// This eliminate tweets already in the replies tree
                {
                    DB.Tweet dbTweet = new DB.Tweet(tweetID, getTweetText(tweet), tweet.getCreatedAt(), false,
                            tweet.getUser().getName(), tweet.getUser().getBiggerProfileImageURLHttps());
                    InternetHelper.getRemoteFile(tweet.getUser().getBiggerProfileImageURLHttps());
                    dbTweets.add(dbTweet);
                    findTweet.put(tweetID, dbTweet);

                    // Traverse the tree of the replies tweets
                    twitter4j.Status treeTweet = tweet;
                    while (treeTweet != null && treeTweet.getInReplyToStatusId() >= 0) {
                        try {
                            long id = treeTweet.getInReplyToStatusId();
                            long userID = treeTweet.getUser().getId();
                            treeTweet = null;
                            if (findTweet.get(id) == null || brandonID == userID)
                                treeTweet = twitter.showStatus(id);
                            else// Remove duplicates. Not sure why they appear, but the difference of the text is a dot at the end.
                                dbTweets.remove(dbTweets.size() - 1);
                        } catch (Exception ignore) {
                        }

                        if (treeTweet != null) {
                            findTweet.put(treeTweet.getId(), dbTweet);
                            StringBuilder replyBuilder = new StringBuilder();

                            replyBuilder.append("<blockquote>");

                            // Profile image
                            replyBuilder.append("<img src=\"");
                            InternetHelper.getRemoteFile(treeTweet.getUser().getBiggerProfileImageURLHttps());
                            replyBuilder.append(treeTweet.getUser().getBiggerProfileImageURLHttps());
                            replyBuilder.append("\"/>&nbsp;<b>");

                            // Username - date
                            replyBuilder.append(treeTweet.getUser().getName());
                            replyBuilder.append("</b> @");
                            replyBuilder.append(treeTweet.getUser().getScreenName());
                            replyBuilder.append(" - ");
                            replyBuilder
                                    .append(TwitterFragment.showDateFormat.format(treeTweet.getCreatedAt()));

                            // Tweet text
                            replyBuilder.append("<br/>");
                            replyBuilder.append(getTweetText(treeTweet));

                            // Remaining
                            replyBuilder.append(dbTweet.htmlReply);
                            replyBuilder.append("</blockquote>");

                            dbTweet.htmlReply = replyBuilder.toString();
                        }
                    }
                }
            }
            DB.Tweet.updateTwitter(dbTweets);
        }
    } catch (Exception e) {
        Logger.reportError(e.toString());
        state = DownloadParseSaveTask.STATE_ERROR_PARSING;
    }

    return state;
}