Support REdgifs and Gfycat videos in post lists and links.

This commit is contained in:
Alex Ning 2020-06-04 16:43:46 +08:00
parent d9de2938b4
commit ca957e8904
9 changed files with 222 additions and 104 deletions

View File

@ -6,5 +6,5 @@ import retrofit2.http.Path;
public interface GfycatAPI { public interface GfycatAPI {
@GET("{gfyid}") @GET("{gfyid}")
Call<String> getSubredditData(@Path("gfyid") String gfyId); Call<String> getGfycatData(@Path("gfyid") String gfyId);
} }

View File

@ -39,6 +39,7 @@ public class LinkResolverActivity extends AppCompatActivity {
private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?"; private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?";
private static final String REDD_IT_POST_PATTERN = "/\\w+/?"; private static final String REDD_IT_POST_PATTERN = "/\\w+/?";
private static final String GFYCAT_PATTERN = "/[\\w-]+$"; private static final String GFYCAT_PATTERN = "/[\\w-]+$";
private static final String REDGIFS_PATTERN = "/watch/[\\w-]+$";
private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?"; private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?";
private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?"; private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?";
private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?"; private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?";
@ -76,119 +77,151 @@ public class LinkResolverActivity extends AppCompatActivity {
path = path.substring(0, path.length() - 1); path = path.substring(0, path.length() - 1);
} }
String messageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); if (path.endsWith("jpg") || path.endsWith("png")) {
String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); Intent intent = new Intent(this, ViewImageActivity.class);
String url = uri.toString();
String fileName = url.substring(url.lastIndexOf('/') + 1);
intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, url);
intent.putExtra(ViewImageActivity.FILE_NAME_KEY, fileName);
intent.putExtra(ViewImageActivity.POST_TITLE_KEY, fileName);
startActivity(intent);
} else if (path.endsWith("gif")) {
Intent intent = new Intent(this, ViewGIFActivity.class);
String url = uri.toString();
String fileName = url.substring(url.lastIndexOf('/') + 1);
intent.putExtra(ViewGIFActivity.GIF_URL_KEY, url);
intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, fileName);
intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, fileName);
startActivity(intent);
} else if (path.endsWith("mp4")) {
Intent intent = new Intent(this, ViewVideoActivity.class);
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_DIRECT);
intent.setData(uri);
startActivity(intent);
} else {
String messageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
String authority = uri.getAuthority(); String authority = uri.getAuthority();
List<String> segments = uri.getPathSegments(); List<String> segments = uri.getPathSegments();
if (authority != null) { if (authority != null) {
if (authority.contains("reddit.com") || authority.contains("redd.it") || authority.contains("reddit.app")) { if (authority.contains("reddit.com") || authority.contains("redd.it") || authority.contains("reddit.app")) {
if (authority.equals("reddit.app.link") && path.isEmpty()) { if (authority.equals("reddit.app.link") && path.isEmpty()) {
String redirect = uri.getQueryParameter("$og_redirect"); String redirect = uri.getQueryParameter("$og_redirect");
handleUri(Uri.parse(redirect)); handleUri(Uri.parse(redirect));
} else if (path.isEmpty()) { } else if (path.isEmpty()) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else if (path.matches(POST_PATTERN)) {
int commentsIndex = segments.lastIndexOf("comments");
if (commentsIndex >= 0 && commentsIndex < segments.size() - 1) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (path.matches(COMMENT_PATTERN)) {
int commentsIndex = segments.lastIndexOf("comments");
if (commentsIndex >= 0 && commentsIndex < segments.size() - 1) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (path.matches(SUBREDDIT_PATTERN)) {
String subredditName = path.substring(3);
if (subredditName.equals("popular") || subredditName.equals("all")) {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName);
intent.putExtra(MainActivity.EXTRA_MESSSAGE_FULLNAME, messageFullname);
intent.putExtra(MainActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent); startActivity(intent);
} else { } else if (path.matches(POST_PATTERN)) {
int commentsIndex = segments.lastIndexOf("comments");
if (commentsIndex >= 0 && commentsIndex < segments.size() - 1) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (path.matches(COMMENT_PATTERN)) {
int commentsIndex = segments.lastIndexOf("comments");
if (commentsIndex >= 0 && commentsIndex < segments.size() - 1) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1));
intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (path.matches(SUBREDDIT_PATTERN)) {
String subredditName = path.substring(3);
if (subredditName.equals("popular") || subredditName.equals("all")) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName);
intent.putExtra(MainActivity.EXTRA_MESSSAGE_FULLNAME, messageFullname);
intent.putExtra(MainActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else {
Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3));
intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
}
} else if (path.matches(USER_PATTERN)) {
Intent intent = new Intent(this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, segments.get(1));
intent.putExtra(ViewUserDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewUserDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else if (path.matches(SIDEBAR_PATTERN)) {
Intent intent = new Intent(this, ViewSidebarActivity.class);
intent.putExtra(ViewSidebarActivity.EXTRA_SUBREDDIT_NAME, path.substring(3, path.length() - 14));
startActivity(intent);
} else if (path.matches(MULTIREDDIT_PATTERN)) {
Intent intent = new Intent(this, ViewMultiRedditDetailActivity.class);
intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_PATH, path);
startActivity(intent);
} else if (path.matches(MULTIREDDIT_PATTERN_2)) {
String subredditName = path.substring(3);
Intent intent = new Intent(this, ViewSubredditDetailActivity.class); Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3)); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent); startActivity(intent);
} else if (authority.equals("redd.it") && path.matches(REDD_IT_POST_PATTERN)) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1));
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (authority.contains("gfycat.com")) {
if (path.matches(GFYCAT_PATTERN)) {
Intent intent = new Intent(this, ViewVideoActivity.class);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, path.substring(1));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (authority.contains("redgifs.com")) {
if (path.matches(REDGIFS_PATTERN)) {
Intent intent = new Intent(this, ViewVideoActivity.class);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, path.substring(7));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (authority.contains("imgur.com")) {
if (path.matches(IMGUR_GALLERY_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_GALLERY);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1));
startActivity(intent);
} else if (path.matches(IMGUR_ALBUM_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_ALBUM);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1));
startActivity(intent);
} else if (path.matches(IMGUR_IMAGE_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_IMAGE);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, path.substring(1));
startActivity(intent);
} else {
deepLinkError(uri);
} }
} else if (path.matches(USER_PATTERN)) {
Intent intent = new Intent(this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, segments.get(1));
intent.putExtra(ViewUserDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewUserDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else if (path.matches(SIDEBAR_PATTERN)) {
Intent intent = new Intent(this, ViewSidebarActivity.class);
intent.putExtra(ViewSidebarActivity.EXTRA_SUBREDDIT_NAME, path.substring(3, path.length() - 14));
startActivity(intent);
} else if (path.matches(MULTIREDDIT_PATTERN)) {
Intent intent = new Intent(this, ViewMultiRedditDetailActivity.class);
intent.putExtra(ViewMultiRedditDetailActivity.EXTRA_MULTIREDDIT_PATH, path);
startActivity(intent);
} else if (path.matches(MULTIREDDIT_PATTERN_2)) {
String subredditName = path.substring(3);
Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent);
} else if (authority.equals("redd.it") && path.matches(REDD_IT_POST_PATTERN)) {
Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1));
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (authority.contains("gfycat.com")) {
if (path.matches(GFYCAT_PATTERN)) {
Intent intent = new Intent(this, ViewVideoActivity.class);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, path.substring(1));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT);
startActivity(intent);
} else {
deepLinkError(uri);
}
} else if (authority.contains("imgur.com")) {
if (path.matches(IMGUR_GALLERY_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_GALLERY);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1));
startActivity(intent);
} else if (path.matches(IMGUR_ALBUM_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_ALBUM);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1));
startActivity(intent);
} else if (path.matches(IMGUR_IMAGE_PATTERN)) {
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_IMAGE);
intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, path.substring(1));
startActivity(intent);
} else { } else {
deepLinkError(uri); deepLinkError(uri);
} }
} else { } else {
deepLinkError(uri); deepLinkError(uri);
} }
} else {
deepLinkError(uri);
} }
} }

View File

@ -78,6 +78,8 @@ public class ViewVideoActivity extends AppCompatActivity {
public static final String EXTRA_PROGRESS_SECONDS = "EPS"; public static final String EXTRA_PROGRESS_SECONDS = "EPS";
public static final String EXTRA_VIDEO_TYPE = "EVT"; public static final String EXTRA_VIDEO_TYPE = "EVT";
public static final String EXTRA_GFYCAT_ID = "EGI"; public static final String EXTRA_GFYCAT_ID = "EGI";
public static final int VIDEO_TYPE_DIRECT = 3;
public static final int VIDEO_TYPE_REDGIFS = 2;
public static final int VIDEO_TYPE_GFYCAT = 1; public static final int VIDEO_TYPE_GFYCAT = 1;
private static final int VIDEO_TYPE_NORMAL = 0; private static final int VIDEO_TYPE_NORMAL = 0;
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
@ -114,6 +116,10 @@ public class ViewVideoActivity extends AppCompatActivity {
@Named("gfycat") @Named("gfycat")
Retrofit gfycatRetrofit; Retrofit gfycatRetrofit;
@Inject
@Named("redgifs")
Retrofit redgifsRetrofit;
@Inject @Inject
@Named("default") @Named("default")
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@ -335,7 +341,8 @@ public class ViewVideoActivity extends AppCompatActivity {
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
videoPlayerView.setPlayer(player); videoPlayerView.setPlayer(player);
if (getIntent().getIntExtra(EXTRA_VIDEO_TYPE, VIDEO_TYPE_NORMAL) == VIDEO_TYPE_GFYCAT) { int videoType = getIntent().getIntExtra(EXTRA_VIDEO_TYPE, VIDEO_TYPE_NORMAL);
if (videoType == VIDEO_TYPE_GFYCAT || videoType == VIDEO_TYPE_REDGIFS) {
if (savedInstanceState != null) { if (savedInstanceState != null) {
String videoUrl = savedInstanceState.getString(VIDEO_URI_STATE); String videoUrl = savedInstanceState.getString(VIDEO_URI_STATE);
if (videoUrl != null) { if (videoUrl != null) {
@ -350,8 +357,15 @@ public class ViewVideoActivity extends AppCompatActivity {
if (gfycatId != null && gfycatId.contains("-")) { if (gfycatId != null && gfycatId.contains("-")) {
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-')); gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
} }
videoFileName = "Gfycat-" + gfycatId + ".mp4"; Retrofit retrofit;
FetchGfycatVideoLinks.fetchGfycatVideoLinks(gfycatRetrofit, gfycatId, if (videoType == VIDEO_TYPE_GFYCAT) {
retrofit = gfycatRetrofit;
videoFileName = "Gfycat-" + gfycatId + ".mp4";
} else {
retrofit = redgifsRetrofit;
videoFileName = "Redgifs-" + gfycatId + ".mp4";
}
FetchGfycatVideoLinks.fetchGfycatVideoLinks(retrofit, gfycatId,
new FetchGfycatVideoLinks.FetchGfycatVideoLinksListener() { new FetchGfycatVideoLinks.FetchGfycatVideoLinksListener() {
@Override @Override
public void success(String webm, String mp4) { public void success(String webm, String mp4) {
@ -367,7 +381,11 @@ public class ViewVideoActivity extends AppCompatActivity {
@Override @Override
public void failed() { public void failed() {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show(); if (videoType == VIDEO_TYPE_GFYCAT) {
Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ViewVideoActivity.this, R.string.fetch_redgifs_video_failed, Toast.LENGTH_SHORT).show();
}
} }
}); });
} else { } else {
@ -376,6 +394,15 @@ public class ViewVideoActivity extends AppCompatActivity {
player.prepare(new DashMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new DashMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);
} }
} if (videoType == VIDEO_TYPE_REDGIFS) {
} else if (videoType == VIDEO_TYPE_DIRECT) {
videoDownloadUrl = mVideoUri.toString();
videoFileName = videoDownloadUrl.substring(videoDownloadUrl.lastIndexOf('/') + 1);
// Produces DataSource instances through which media data is loaded.
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity"));
// Prepare the player with the source.
player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
} else { } else {
videoDownloadUrl = intent.getStringExtra(EXTRA_VIDEO_DOWNLOAD_URL); videoDownloadUrl = intent.getStringExtra(EXTRA_VIDEO_DOWNLOAD_URL);
videoFileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_ID) + ".mp4"; videoFileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_ID) + ".mp4";

View File

@ -100,6 +100,16 @@ class AppModule {
.build(); .build();
} }
@Provides
@Named("redgifs")
@Singleton
Retrofit provideRedgifsRetrofit() {
return new Retrofit.Builder()
.baseUrl(APIUtils.REDGIFS_API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
@Provides @Provides
@Named("imgur") @Named("imgur")
@Singleton @Singleton

View File

@ -22,7 +22,7 @@ public class FetchGfycatVideoLinks {
public static void fetchGfycatVideoLinks(Retrofit gfycatRetrofit, String gfycatId, public static void fetchGfycatVideoLinks(Retrofit gfycatRetrofit, String gfycatId,
FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) { FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) {
gfycatRetrofit.create(GfycatAPI.class).getSubredditData(gfycatId).enqueue(new Callback<String>() { gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit.Post; package ml.docilealligator.infinityforreddit.Post;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.Html; import android.text.Html;
@ -277,6 +278,19 @@ public class ParsePost {
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
post.setVideoUrl(url); post.setVideoUrl(url);
} else if (url.endsWith("mp4")) {
//Video post
int postType = Post.VIDEO_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title,
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(url);
post.setVideoDownloadUrl(url);
} else { } else {
if (url.contains(permalink)) { if (url.contains(permalink)) {
//Text post but with a preview //Text post but with a preview
@ -339,6 +353,19 @@ public class ParsePost {
archived, locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
} else if (url.endsWith("mp4")) {
//Video post
int postType = Post.VIDEO_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title,
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(url);
post.setVideoDownloadUrl(url);
} else { } else {
//CP No Preview Link post //CP No Preview Link post
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
@ -352,6 +379,17 @@ public class ParsePost {
} }
} }
if (post.getPostType() == Post.VIDEO_TYPE) {
try {
Uri uri = Uri.parse(url);
String authority = uri.getAuthority();
if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) {
post.setPostType(Post.LINK_TYPE);
post.setUrl(url);
}
} catch (IllegalArgumentException ignore) { }
}
return post; return post;
} }

View File

@ -324,6 +324,10 @@ public class Post implements Parcelable {
return url; return url;
} }
public void setUrl(String url) {
this.url = url;
}
public String getVideoUrl() { public String getVideoUrl() {
return videoUrl; return videoUrl;
} }
@ -372,6 +376,10 @@ public class Post implements Parcelable {
return postType; return postType;
} }
public void setPostType(int postType) {
this.postType = postType;
}
public int getVoteType() { public int getVoteType() {
return voteType; return voteType;
} }

View File

@ -19,6 +19,7 @@ public class APIUtils {
public static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com"; public static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com";
public static final String API_UPLOAD_VIDEO_URI = "https://reddit-uploaded-video.s3-accelerate.amazonaws.com"; public static final String API_UPLOAD_VIDEO_URI = "https://reddit-uploaded-video.s3-accelerate.amazonaws.com";
public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/"; public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/";
public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v1/gfycats/";
public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/"; public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/";
public static final String CLIENT_ID_KEY = "client_id"; public static final String CLIENT_ID_KEY = "client_id";

View File

@ -734,6 +734,7 @@
<string name="popular">Popular</string> <string name="popular">Popular</string>
<string name="fetch_gfycat_video_failed">Fetch Gfycat video failed</string> <string name="fetch_gfycat_video_failed">Fetch Gfycat video failed</string>
<string name="fetch_redgifs_video_failed">Fetch Redgifs video failed</string>
<string name="fetching_video_info_please_wait">Fetching video info. Please wait.</string> <string name="fetching_video_info_please_wait">Fetching video info. Please wait.</string>
<string name="error_fetching_imgur_media">Cannot load images</string> <string name="error_fetching_imgur_media">Cannot load images</string>