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:org.gabrielebaldassarre.twitter.tweet.TalendFlowTweetBehaviour.java

License:Open Source License

/**
 * Visit a target {@link TalendFlow} for parsed or raw json output.
 * /*from www.j a  v a 2 s  .com*/
 * @param target the data flow to fill
 */
public void visit(TalendFlow target) {
    ResourceBundle rb = ResourceBundle.getBundle("tTwitterInput", Locale.getDefault());

    TalendRowFactory rowFactory = target.getModel().getRowFactory();
    valid = false;

    @SuppressWarnings("unchecked")
    List<Status> tweets = (List<Status>) resultSet.getValue();
    for (int i = 0; i < tweets.size(); i++) {
        Status tweet = tweets.get(i);
        TalendRow current = rowFactory.newRow(target);

        Iterator<Entry<TalendColumn, TweetField>> col = associations.entrySet().iterator();
        while (col.hasNext()) {
            List<String> h;
            List<Long> l;

            Map.Entry<TalendColumn, TweetField> row = (Map.Entry<TalendColumn, TweetField>) col.next();

            if (target != null && !row.getKey().getFlow().equals(target)) {
                throw new IllegalArgumentException(String.format(rb.getString("exception.columnNotInFlow"),
                        row.getKey().getName(), target.getName()));
            }

            switch (row.getValue()) {
            case CREATION_DATE:
                String literalDate = (new StringBuilder(
                        TalendFlowTweetBehaviour.DATEFORMAT.format(tweet.getCreatedAt()))).toString();

                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(literalDate));
                case LONG:
                    current.setValue(row.getKey(), Long.parseLong(literalDate));
                case DOUBLE:
                    current.setValue(row.getKey(), Double.parseDouble(literalDate));
                case FLOAT:
                    current.setValue(row.getKey(), Float.parseFloat(literalDate));
                case INTEGER:
                    current.setValue(row.getKey(), Integer.parseInt(literalDate));
                case DATE:
                    current.setValue(row.getKey(), tweet.getCreatedAt());
                    break;
                case STRING:
                    current.setValue(row.getKey(), literalDate);
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case FROM_NAME:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), tweet.getUser().getName());
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case FROM_USERID:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.getUser().getId()));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), new Double(tweet.getUser().getId()));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), new Float(tweet.getUser().getId()));
                    break;
                case LONG:
                    current.setValue(row.getKey(), new Long(tweet.getUser().getId()));
                    break;
                case STRING:
                    current.setValue(row.getKey(), String.valueOf((tweet.getUser().getId())));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case FROM_SCREEN_NAME:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), tweet.getUser().getScreenName());
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case HASHTAGS:
                List<HashtagEntity> hashtags = Arrays.asList(tweet.getHashtagEntities());
                h = new ArrayList<String>(hashtags.size());

                for (HashtagEntity hashtag : hashtags) {
                    h.add((includeHash() ? "#" : "") + hashtag.getText());
                }
                switch (row.getKey().getType()) {
                case STRING:
                case LIST:
                    current.setValue(row.getKey(), !TalendType.STRING.equals(row.getKey().getType()) ? h
                            : Joiner.on(getEntitiesSeparator()).join(h));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case IS_FAVORITED:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.isFavorited() ? 1 : 0));
                    break;
                case BOOLEAN:
                    current.setValue(row.getKey(), tweet.isFavorited());
                    break;
                case BYTE:
                    current.setValue(row.getKey(), (byte) (tweet.isFavorited() ? 1 : 0));
                    break;
                case CHARACTER:
                    current.setValue(row.getKey(), (tweet.isFavorited() ? '1' : '0'));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), (double) (tweet.isFavorited() ? 1d : 0d));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), (float) (tweet.isFavorited() ? 1f : 0f));
                    break;
                case INTEGER:
                    current.setValue(row.getKey(), (tweet.isFavorited() ? 1 : 0));
                    break;
                case LONG:
                    current.setValue(row.getKey(), (long) (tweet.isFavorited() ? 1l : 0l));
                    break;
                case SHORT:
                    current.setValue(row.getKey(), (short) (tweet.isFavorited() ? (short) 1 : (short) 0));
                    break;
                case STRING:
                    current.setValue(row.getKey(), (tweet.isFavorited() ? "1" : "0"));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));

                }
                break;
            case IS_POSSIBLY_SENSITIVE:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.isPossiblySensitive() ? 1 : 0));
                    break;
                case BOOLEAN:
                    current.setValue(row.getKey(), tweet.isPossiblySensitive());
                    break;
                case BYTE:
                    current.setValue(row.getKey(), (byte) (tweet.isPossiblySensitive() ? 1 : 0));
                    break;
                case CHARACTER:
                    current.setValue(row.getKey(), (tweet.isPossiblySensitive() ? '1' : '0'));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), (double) (tweet.isPossiblySensitive() ? 1d : 0d));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), (float) (tweet.isPossiblySensitive() ? 1f : 0f));
                    break;
                case INTEGER:
                    current.setValue(row.getKey(), (tweet.isPossiblySensitive() ? 1 : 0));
                    break;
                case LONG:
                    current.setValue(row.getKey(), (long) (tweet.isPossiblySensitive() ? 1l : 0l));
                    break;
                case SHORT:
                    current.setValue(row.getKey(),
                            (short) (tweet.isPossiblySensitive() ? (short) 1 : (short) 0));
                    break;
                case STRING:
                    current.setValue(row.getKey(), (tweet.isPossiblySensitive() ? "1" : "0"));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case IS_RETWEET:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.isRetweet() ? 1 : 0));
                    break;
                case BOOLEAN:
                    current.setValue(row.getKey(), tweet.isRetweet());
                    break;
                case BYTE:
                    current.setValue(row.getKey(), (byte) (tweet.isRetweet() ? 1 : 0));
                    break;
                case CHARACTER:
                    current.setValue(row.getKey(), (tweet.isRetweet() ? '1' : '0'));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), (double) (tweet.isRetweet() ? 1d : 0d));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), (float) (tweet.isRetweet() ? 1f : 0f));
                    break;
                case INTEGER:
                    current.setValue(row.getKey(), (tweet.isRetweet() ? 1 : 0));
                    break;
                case LONG:
                    current.setValue(row.getKey(), (long) (tweet.isRetweet() ? 1l : 0l));
                    break;
                case SHORT:
                    current.setValue(row.getKey(), (short) (tweet.isRetweet() ? (short) 1 : (short) 0));
                    break;
                case STRING:
                    current.setValue(row.getKey(), (tweet.isRetweet() ? "1" : "0"));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                current.setValue(row.getKey(), tweet.isRetweet());
                break;
            case LOCATION:
                GeoLocation g = tweet.getGeoLocation();
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(),
                            g != null
                                    ? String.valueOf(g.getLatitude()) + getEntitiesSeparator()
                                            + String.valueOf(g.getLongitude())
                                    : null);
                    break;
                case OBJECT:
                    current.setValue(row.getKey(), g);
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case REPLYTO_SCREEN_NAME:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), tweet.getInReplyToScreenName());
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case REPLYTO_STATUSID:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.getInReplyToStatusId()));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), new Double(tweet.getInReplyToStatusId()));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), new Float(tweet.getInReplyToStatusId()));
                    break;
                case LONG:
                    current.setValue(row.getKey(), new Long(tweet.getInReplyToStatusId()));
                    break;
                case STRING:
                    current.setValue(row.getKey(), String.valueOf((tweet.getInReplyToStatusId())));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case REPLYTO_USERID:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.getInReplyToUserId()));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), new Double(tweet.getInReplyToUserId()));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), new Float(tweet.getInReplyToUserId()));
                    break;
                case LONG:
                    current.setValue(row.getKey(), new Long(tweet.getInReplyToUserId()));
                    break;
                case STRING:
                    current.setValue(row.getKey(), String.valueOf((tweet.getInReplyToUserId())));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case RETWEET_COUNT:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.getRetweetCount()));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), new Double(tweet.getRetweetCount()));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), new Float(tweet.getRetweetCount()));
                    break;
                case LONG:
                    current.setValue(row.getKey(), new Long(tweet.getRetweetCount()));
                    break;
                case STRING:
                    current.setValue(row.getKey(), String.valueOf((tweet.getRetweetCount())));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case SOURCE:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), tweet.getSource());
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case STATUS_ID:
                switch (row.getKey().getType()) {
                case BIGDECIMAL:
                    current.setValue(row.getKey(), new BigDecimal(tweet.getId()));
                    break;
                case DOUBLE:
                    current.setValue(row.getKey(), new Double(tweet.getId()));
                    break;
                case FLOAT:
                    current.setValue(row.getKey(), new Float(tweet.getId()));
                    break;
                case LONG:
                    current.setValue(row.getKey(), new Long(tweet.getId()));
                    break;
                case STRING:
                    current.setValue(row.getKey(), String.valueOf((tweet.getId())));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case TEXT:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), tweet.getText());
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case URL_ENTITIES:
            case URL_ENTITIES_STRING:
                List<URLEntity> urlEntities = Arrays.asList(tweet.getURLEntities());
                h = new ArrayList<String>(urlEntities.size());

                for (URLEntity urlEntity : urlEntities) {
                    h.add(urlEntity.getExpandedURL());
                }
                switch (row.getKey().getType()) {
                case STRING:
                case LIST:
                    current.setValue(row.getKey(), !TalendType.STRING.equals(row.getKey().getType()) ? h
                            : Joiner.on(getEntitiesSeparator()).join(h));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case USER_MENTIONS:
                List<UserMentionEntity> userMentionsEntities = Arrays.asList(tweet.getUserMentionEntities());
                l = new ArrayList<Long>(userMentionsEntities.size());

                for (UserMentionEntity userMention : userMentionsEntities) {
                    l.add(userMention.getId());
                }
                switch (row.getKey().getType()) {
                case STRING:
                case LIST:
                    current.setValue(row.getKey(), !TalendType.STRING.equals(row.getKey().getType()) ? l
                            : Joiner.on(getEntitiesSeparator()).join(l));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case USER_MENTIONS_SCREEN_NAME:
                List<UserMentionEntity> userMentionsScreen = Arrays.asList(tweet.getUserMentionEntities());
                h = new ArrayList<String>(userMentionsScreen.size());

                for (UserMentionEntity userMention : userMentionsScreen) {
                    h.add((includeHash() ? "@" : "") + userMention.getScreenName());
                }
                switch (row.getKey().getType()) {
                case STRING:
                case LIST:
                    current.setValue(row.getKey(), !TalendType.STRING.equals(row.getKey().getType()) ? h
                            : Joiner.on(getEntitiesSeparator()).join(h));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            case JSON:
                switch (row.getKey().getType()) {
                case STRING:
                    current.setValue(row.getKey(), DataObjectFactory.getRawJSON(tweet));
                    break;
                default:
                    throw new IllegalArgumentException(String.format(rb.getString("exception.uncastableColumn"),
                            row.getKey().getType().getTypeString(), row.getKey().getName()));
                }
                break;
            default:
                throw new IllegalArgumentException(
                        String.format(rb.getString("exception.unparseableColumn"), row.getKey().getName()));

            }

        }

    }
    valid = true;
}

From source file:org.nsoft.openbus.model.Mensagem.java

License:Open Source License

public static Mensagem creteFromTwitterStatus(Status s) {
    try {/*from   w  w  w  .ja v a 2s  .com*/
        Mensagem mensagem = new Mensagem();

        String text = s.getText();

        for (HashtagEntity h : s.getHashtagEntities()) {

            String subText = "<a href=\"twitter_search://do_search?search=" + h.getText() + "\">#" + h.getText()
                    + "</a>";
            text = text.replace("#" + h.getText(), subText);

        }

        for (UserMentionEntity u : s.getUserMentionEntities()) {

            String subText = "<a href=\"twitter_search_user://find_user?username=" + u.getScreenName() + "\">@"
                    + u.getScreenName() + "</a>";
            text = text.replace("@" + u.getScreenName(), subText);

        }

        mensagem.setAction(OpenTwitterStatusAction.getInstance());
        mensagem.addtions = createAddtions(s);
        mensagem.addtions.put("htmlText", text);
        mensagem.idMensagem = Long.toString(s.getId());
        mensagem.nome_usuario = s.getUser().getName();
        mensagem.mensagem = s.getText();
        mensagem.imagePath = new URL(s.getUser().getOriginalProfileImageURL());
        mensagem.data = s.getCreatedAt();
        mensagem.idUser = s.getUser().getId();
        mensagem.tipo = TIPO_STATUS;

        return mensagem;
    } catch (JSONException e) {
        return null;
    } catch (MalformedURLException e) {
        return null;
    }
}

From source file:org.richfaces.examples.tweetstream.dataserver.listeners.TweetStreamListener.java

License:Open Source License

private Tweet createTweet(Status status) {
    //create a new tweet from the status update
    Tweet tweet = new Tweet();
    tweet.setText(status.getText());//from  ww w. jav a2 s.  c  o  m
    tweet.setId(status.getUser().getId());
    tweet.setProfileImageUrl(status.getUser().getProfileImageURL().toString());
    tweet.setScreenName(status.getUser().getScreenName());
    if (status.getRetweetedStatus() != null) {
        tweet.setRetweet(status.getRetweetedStatus().isRetweet());
    }

    //process hashtags in the tweet
    HashtagEntity[] hashtagEntities = status.getHashtagEntities();
    if (hashtagEntities != null) {
        ArrayList<String> curTags = new ArrayList<String>(hashtagEntities.length);

        for (HashtagEntity tw4jTag : hashtagEntities) {
            curTags.add(tw4jTag.getText());
        }

        tweet.setHashTags(curTags.toArray(new String[0]));
    } else {
        tweet.setHashTags(new String[0]);
    }
    return tweet;
}

From source file:org.smarttechie.servlet.SimpleStream.java

public void getStream(TwitterStream twitterStream, String[] parametros, final Session session)//,PrintStream out)
{

    listener = new StatusListener() {

        @Override/*from  w ww .  j a  v a 2  s  .co m*/
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatus(Status status) {
            Twitter twitter = new TwitterFactory().getInstance();
            User user = status.getUser();

            // gets Username
            String username = status.getUser().getScreenName();
            System.out.println("");
            String profileLocation = user.getLocation();
            System.out.println(profileLocation);
            long tweetId = status.getId();
            System.out.println(tweetId);
            String content = status.getText();
            System.out.println(content + "\n");

            JSONObject obj = new JSONObject();
            obj.put("User", status.getUser().getScreenName());
            obj.put("ProfileLocation", user.getLocation().replaceAll("'", "''"));
            obj.put("Id", status.getId());
            obj.put("UserId", status.getUser().getId());
            //obj.put("User", status.getUser());
            obj.put("Message", status.getText().replaceAll("'", "''"));
            obj.put("CreatedAt", status.getCreatedAt().toString());
            obj.put("CurrentUserRetweetId", status.getCurrentUserRetweetId());
            //Get user retweeteed
            String otheruser;
            try {
                if (status.getCurrentUserRetweetId() != -1) {
                    User user2 = twitter.showUser(status.getCurrentUserRetweetId());
                    otheruser = user2.getScreenName();
                    System.out.println("Other user: " + otheruser);
                }
            } catch (Exception ex) {
                System.out.println("ERROR: " + ex.getMessage().toString());
            }
            obj.put("IsRetweet", status.isRetweet());
            obj.put("IsRetweeted", status.isRetweeted());
            obj.put("IsFavorited", status.isFavorited());

            obj.put("InReplyToUserId", status.getInReplyToUserId());
            //In reply to
            obj.put("InReplyToScreenName", status.getInReplyToScreenName());

            obj.put("RetweetCount", status.getRetweetCount());
            if (status.getGeoLocation() != null) {
                obj.put("GeoLocationLatitude", status.getGeoLocation().getLatitude());
                obj.put("GeoLocationLongitude", status.getGeoLocation().getLongitude());
            }

            JSONArray listHashtags = new JSONArray();
            String hashtags = "";
            for (HashtagEntity entity : status.getHashtagEntities()) {
                listHashtags.add(entity.getText());
                hashtags += entity.getText() + ",";
            }

            if (!hashtags.isEmpty())
                obj.put("HashtagEntities", hashtags.substring(0, hashtags.length() - 1));

            if (status.getPlace() != null) {
                obj.put("PlaceCountry", status.getPlace().getCountry());
                obj.put("PlaceFullName", status.getPlace().getFullName());
            }

            obj.put("Source", status.getSource());
            obj.put("IsPossiblySensitive", status.isPossiblySensitive());
            obj.put("IsTruncated", status.isTruncated());

            if (status.getScopes() != null) {
                JSONArray listScopes = new JSONArray();
                String scopes = "";
                for (String scope : status.getScopes().getPlaceIds()) {
                    listScopes.add(scope);
                    scopes += scope + ",";
                }

                if (!scopes.isEmpty())
                    obj.put("Scopes", scopes.substring(0, scopes.length() - 1));
            }

            obj.put("QuotedStatusId", status.getQuotedStatusId());

            JSONArray list = new JSONArray();
            String contributors = "";
            for (long id : status.getContributors()) {
                list.add(id);
                contributors += id + ",";
            }

            if (!contributors.isEmpty())
                obj.put("Contributors", contributors.substring(0, contributors.length() - 1));

            System.out.println("" + obj.toJSONString());

            insertNodeNeo4j(obj);

            //out.println(obj.toJSONString());
            String statement = "INSERT INTO TweetsClassification JSON '" + obj.toJSONString() + "';";
            executeQuery(session, statement);
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    };
    FilterQuery fq = new FilterQuery();

    fq.track(parametros);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Creates a content for the given tweet, adds it to the data set and sets
 * the author./*from  w  w  w.j a v a2s.co  m*/
 * 
 * @param author
 *            Person corresponding to the twitter user which authored the
 *            tweet
 * @param tweet
 *            The tweet
 * @return The Content created from the tweet, null in error case.
 */
private Content createContentFromTweet(Person author, Status tweet) {
    if (tweet == null) {
        return null;
    }

    String tweetText = tweet.getText();
    if (tweetText == null || tweetText.isEmpty()) {
        return null;
    }
    String ident = tweet.getId() + "";

    if (this.getContentWithSourceIdent(ident) != null) {
        // status already created
        return null;
    }

    Content tweetContent = factory.createContent();
    tweetContent.setStringValue(tweetText);
    tweetContent.setName(createTitleFromTwitterText(tweetText));

    tweetContent = (Content) this.add(tweetContent, ident);

    if (tweetContent == null) {
        return null;
    }

    tweetContent.metaTag(TwitterTags.TWITTER);
    tweetContent.setCreated(tweet.getCreatedAt());

    if (author != null) {
        tweetContent.setAuthor(author);
    }

    // and tag the status
    HashtagEntity[] hashtags = tweet.getHashtagEntities();

    tagIOwithHashtags(tweetContent, hashtags);

    UserMentionEntity[] mentionedUsers = tweet.getUserMentionEntities();
    if (mentionedUsers != null && mentionedUsers.length > 0
            && source.isPropertyTrue(TwitterProperties.ADD_MENTIONED_PEOPLE_PROPERTY)) {
        for (int i = 0; i < mentionedUsers.length; i++) {
            Person mentionedPerson = getPersonForTwitterUserId(mentionedUsers[i].getId());

            if (mentionedPerson == null) {
                continue;
            }

            tweetContent.addContributor(mentionedPerson);
        }
    }

    URLEntity[] urlEntities = tweet.getURLEntities();
    if (urlEntities != null && urlEntities.length > 0
            && source.isPropertyTrue(TwitterProperties.ADD_URL_ENTITIES_PROPERTY)) {
        for (int i = 0; i < urlEntities.length; i++) {
            String url = urlEntities[i].getURL();
            if (url != null) {
                // attach url as website
                tweetContent.addWebSite(url);
            }
        }
    }

    // no more available
    //      String language = tweet.getIsoLanguageCode();
    //      if(language != null && !language.isEmpty())
    //      {
    //         // set in content
    //         tweetContent.setLocale(language);
    //         // set as meta tag
    //         tweetContent.metaTag(language);
    //      }

    // TODO check media entities
    // MediaEntity[] mediaEntities = twitterStatus.getMediaEntities();

    // add location
    GeoLocation tweetLocation = tweet.getGeoLocation();
    Place place = tweet.getPlace();

    if (tweetLocation != null || place != null) {
        Location location = factory.createLocation();
        if (place != null) {
            location.setStreet(place.getStreetAddress());
            location.setCountry(place.getCountry());
            location.setStringValue(place.getFullName());
        }
        if (tweetLocation != null) {
            location.setLatitude(tweetLocation.getLatitude() + "");
            location.setLongitude(tweetLocation.getLongitude() + "");
        }
        location = (Location) this.add(location, "tloc_" + tweet.getId());

        if (location != null) {
            location.metaTag(TwitterTags.TWITTER);
            tweetContent.extend(location);
            if (place != null) {
                location.metaTag(place.getCountryCode());
                location.metaTag(place.getPlaceType());
            }
        }
    }

    return tweetContent;
}

From source file:org.talend.spark.utils.twitter.TwitterUtil.java

License:Open Source License

public static Object parse(TwitterParameter parameter, Status status) {
    if (parameter == TwitterParameter.USERNAME) {
        return status.getUser().getName();
    } else if (parameter == TwitterParameter.TEXT) {
        return status.getText();
    } else if (parameter == TwitterParameter.SOURCE) {
        return status.getSource();
    } else if (parameter == TwitterParameter.ACCESSLEVEL) {
        return status.getAccessLevel();
    } else if (parameter == TwitterParameter.DATE) {
        return status.getCreatedAt();
    } else if (parameter == TwitterParameter.ID) {
        return status.getId();
    } else if (parameter == TwitterParameter.GEOLOCATION_LATITUDE) {
        return status.getGeoLocation().getLatitude();
    } else if (parameter == TwitterParameter.GEOLOCATION_LONGITUDE) {
        return status.getGeoLocation().getLongitude();
    } else if (parameter == TwitterParameter.HASHTAG) {
        String hashTags = "";
        HashtagEntity[] hashTagArray = status.getHashtagEntities();
        for (int i = 0; i < hashTagArray.length; i++) {
            if (hashTags.equals("")) {
                hashTags = hashTagArray[i].getText();
            } else {
                hashTags = hashTags + "," + hashTagArray[i].getText();
            }//from  ww w. ja  va 2s  .c  o  m
        }
        return hashTags;
    }
    return null;
}

From source file:org.vsepml.storm.twitter.StormTwitterHashtagSplitter.java

License:Apache License

@Override
public void execute(Tuple tuple) {
    Status tweet = (Status) tuple.getValueByField("tweet");
    HashtagEntity[] heTab = tweet.getHashtagEntities();
    for (int i = 0; i < heTab.length; i++)
        collector.emit(new Values(tweet.getGeoLocation(), heTab[i].getText()));
}

From source file:org.wandora.application.tools.extractors.twitter.AbstractTwitterExtractor.java

License:Open Source License

public Topic reifyTweet(Status t, TopicMap tm) {
    Topic tweetTopic = null;//from w  ww.  j  a  v a2 s  .  c om
    try {
        long tId = t.getId();
        String msg = t.getText();
        User user = t.getUser();

        if (user == null) {
            tweetTopic = reifyTweet(tId, null, msg, tm);
        }

        else {
            String userScreenName = user.getScreenName();

            tweetTopic = reifyTweet(tId, userScreenName, msg, tm);

            Topic userTopic = reifyTwitterUser(user, tm);

            if (tweetTopic != null && userTopic != null) {
                Association a = tm.createAssociation(getTwitterFromUserType(tm));
                a.addPlayer(tweetTopic, getTweetType(tm));
                a.addPlayer(userTopic, getTwitterUserType(tm));
            }
        }

        /*
        String toUser = t.getToUser();
        if(toUser != null) {
        long toUid = t.getToUserId();       
        Topic toUserTopic = reifyTwitterUser(toUser, toUid, tm);
        if(tweetTopic != null && toUserTopic != null) {
            Association a = tm.createAssociation(getTwitterToUserType(tm));
            a.addPlayer(tweetTopic, getTweetType(tm));
            a.addPlayer(toUserTopic, getTwitterUserType(tm));
        }
        }
        */

        Date d = t.getCreatedAt();
        if (tweetTopic != null && d != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = df.format(d);
            tweetTopic.setData(getTweetDateType(tm), TMBox.getLangTopic(tweetTopic, DEFAULT_LANG), dateStr);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            dateStr = sdf.format(d);
            Topic dateTopic = ExtractHelper.getOrCreateTopic(DATE_SI_BODY + dateStr, dateStr,
                    getTweetDateType(tm), tm);
            if (dateTopic != null) {
                Association a = tm.createAssociation(getTweetDateType(tm));
                a.addPlayer(tweetTopic, getTweetType(tm));
                a.addPlayer(dateTopic, getTweetDateType(tm));
            }
        }

        /*
        String l = t.getIsoLanguageCode();
        if(l != null) {
        Topic tweetLangTopic = TMBox.getLangTopic(tweetTopic, l);
        if(tweetLangTopic != null) {
            Association a = tm.createAssociation(getTweetLangType(tm));
            a.addPlayer(tweetTopic, getTweetType(tm));
            a.addPlayer(tweetLangTopic, getTweetLangType(tm));
        }
        }
        */

        GeoLocation geo = t.getGeoLocation();
        if (geo != null) {
            double lat = geo.getLatitude();
            double lon = geo.getLongitude();
            String geoLocStr = lat + "," + lon;
            tweetTopic.setData(getTweetGeoLocationType(tm), TMBox.getLangTopic(tweetTopic, DEFAULT_LANG),
                    geoLocStr);
        }

        HashtagEntity[] entities = t.getHashtagEntities();
        if (entities != null && entities.length > 0) {
            for (int i = 0; i < entities.length; i++) {
                Topic entityTopic = reifyHashtagEntity(entities[i], tm);
                if (entityTopic != null) {
                    Association a = tm.createAssociation(getHashtagType(tm));
                    a.addPlayer(tweetTopic, getTweetType(tm));
                    a.addPlayer(entityTopic, getHashtagType(tm));
                }
            }
        }

        MediaEntity[] mediaEntities = t.getMediaEntities();
        if (mediaEntities != null && mediaEntities.length > 0) {
            for (int i = 0; i < mediaEntities.length; i++) {
                Topic entityTopic = reifyMediaEntity(mediaEntities[i], tm);
                if (entityTopic != null) {
                    Association a = tm.createAssociation(getMediaEntityType(tm));
                    a.addPlayer(tweetTopic, getTweetType(tm));
                    a.addPlayer(entityTopic, getMediaEntityType(tm));
                }
            }
        }

        URLEntity[] urlEntities = t.getURLEntities();
        if (urlEntities != null && urlEntities.length > 0) {
            for (int i = 0; i < urlEntities.length; i++) {
                Topic entityTopic = reifyUrlEntity(urlEntities[i], tm);
                if (entityTopic != null) {
                    Association a = tm.createAssociation(getURLEntityType(tm));
                    a.addPlayer(tweetTopic, getTweetType(tm));
                    a.addPlayer(entityTopic, getURLEntityType(tm));
                }
            }
        }
    } catch (Exception e) {
        log(e);
    }
    return tweetTopic;
}

From source file:org.xmlsh.twitter.util.TwitterWriter.java

License:BSD License

public void write(Status t) throws XMLStreamException {
    startElement("tweet");
    attribute("id", t.getId());

    // write("annotations",t.getAnnotations());
    write("created-at", t.getCreatedAt());
    write("from-user", sanitizeID(t.getUser().getId()), sanitizeUser(t.getUser().getName()));
    write("geo-location", t.getGeoLocation());
    write("hash-tags", t.getHashtagEntities());
    write("iso-language-code", t.getUser().getLang());
    write("location", t.getUser().getLocation());
    write("media", t.getMediaEntities());
    write("place", t.getPlace());
    write("profile-image-url", sanitizeUser(t.getUser().getProfileImageURL()));
    write("source", t.getSource());
    write("text", t.getText());
    write("to-user", sanitizeID(t.getInReplyToUserId()), sanitizeUser(t.getInReplyToScreenName()));
    write("url-entities", t.getURLEntities());
    write("user-mention-entities", t.getUserMentionEntities());

    endElement();/*from   www  .j  a v a  2s .  c  o  m*/

}

From source file:org.xmlsh.twitter.util.TwitterWriter.java

License:BSD License

public void write(String localName, Status status) throws XMLStreamException {
    if (status != null) {
        startElement(localName);/* ww  w  .j  av a 2  s .  c o m*/
        attribute("id", status.getId());

        // write("annotations",t.getAnnotations());
        write("created-at", status.getCreatedAt());

        write("user", status.getUser());
        write("geo-location", status.getGeoLocation());
        write("hash-tags", status.getHashtagEntities());

        write("media", status.getMediaEntities());
        write("place", status.getPlace());

        write("source", status.getSource());
        write("text", status.getText());

        write("url-entities", status.getURLEntities());
        write("user-mention-entities", status.getUserMentionEntities());

        endElement();
    }
}