Treat streamable link posts as video posts.

This commit is contained in:
Alex Ning 2021-12-12 20:48:23 +08:00
parent 1abc703d7e
commit a2c6685dd8
5 changed files with 80 additions and 41 deletions

View File

@ -2,17 +2,15 @@ package ml.docilealligator.infinityforreddit;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.NonNull;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.GfycatAPI; import ml.docilealligator.infinityforreddit.apis.GfycatAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -32,45 +30,42 @@ public class FetchGfycatOrRedgifsVideoLinks {
public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit, public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit,
String gfycatId, String gfycatId,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() { executor.execute(() -> {
@Override try {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { Response<String> response = gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else { } else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()));
} }
} } catch (IOException e) {
e.printStackTrace();
@Override handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1));
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
} }
}); });
} }
public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler, public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler,
Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, Retrofit gfycatRetrofit, Retrofit redgifsRetrofit,
String gfycatId, boolean isGfycatVideo, String gfycatId, boolean isGfycatVideo,
boolean automaticallyTryRedgifs) { boolean automaticallyTryRedgifs) {
gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); executor.execute(() -> {
gfycatCall.enqueue(new Callback<String>() { gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId);
@Override try {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { Response<String> response = gfycatCall.execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else { } else {
if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) {
fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit, fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit,
redgifsRetrofit, gfycatId, false, false); redgifsRetrofit, gfycatId, false, false);
} else { } else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()));
} }
} }
} } catch (IOException e) {
e.printStackTrace();
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchGfycatOrRedgifsVideoLinksListener.failed(-1); fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
} }
}); });
@ -82,7 +77,7 @@ public class FetchGfycatOrRedgifsVideoLinks {
} }
} }
private static void parseGfycatVideoLinks(Executor executor, Handler handler, String response, private static void parseGfycatVideoLinks(Handler handler, String response,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
try { try {
JSONObject jsonObject = new JSONObject(response); JSONObject jsonObject = new JSONObject(response);

View File

@ -1624,6 +1624,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
intent.setData(Uri.parse(mPost.getVideoUrl())); intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl());
} }
} else if (mPost.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(mPost.getVideoUrl())); intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl());
@ -1835,6 +1838,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
} else if (mPost.isRedgifs()) { } else if (mPost.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId());
} else if (mPost.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(mPost.getVideoUrl())); intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName());
@ -2154,6 +2160,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
} else if (mPost.isRedgifs()) { } else if (mPost.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId());
} else if (mPost.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(mPost.getVideoUrl())); intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName());

View File

@ -69,14 +69,12 @@ import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks;
import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface;
import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy;
import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.SaveThing;
import ml.docilealligator.infinityforreddit.VoteThing; import ml.docilealligator.infinityforreddit.VoteThing;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.SearchResultActivity;
import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity; import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity;
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity; import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity;
@ -1891,6 +1889,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
} else if (post.isRedgifs()) { } else if (post.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, post.getGfycatId()); intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, post.getGfycatId());
} else if (post.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, post.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, post.getSubredditName()); intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, post.getSubredditName());
@ -2626,6 +2627,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl());
} }
} else if (post.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, post.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl());
@ -3907,6 +3911,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl());
} }
} else if (post.isStreamable()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE);
intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, post.getStreamableShortCode());
} else { } else {
intent.setData(Uri.parse(post.getVideoUrl())); intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl());

View File

@ -598,20 +598,26 @@ public class ParsePost {
Uri uri = Uri.parse(url); Uri uri = Uri.parse(url);
String authority = uri.getAuthority(); String authority = uri.getAuthority();
// Gyfcat ids must be lowercase to resolve to a video through the api, we are not if (authority != null) {
// guaranteed to get an id that is all lowercase. if (authority.contains("gfycat.com")) {
String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase(); String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase();
post.setPostType(Post.VIDEO_TYPE);
if (authority != null && (authority.contains("gfycat.com"))) { post.setIsGfycat(true);
post.setPostType(Post.VIDEO_TYPE); post.setVideoUrl(url);
post.setIsGfycat(true); post.setGfycatId(gfycatId);
post.setVideoUrl(url); } else if (authority.contains("redgifs.com")) {
post.setGfycatId(gfycatId); String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase();
} else if (authority != null && authority.contains("redgifs.com")) { post.setPostType(Post.VIDEO_TYPE);
post.setPostType(Post.VIDEO_TYPE); post.setIsRedgifs(true);
post.setIsRedgifs(true); post.setVideoUrl(url);
post.setVideoUrl(url); post.setGfycatId(gfycatId);
post.setGfycatId(gfycatId); } else if (authority.equals("streamable.com")) {
String shortCode = url.substring(url.lastIndexOf("/") + 1);
post.setPostType(Post.VIDEO_TYPE);
post.setIsStreamable(true);
post.setVideoUrl(url);
post.setStreamableShortCode(shortCode);
}
} }
} }
} }

View File

@ -51,8 +51,10 @@ public class Post implements Parcelable {
private String videoUrl; private String videoUrl;
private String videoDownloadUrl; private String videoDownloadUrl;
private String gfycatId; private String gfycatId;
private String streamableShortCode;
private boolean isGfycat; private boolean isGfycat;
private boolean isRedgifs; private boolean isRedgifs;
private boolean isStreamable;
private boolean loadGfyOrRedgifsVideoSuccess; private boolean loadGfyOrRedgifsVideoSuccess;
private String permalink; private String permalink;
private String flair; private String flair;
@ -172,8 +174,10 @@ public class Post implements Parcelable {
videoUrl = in.readString(); videoUrl = in.readString();
videoDownloadUrl = in.readString(); videoDownloadUrl = in.readString();
gfycatId = in.readString(); gfycatId = in.readString();
streamableShortCode = in.readString();
isGfycat = in.readByte() != 0; isGfycat = in.readByte() != 0;
isRedgifs = in.readByte() != 0; isRedgifs = in.readByte() != 0;
isStreamable = in.readByte() != 0;
loadGfyOrRedgifsVideoSuccess = in.readByte() != 0; loadGfyOrRedgifsVideoSuccess = in.readByte() != 0;
permalink = in.readString(); permalink = in.readString();
flair = in.readString(); flair = in.readString();
@ -321,6 +325,14 @@ public class Post implements Parcelable {
this.gfycatId = gfycatId; this.gfycatId = gfycatId;
} }
public String getStreamableShortCode() {
return streamableShortCode;
}
public void setStreamableShortCode(String shortCode) {
this.streamableShortCode = shortCode;
}
public boolean isGfycat() { public boolean isGfycat() {
return isGfycat; return isGfycat;
} }
@ -337,6 +349,14 @@ public class Post implements Parcelable {
this.isRedgifs = isRedgifs; this.isRedgifs = isRedgifs;
} }
public boolean isStreamable() {
return isStreamable;
}
public void setIsStreamable(boolean isStreamable) {
this.isStreamable = isStreamable;
}
public boolean isLoadGfycatOrRedgifsVideoSuccess() { public boolean isLoadGfycatOrRedgifsVideoSuccess() {
return loadGfyOrRedgifsVideoSuccess; return loadGfyOrRedgifsVideoSuccess;
} }
@ -532,8 +552,10 @@ public class Post implements Parcelable {
parcel.writeString(videoUrl); parcel.writeString(videoUrl);
parcel.writeString(videoDownloadUrl); parcel.writeString(videoDownloadUrl);
parcel.writeString(gfycatId); parcel.writeString(gfycatId);
parcel.writeString(streamableShortCode);
parcel.writeByte((byte) (isGfycat ? 1 : 0)); parcel.writeByte((byte) (isGfycat ? 1 : 0));
parcel.writeByte((byte) (isRedgifs ? 1 : 0)); parcel.writeByte((byte) (isRedgifs ? 1 : 0));
parcel.writeByte((byte) (isStreamable ? 1 : 0));
parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0)); parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0));
parcel.writeString(permalink); parcel.writeString(permalink);
parcel.writeString(flair); parcel.writeString(flair);