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 {
@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 REDD_IT_POST_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_ALBUM_PATTERN = "/(album|a)/\\w+/?";
private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?";
@ -76,6 +77,28 @@ public class LinkResolverActivity extends AppCompatActivity {
path = path.substring(0, path.length() - 1);
}
if (path.endsWith("jpg") || path.endsWith("png")) {
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);
@ -165,6 +188,15 @@ public class LinkResolverActivity extends AppCompatActivity {
} 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);
@ -191,6 +223,7 @@ public class LinkResolverActivity extends AppCompatActivity {
deepLinkError(uri);
}
}
}
finish();
}

View File

@ -78,6 +78,8 @@ public class ViewVideoActivity extends AppCompatActivity {
public static final String EXTRA_PROGRESS_SECONDS = "EPS";
public static final String EXTRA_VIDEO_TYPE = "EVT";
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;
private static final int VIDEO_TYPE_NORMAL = 0;
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
@ -114,6 +116,10 @@ public class ViewVideoActivity extends AppCompatActivity {
@Named("gfycat")
Retrofit gfycatRetrofit;
@Inject
@Named("redgifs")
Retrofit redgifsRetrofit;
@Inject
@Named("default")
SharedPreferences mSharedPreferences;
@ -335,7 +341,8 @@ public class ViewVideoActivity extends AppCompatActivity {
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
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) {
String videoUrl = savedInstanceState.getString(VIDEO_URI_STATE);
if (videoUrl != null) {
@ -350,8 +357,15 @@ public class ViewVideoActivity extends AppCompatActivity {
if (gfycatId != null && gfycatId.contains("-")) {
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
}
Retrofit retrofit;
if (videoType == VIDEO_TYPE_GFYCAT) {
retrofit = gfycatRetrofit;
videoFileName = "Gfycat-" + gfycatId + ".mp4";
FetchGfycatVideoLinks.fetchGfycatVideoLinks(gfycatRetrofit, gfycatId,
} else {
retrofit = redgifsRetrofit;
videoFileName = "Redgifs-" + gfycatId + ".mp4";
}
FetchGfycatVideoLinks.fetchGfycatVideoLinks(retrofit, gfycatId,
new FetchGfycatVideoLinks.FetchGfycatVideoLinksListener() {
@Override
public void success(String webm, String mp4) {
@ -367,7 +381,11 @@ public class ViewVideoActivity extends AppCompatActivity {
@Override
public void failed() {
progressBar.setVisibility(View.GONE);
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 {
@ -376,6 +394,15 @@ public class ViewVideoActivity extends AppCompatActivity {
player.prepare(new DashMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
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 {
videoDownloadUrl = intent.getStringExtra(EXTRA_VIDEO_DOWNLOAD_URL);
videoFileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_ID) + ".mp4";

View File

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

View File

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

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit.Post;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Html;
@ -277,6 +278,19 @@ public class ParsePost {
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
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 {
if (url.contains(permalink)) {
//Text post but with a preview
@ -339,6 +353,19 @@ public class ParsePost {
archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
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 {
//CP No Preview Link post
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;
}

View File

@ -324,6 +324,10 @@ public class Post implements Parcelable {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getVideoUrl() {
return videoUrl;
}
@ -372,6 +376,10 @@ public class Post implements Parcelable {
return postType;
}
public void setPostType(int postType) {
this.postType = postType;
}
public int getVoteType() {
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_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 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 CLIENT_ID_KEY = "client_id";

View File

@ -734,6 +734,7 @@
<string name="popular">Popular</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="error_fetching_imgur_media">Cannot load images</string>