Example usage for twitter4j Status getHashtagEntities

List of usage examples for twitter4j Status getHashtagEntities

Introduction

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

Prototype

HashtagEntity[] getHashtagEntities();

Source Link

Document

Returns an array if hashtag mentioned in the tweet.

Usage

From source file:adapter.TwitterAllAdapter.java

License:Apache License

@Override
public void run() {

    while (true) {

        try {//from   w  w  w . jav  a2s.  co  m
            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 {// w  w w.j a va2  s  .c  om

            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");
        }

    }
}

From source file:adapter.TwitterLanguageAdapter.java

License:Apache License

@Override
public void run() {

    while (true) {
        try {/*ww  w  . jav a 2 s  . 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");
        }

    }
}

From source file:adapter.TwitterLocationAdapter.java

License:Apache License

@Override
public void run() {

    while (true) {
        try {/*from   w  ww  . j a va  2s  . c om*/
            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");
        }

    }
}

From source file:aic2013.extractor.TwitterExtractor.java

public static void main(String[] args) throws Exception {
    EntityManagerFactory emf = null;/*from www . j  a v  a 2s. 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:au.net.moon.tUtils.twitterFields.java

License:Open Source License

/**
 * Get a <CODE>HashMap</CODE> of tweet fields by parsing a twitter4j Status
 * //from  w  w  w  .ja  va2  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: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);// w  ww  . j  a  va 2  s.c om

    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.tigon.analytics.TweetCollector.java

License:Apache License

@Tick(unit = TimeUnit.MILLISECONDS, delay = 100)
public void collect() throws InterruptedException {
    if (this.queue == null) {
        // Sleep and return if public timeline is disabled
        Thread.sleep(1000);/*from w  ww .j  a  va2s .c o m*/
        return;
    }
    int batchSize = 100;

    for (int i = 0; i < batchSize; i++) {
        Status tweet = queue.poll();
        if (tweet == null) {
            break;
        }

        // emitting more data to get higher throughput
        for (int k = 0; k < tweetAmplification; k++) {
            List<String> hashtags = Lists.newArrayList();
            for (HashtagEntity hashtag : tweet.getHashtagEntities()) {
                hashtags.add(hashtag.getText());
            }
            output.emit(new SimpleTweet(tweet.getText(), hashtags));
        }
    }
}

From source file:com.cask.twitter.TweetStream.java

License:Apache License

@Override
public void initialize(FlowletContext context) {
    statusListener = new StatusListener() {
        @Override/*from   w ww.j a v  a 2  s. co  m*/
        public void onStatus(Status status) {
            for (HashtagEntity hash : status.getHashtagEntities()) {
                tweetQ.add(hash.getText());
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int i) {

        }

        @Override
        public void onScrubGeo(long l, long l2) {

        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {

        }

        @Override
        public void onException(Exception e) {
            LOG.error(e.getMessage());
        }
    };

    cb = new ConfigurationBuilder();
    cb.setDebugEnabled(false)
            //Provide Twitter Credentials through Runtime Arguments.
            .setOAuthConsumerKey(context.getRuntimeArguments().get("ConsumerKey"))
            .setOAuthConsumerSecret(context.getRuntimeArguments().get("ConsumerSecret"))
            .setOAuthAccessToken(context.getRuntimeArguments().get("AccessToken"))
            .setOAuthAccessTokenSecret(context.getRuntimeArguments().get("AccessTokenSecret"));

    tStream = new TwitterStreamFactory(cb.build()).getInstance();
    //For testing, we can disable Twitter Stream and use testStream to send sample hashTags.
    if (!context.getRuntimeArguments().containsKey("disableLiveStream")) {
        tStream.addListener(statusListener);
        tStream.sample();
    }
}

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

License:Open Source License

/**
 * Adds for each Hashtag in a given tweet an SQL Batch Entry in the 
 * table 'hashtags' and also for the relation between tweet and hashtag in
 * 'tweets_has_hashtags' the storing of the batch takes place in the
 * saveAllTransactionSafe./* ww  w. j a  v  a 2  s .c  o m*/
 * 
 * @param tweet The tweet whose hashtags shall be saved.
 * @throws SQLException
 */
private void saveTweetsHashtags(Status tweet) throws SQLException {
    // get Hashtags in given tweet
    HashtagEntity[] htEntities = tweet.getHashtagEntities();

    for (HashtagEntity htEntity : htEntities) {
        // save hashtag
        saveHashtag(htEntity);

        // save relation
        saveTweetHashtagRelation(tweet, htEntity);
    }
}