use of twitter4j.MediaEntity in project Talon-for-Twitter by klinker24.
the class TweetLinkUtils method getLinksInStatus.
private static String[] getLinksInStatus(String tweetTexts, UserMentionEntity[] users, HashtagEntity[] hashtags, URLEntity[] urls, MediaEntity[] medias) {
String mUsers = "";
for (UserMentionEntity name : users) {
String n = name.getScreenName();
if (n.length() > 1) {
mUsers += n + " ";
}
}
String mHashtags = "";
for (HashtagEntity hashtagEntity : hashtags) {
String text = hashtagEntity.getText();
if (text.length() > 1) {
mHashtags += text + " ";
}
}
String expandedUrls = "";
String compressedUrls = "";
for (URLEntity entity : urls) {
String url = entity.getExpandedURL();
if (url.length() > 1) {
expandedUrls += url + " ";
compressedUrls += entity.getURL() + " ";
}
}
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 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") || str.contains("playlist"))) {
// first get the youtube surfaceView 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 surfaceView 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/") && !str.contains(".gifv")) {
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/") && !str.contains(".gifv")) {
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) {
e.printStackTrace();
tweetTexts = tweetTexts.replace(comp, sMediaDisplay[i].replace("http://", "").replace("https://", "").replace("www.", ""));
}
imageUrl = medias[0].getMediaURL();
for (MediaEntity m : medias) {
if (m.getType().equals("photo")) {
if (!imageUrl.contains(m.getMediaURL())) {
imageUrl += " " + m.getMediaURL();
}
}
}
otherUrl += sMediaDisplay[i];
}
}
return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers };
}
use of twitter4j.MediaEntity in project twicalico by moko256.
the class ShowImageActivity method contentDownload.
private void contentDownload() {
String path = "";
String ext = "";
MediaEntity mediaEntity = mediaEntities[pager.getCurrentItem()];
switch(mediaEntity.getType()) {
case "video":
for (MediaEntity.Variant variant : mediaEntity.getVideoVariants()) {
if (variant.getContentType().equals("video/mp4")) {
path = variant.getUrl();
ext = "mp4";
}
}
break;
case "animated_gif":
path = mediaEntity.getVideoVariants()[0].getUrl();
ext = "mp4";
break;
case "photo":
default:
String[] pathSplitWithDot = mediaEntity.getMediaURLHttps().split(".");
path = TwitterStringUtils.convertLargeImageUrl(mediaEntity.getMediaURLHttps());
ext = pathSplitWithDot[pathSplitWithDot.length - 1];
break;
}
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
String fileName = String.valueOf(mediaEntity.getId()) + "." + ext;
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/" + getString(R.string.app_name) + "/" + fileName);
request.setTitle(fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
manager.enqueue(request);
}
use of twitter4j.MediaEntity in project twicalico by moko256.
the class ShowImageActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
List<MediaEntity> mediaEntitiesList = (List<MediaEntity>) getIntent().getSerializableExtra(FRAG_MEDIA_ENTITIES);
mediaEntities = new MediaEntity[mediaEntitiesList.size()];
mediaEntitiesList.toArray(mediaEntities);
int position = getIntent().getIntExtra(FRAG_POSITION, 0);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
pager = findViewById(R.id.activity_show_image_view_pager);
pager.setAdapter(new ImagePagerAdapter(getSupportFragmentManager(), mediaEntities));
pager.setCurrentItem(position);
}
use of twitter4j.MediaEntity in project twicalico by moko256.
the class TwitterStringUtils method getStatusTextSequence.
public static CharSequence getStatusTextSequence(Status item) {
String postText = item.getText();
StringBuilder builder = new StringBuilder(postText);
URLEntity[] urlEntities = item.getURLEntities();
int tweetLength = postText.codePointCount(0, postText.length());
int sp = 0;
for (URLEntity entity : urlEntities) {
String url = entity.getURL();
String displayUrl = entity.getDisplayURL();
int urlLength = url.codePointCount(0, url.length());
int displayUrlLength = displayUrl.codePointCount(0, displayUrl.length());
if (entity.getStart() <= tweetLength && entity.getEnd() <= tweetLength) {
int dusp = displayUrlLength - urlLength;
builder.replace(postText.offsetByCodePoints(0, entity.getStart()) + sp, postText.offsetByCodePoints(0, entity.getEnd()) + sp, displayUrl);
sp += dusp;
}
}
MediaEntity[] mediaEntities = item.getMediaEntities();
if (mediaEntities.length > 0) {
MediaEntity mediaEntity = mediaEntities[0];
int result = builder.indexOf(mediaEntity.getURL(), builder.offsetByCodePoints(0, mediaEntity.getStart()));
if (result != -1) {
builder.replace(result, result + mediaEntity.getURL().length(), "");
}
}
return builder;
}
use of twitter4j.MediaEntity in project twicalico by moko256.
the class MTStatus method getMediaEntities.
@Override
public MediaEntity[] getMediaEntities() {
List<Attachment> medias = status.getMediaAttachments();
MediaEntity[] mediaEntities = new MediaEntity[medias.size()];
for (int i = 0; i < mediaEntities.length; i++) {
Attachment media = medias.get(i);
mediaEntities[i] = new MTMediaEntity(media);
}
return mediaEntities;
}
Aggregations