List of usage examples for twitter4j Status getHashtagEntities
HashtagEntity[] getHashtagEntities();
From source file:com.daiv.android.twitter.utils.TweetLinkUtils.java
License:Apache License
public static String[] getLinksInStatus(Status status) { UserMentionEntity[] users = status.getUserMentionEntities(); String mUsers = ""; for (UserMentionEntity name : users) { String n = name.getScreenName(); if (n.length() > 1) { mUsers += n + " "; }//w w w . j av a 2s. co m } HashtagEntity[] hashtags = status.getHashtagEntities(); String mHashtags = ""; for (HashtagEntity hashtagEntity : hashtags) { String text = hashtagEntity.getText(); if (text.length() > 1) { mHashtags += text + " "; } } URLEntity[] urls = status.getURLEntities(); String expandedUrls = ""; String compressedUrls = ""; for (URLEntity entity : urls) { String url = entity.getExpandedURL(); if (url.length() > 1) { expandedUrls += url + " "; compressedUrls += entity.getURL() + " "; } } MediaEntity[] medias = status.getMediaEntities(); String mediaExp = ""; String mediaComp = ""; String mediaDisplay = ""; for (MediaEntity e : medias) { String url = e.getURL(); if (url.length() > 1) { mediaComp += url + " "; mediaExp += e.getExpandedURL() + " "; mediaDisplay += e.getDisplayURL() + " "; } } String[] sExpandedUrls; String[] sCompressedUrls; String[] sMediaExp; String[] sMediaComp; String[] sMediaDisplay; try { sCompressedUrls = compressedUrls.split(" "); } catch (Exception e) { sCompressedUrls = new String[0]; } try { sExpandedUrls = expandedUrls.split(" "); } catch (Exception e) { sExpandedUrls = new String[0]; } try { sMediaComp = mediaComp.split(" "); } catch (Exception e) { sMediaComp = new String[0]; } try { sMediaExp = mediaExp.split(" "); } catch (Exception e) { sMediaExp = new String[0]; } try { sMediaDisplay = mediaDisplay.split(" "); } catch (Exception e) { sMediaDisplay = new String[0]; } String tweetTexts = status.getText(); String imageUrl = ""; String otherUrl = ""; for (int i = 0; i < sCompressedUrls.length; i++) { String comp = sCompressedUrls[i]; String exp = sExpandedUrls[i]; if (comp.length() > 1 && exp.length() > 1) { String str = exp.toLowerCase(); try { String replacement = exp.replace("http://", "").replace("https://", "").replace("www.", ""); boolean hasCom = replacement.contains(".com"); replacement = replacement.substring(0, 30) + "..."; if (hasCom && !replacement.contains(".com")) { // the link was too long... replacement = exp.replace("http://", "").replace("https://", "").replace("www.", ""); replacement = replacement.substring(0, replacement.indexOf(".com") + 6) + "..."; } tweetTexts = tweetTexts.replace(comp, replacement); } catch (Exception e) { tweetTexts = tweetTexts.replace(comp, exp.replace("http://", "").replace("https://", "").replace("www.", "")); } if (str.contains("instag") && !str.contains("blog.insta")) { imageUrl = exp + "media/?size=l"; otherUrl += exp + " "; } else if (exp.toLowerCase().contains("youtub") && !(str.contains("channel") || str.contains("user"))) { // first get the youtube video code int start = exp.indexOf("v=") + 2; int end = exp.length(); if (exp.substring(start).contains("&")) { end = exp.indexOf("&"); } else if (exp.substring(start).contains("?")) { end = exp.indexOf("?"); } try { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg"; } catch (Exception e) { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1) + "/hqdefault.jpg"; } otherUrl += exp + " "; } else if (str.contains("youtu.be")) { // first get the youtube video code int start = exp.indexOf(".be/") + 4; int end = exp.length(); if (exp.substring(start).contains("&")) { end = exp.indexOf("&"); } else if (exp.substring(start).contains("?")) { end = exp.indexOf("?"); } try { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg"; } catch (Exception e) { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1) + "/hqdefault.jpg"; } otherUrl += exp + " "; } else if (str.contains("twitpic")) { int start = exp.indexOf(".com/") + 5; imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", ""); otherUrl += exp + " "; } else if (str.contains("i.imgur") && !str.contains("/a/")) { int start = exp.indexOf(".com/") + 5; imageUrl = "http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "") + "l.jpg"; imageUrl = imageUrl.replace("gallery/", ""); otherUrl += exp + " "; } else if (str.contains("imgur") && !str.contains("/a/")) { int start = exp.indexOf(".com/") + 6; imageUrl = "http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "") + "l.jpg"; imageUrl = imageUrl.replace("gallery/", "").replace("a/", ""); otherUrl += exp + " "; } else if (str.contains("pbs.twimg.com")) { imageUrl = exp; otherUrl += exp + " "; } else if (str.contains("ow.ly/i/")) { imageUrl = "http://static.ow.ly/photos/original/" + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg"; otherUrl += exp + " "; } else if (str.contains("p.twipple.jp")) { imageUrl = "http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "") .replace("http://", "").replace("https://", "").replace("www.", ""); otherUrl += exp + " "; } else if (str.contains(".jpg") || str.contains(".png")) { imageUrl = exp; otherUrl += exp + " "; } else if (str.contains("img.ly")) { imageUrl = exp.replace("https", "http").replace("http://img.ly/", "http://img.ly/show/large/"); otherUrl += exp + " "; } else { otherUrl += exp + " "; } } } for (int i = 0; i < sMediaComp.length; i++) { String comp = sMediaComp[i]; String exp = sMediaExp[i]; if (comp.length() > 1 && exp.length() > 1) { try { String replacement = sMediaDisplay[i].replace("http://", "").replace("https://", "") .replace("www.", ""); boolean hasCom = replacement.contains(".com"); replacement = replacement.substring(0, 22) + "..."; if (hasCom && !replacement.contains(".com")) { // the link was too long... replacement = sMediaDisplay[i].replace("http://", "").replace("https://", "") .replace("www.", ""); replacement = replacement.substring(0, replacement.indexOf(".com") + 6) + "..."; } tweetTexts = tweetTexts.replace(comp, replacement); } catch (Exception e) { tweetTexts = tweetTexts.replace(comp, sMediaDisplay[i].replace("http://", "").replace("https://", "").replace("www.", "")); } imageUrl = status.getMediaEntities()[0].getMediaURL(); for (MediaEntity m : status.getExtendedMediaEntities()) { if (m.getType().equals("photo")) { if (!imageUrl.contains(m.getMediaURL())) { imageUrl += " " + m.getMediaURL(); } } } otherUrl += sMediaDisplay[i]; } } return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers }; }
From source file:com.datatorrent.contrib.twitter.TwitterSampleInput.java
License:Open Source License
@Override public void emitTuples() { for (int size = statuses.size(); size-- > 0;) { Status s = statuses.poll(); if (status.isConnected()) { status.emit(s);//from w w w .ja v a 2 s . com } if (text.isConnected()) { text.emit(s.getText()); } if (url.isConnected()) { URLEntity[] entities = s.getURLEntities(); if (entities != null) { for (URLEntity ue : entities) { url.emit((ue.getExpandedURL() == null ? ue.getURL() : ue.getExpandedURL()).toString()); } } } if (hashtag.isConnected()) { HashtagEntity[] hashtagEntities = s.getHashtagEntities(); if (hashtagEntities != null) { for (HashtagEntity he : hashtagEntities) { hashtag.emit(he.getText()); } } } } }
From source file:com.ebay.pulsar.twittersample.channel.TwitterSampleChannel.java
License:GNU General Public License
@Override public void open() throws EventException { super.open(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(false);//from ww w . j ava 2s .c o m twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onStallWarning(StallWarning warning) { } @Override public void onStatus(Status status) { HashtagEntity[] hashtagEntities = status.getHashtagEntities(); JetstreamEvent event = new JetstreamEvent(); event.setEventType("TwitterSample"); Place place = status.getPlace(); if (place != null) { event.put("country", place.getCountry()); } event.put("ct", status.getCreatedAt().getTime()); event.put("text", status.getText()); event.put("lang", status.getLang()); event.put("user", status.getUser().getName()); if (hashtagEntities != null && hashtagEntities.length > 0) { StringBuilder s = new StringBuilder(); s.append(hashtagEntities[0].getText()); for (int i = 1; i < hashtagEntities.length; i++) { s.append(","); s.append(hashtagEntities[i].getText()); } event.put("hashtag", s.toString()); } fireSendEvent(event); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } }; twitterStream.addListener(listener); twitterStream.sample(); }
From source file:com.github.daytron.twaattin.ui.tabledecorator.TweetColumnDecorator.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public Object generateCell(Table source, Object itemId, Object columnId) { fragments = new ArrayList<>(); Item item = source.getItem(itemId);//from ww w. j av a2s .co m @SuppressWarnings("rawtypes") BeanItem<Status> beanItem = (BeanItem) item; Status status = beanItem.getBean(); createFragmentsWithUrl(status.getURLEntities()); createFragmentsWithTag(status.getHashtagEntities()); createFragmentsWithMention(status.getUserMentionEntities()); Collections.sort(fragments); StringBuilder builder = new StringBuilder(status.getText()); int offset = 0; for (TweetFragment fragment : fragments) { builder.replace(fragment.getStart() + offset, fragment.getEnd() + offset, fragment.getReplacement()); offset += fragment.getOffset(); } return new Label(builder.toString(), HTML); }
From source file:com.github.jcustenborder.kafka.connect.twitter.StatusConverter.java
License:Apache License
public static void convert(Status status, Struct struct) { struct.put("CreatedAt", status.getCreatedAt()).put("Id", status.getId()).put("Text", status.getText()) .put("Source", status.getSource()).put("Truncated", status.isTruncated()) .put("InReplyToStatusId", status.getInReplyToStatusId()) .put("InReplyToUserId", status.getInReplyToUserId()) .put("InReplyToScreenName", status.getInReplyToScreenName()).put("Favorited", status.isFavorited()) .put("Retweeted", status.isRetweeted()).put("FavoriteCount", status.getFavoriteCount()) .put("Retweet", status.isRetweet()).put("RetweetCount", status.getRetweetCount()) .put("RetweetedByMe", status.isRetweetedByMe()) .put("CurrentUserRetweetId", status.getCurrentUserRetweetId()) .put("PossiblySensitive", status.isPossiblySensitive()).put("Lang", status.getLang()); Struct userStruct;/*ww w . j av a 2s. co m*/ if (null != status.getUser()) { userStruct = new Struct(USER_SCHEMA); convert(status.getUser(), userStruct); } else { userStruct = null; } struct.put("User", userStruct); Struct placeStruct; if (null != status.getPlace()) { placeStruct = new Struct(PLACE_SCHEMA); convert(status.getPlace(), placeStruct); } else { placeStruct = null; } struct.put("Place", placeStruct); Struct geoLocationStruct; if (null != status.getGeoLocation()) { geoLocationStruct = new Struct(GEO_LOCATION_SCHEMA); convert(status.getGeoLocation(), geoLocationStruct); } else { geoLocationStruct = null; } struct.put("GeoLocation", geoLocationStruct); List<Long> contributers = new ArrayList<>(); if (null != status.getContributors()) { for (Long l : status.getContributors()) { contributers.add(l); } } struct.put("Contributors", contributers); List<String> withheldInCountries = new ArrayList<>(); if (null != status.getWithheldInCountries()) { for (String s : status.getWithheldInCountries()) { withheldInCountries.add(s); } } struct.put("WithheldInCountries", withheldInCountries); struct.put("HashtagEntities", convert(status.getHashtagEntities())); struct.put("UserMentionEntities", convert(status.getUserMentionEntities())); struct.put("MediaEntities", convert(status.getMediaEntities())); struct.put("SymbolEntities", convert(status.getSymbolEntities())); struct.put("URLEntities", convert(status.getURLEntities())); }
From source file:com.klinker.android.twitter.utils.TweetLinkUtils.java
License:Apache License
public static String[] getLinksInStatus(Status status) { UserMentionEntity[] users = status.getUserMentionEntities(); String mUsers = ""; for (UserMentionEntity name : users) { String n = name.getScreenName(); if (n.length() > 1) { mUsers += n + " "; }/* w w w . j a v a 2 s .co m*/ } HashtagEntity[] hashtags = status.getHashtagEntities(); String mHashtags = ""; for (HashtagEntity hashtagEntity : hashtags) { String text = hashtagEntity.getText(); if (text.length() > 1) { mHashtags += text + " "; } } URLEntity[] urls = status.getURLEntities(); String expandedUrls = ""; String compressedUrls = ""; for (URLEntity entity : urls) { String url = entity.getExpandedURL(); if (url.length() > 1) { expandedUrls += url + " "; compressedUrls += entity.getURL() + " "; } } MediaEntity[] medias = status.getMediaEntities(); String mediaExp = ""; String mediaComp = ""; String mediaDisplay = ""; for (MediaEntity e : medias) { String url = e.getURL(); if (url.length() > 1) { mediaComp += url + " "; mediaExp += e.getExpandedURL() + " "; mediaDisplay += e.getDisplayURL() + " "; } } String[] sExpandedUrls; String[] sCompressedUrls; String[] sMediaExp; String[] sMediaComp; String[] sMediaDisplay; try { sCompressedUrls = compressedUrls.split(" "); } catch (Exception e) { sCompressedUrls = new String[0]; } try { sExpandedUrls = expandedUrls.split(" "); } catch (Exception e) { sExpandedUrls = new String[0]; } try { sMediaComp = mediaComp.split(" "); } catch (Exception e) { sMediaComp = new String[0]; } try { sMediaExp = mediaExp.split(" "); } catch (Exception e) { sMediaExp = new String[0]; } try { sMediaDisplay = mediaDisplay.split(" "); } catch (Exception e) { sMediaDisplay = new String[0]; } String tweetTexts = status.getText(); String imageUrl = ""; String otherUrl = ""; for (int i = 0; i < sCompressedUrls.length; i++) { String comp = sCompressedUrls[i]; String exp = sExpandedUrls[i]; if (comp.length() > 1 && exp.length() > 1) { String str = exp.toLowerCase(); try { tweetTexts = tweetTexts.replace(comp, exp.replace("http://", "").replace("https://", "").replace("www.", "").substring(0, 30) + "..."); } catch (Exception e) { tweetTexts = tweetTexts.replace(comp, exp.replace("http://", "").replace("https://", "").replace("www.", "")); } if (str.contains("instag") && !str.contains("blog.insta")) { imageUrl = exp + "media/?size=m"; otherUrl += exp + " "; } else if (exp.toLowerCase().contains("youtub") && !(str.contains("channel") || str.contains("user"))) { // first get the youtube video code int start = exp.indexOf("v=") + 2; int end = exp.length(); if (exp.substring(start).contains("&")) { end = exp.indexOf("&"); } else if (exp.substring(start).contains("?")) { end = exp.indexOf("?"); } try { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg"; } catch (Exception e) { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1) + "/hqdefault.jpg"; } otherUrl += exp + " "; } else if (str.contains("youtu.be")) { // first get the youtube video code int start = exp.indexOf(".be/") + 4; int end = exp.length(); if (exp.substring(start).contains("&")) { end = exp.indexOf("&"); } else if (exp.substring(start).contains("?")) { end = exp.indexOf("?"); } try { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg"; } catch (Exception e) { imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1) + "/mqefault.jpg"; } otherUrl += exp + " "; } else if (str.contains("twitpic")) { int start = exp.indexOf(".com/") + 5; imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", ""); otherUrl += exp + " "; } else if (str.contains("i.imgur") && !str.contains("/a/")) { int start = exp.indexOf(".com/") + 5; imageUrl = "http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "") + "m.jpg"; imageUrl = imageUrl.replace("gallery/", ""); otherUrl += exp + " "; } else if (str.contains("imgur") && !str.contains("/a/")) { int start = exp.indexOf(".com/") + 6; imageUrl = "http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "") + "m.jpg"; imageUrl = imageUrl.replace("gallery/", "").replace("a/", ""); otherUrl += exp + " "; } else if (str.contains("pbs.twimg.com")) { imageUrl = exp; otherUrl += exp + " "; } else if (str.contains("ow.ly/i")) { imageUrl = "http://static.ow.ly/photos/original/" + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg"; otherUrl += exp + " "; } else if (str.contains("p.twipple.jp")) { imageUrl = "http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "") .replace("http://", "").replace("https://", "").replace("www.", ""); otherUrl += exp + " "; } else if (str.contains(".jpg") || str.contains(".png")) { imageUrl = exp; otherUrl += exp + " "; } else { otherUrl += exp + " "; } } } for (int i = 0; i < sMediaComp.length; i++) { String comp = sMediaComp[i]; String exp = sMediaExp[i]; if (comp.length() > 1 && exp.length() > 1) { try { tweetTexts = tweetTexts.replace(comp, sMediaDisplay[i].replace("http://", "") .replace("https://", "").replace("www.", "").substring(0, 22) + "..."); } catch (Exception e) { tweetTexts = tweetTexts.replace(comp, sMediaDisplay[i].replace("http://", "").replace("https://", "").replace("www.", "")); } imageUrl = status.getMediaEntities()[0].getMediaURL(); for (MediaEntity m : status.getExtendedMediaEntities()) { if (m.getType().equals("photo")) { if (!imageUrl.contains(m.getMediaURL())) { imageUrl += " " + m.getMediaURL(); } } } otherUrl += sMediaDisplay[i]; } } return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers }; }
From source file:com.lambda.stack.HashTagMapper.java
@Override public Iterable<Tuple2<String, Integer>> call(Status t) throws Exception { List<Tuple2<String, Integer>> result = new LinkedList<>(); for (HashtagEntity he : t.getHashtagEntities()) { result.add(new Tuple2<>(he.getText(), Integer.valueOf(1))); }//ww w .j a va 2 s .c o m return result; }
From source file:com.mothsoft.alexis.engine.retrieval.TwitterRetrievalTaskImpl.java
License:Apache License
private List<TweetHashtag> readHashtags(Status status) { final List<TweetHashtag> hashtags = new ArrayList<TweetHashtag>(); if (status.getHashtagEntities() != null) { for (final HashtagEntity entity : status.getHashtagEntities()) { final TweetHashtag hashtag = new TweetHashtag((short) entity.getStart(), (short) entity.getEnd(), entity.getText());/*ww w. j av a 2s. c om*/ hashtags.add(hashtag); } } return hashtags; }
From source file:com.projectlaver.util.TwitterListingResponseHandler.java
License:Open Source License
public void processStatus(Status status) { if (this.logger.isDebugEnabled()) { this.logger.debug("+processStatus() with status id: " + status.getId()); }//from w w w . j a v a2s . co m String twitterId = this.longToString(status.getId()); User profile = status.getUser(); // Convert twitterIds to Strings here because these fields are treated as varchars by the database String providerUserId = this.longToString(profile.getId()); String inReplyToStatusId = this.longToString(status.getInReplyToStatusId()); String tweetText = status.getText(); Boolean isRetweet = (status.getRetweetedStatus() != null); // these two fields are required; without an @mention and a #hashtag we do not need to persist this tweet String mentionedProviderUserId = this.getMentionedProviderUserId(status.getUserMentionEntities()); String hashtag = this.getMentionedHashtag(status.getHashtagEntities()); if (StringUtils.isNoneBlank(mentionedProviderUserId, hashtag)) { ReplyMessageDTO dto = new ReplyMessageDTO(SocialProviders.TWITTER, providerUserId, twitterId, inReplyToStatusId, null, isRetweet, tweetText, hashtag, INITIAL_STATUS, status.getCreatedAt()); // Use a try catch here to trap excptions try { super.processUserMessage(mentionedProviderUserId, dto, false); // Retry on deadlock } catch (DeadlockLoserDataAccessException e) { this.logger.error("Lost deadlock trying to insert tweet. Falling back on retry logic."); this.retryProcessAfterDeadlock(mentionedProviderUserId, dto); // Log on uncaught exception } catch (Exception e) { this.logger.error(String.format( "Valid message with id: %s read by stream, but processing failed with an exception.", twitterId), e); this.logger.error("UNINSERTED TWEET: " + ToStringBuilder.reflectionToString(dto)); } } if (this.logger.isDebugEnabled()) { this.logger.debug("-processStatus() with status id: " + status.getId()); } }
From source file:com.raythos.sentilexo.twitter.utils.StatusArraysHelper.java
public static List<String> getHashTagsList(Status status) { @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<String> result = new ArrayList<>(); for (HashtagEntity h : status.getHashtagEntities()) { result.add(h.getText().toLowerCase()); }/* w w w.j a v a 2 s. co m*/ return result; }