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 androidx.annotation.NonNull;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.GfycatAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
@ -32,45 +30,42 @@ public class FetchGfycatOrRedgifsVideoLinks {
public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit,
String gfycatId,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
executor.execute(() -> {
try {
Response<String> response = gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).execute();
if (response.isSuccessful()) {
parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
} catch (IOException e) {
e.printStackTrace();
handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1));
}
});
}
public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler,
Retrofit gfycatRetrofit, Retrofit redgifsRetrofit,
String gfycatId, boolean isGfycatVideo,
boolean automaticallyTryRedgifs) {
gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId);
gfycatCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
executor.execute(() -> {
gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId);
try {
Response<String> response = gfycatCall.execute();
if (response.isSuccessful()) {
parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else {
if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) {
fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit,
redgifsRetrofit, gfycatId, false, false);
} else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()));
}
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
} catch (IOException e) {
e.printStackTrace();
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) {
try {
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.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 {
intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl());
@ -1835,6 +1838,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
} else if (mPost.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
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 {
intent.setData(Uri.parse(mPost.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName());
@ -2154,6 +2160,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
} else if (mPost.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
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 {
intent.setData(Uri.parse(mPost.getVideoUrl()));
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 ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks;
import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface;
import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy;
import ml.docilealligator.infinityforreddit.SaveThing;
import ml.docilealligator.infinityforreddit.VoteThing;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.SearchResultActivity;
import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity;
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity;
@ -1891,6 +1889,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
} else if (post.isRedgifs()) {
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
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 {
intent.setData(Uri.parse(post.getVideoUrl()));
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.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 {
intent.setData(Uri.parse(post.getVideoUrl()));
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.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 {
intent.setData(Uri.parse(post.getVideoUrl()));
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl());

View File

@ -598,20 +598,26 @@ public class ParsePost {
Uri uri = Uri.parse(url);
String authority = uri.getAuthority();
// Gyfcat ids must be lowercase to resolve to a video through the api, we are not
// guaranteed to get an id that is all lowercase.
String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase();
if (authority != null && (authority.contains("gfycat.com"))) {
post.setPostType(Post.VIDEO_TYPE);
post.setIsGfycat(true);
post.setVideoUrl(url);
post.setGfycatId(gfycatId);
} else if (authority != null && authority.contains("redgifs.com")) {
post.setPostType(Post.VIDEO_TYPE);
post.setIsRedgifs(true);
post.setVideoUrl(url);
post.setGfycatId(gfycatId);
if (authority != null) {
if (authority.contains("gfycat.com")) {
String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase();
post.setPostType(Post.VIDEO_TYPE);
post.setIsGfycat(true);
post.setVideoUrl(url);
post.setGfycatId(gfycatId);
} else if (authority.contains("redgifs.com")) {
String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase();
post.setPostType(Post.VIDEO_TYPE);
post.setIsRedgifs(true);
post.setVideoUrl(url);
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 videoDownloadUrl;
private String gfycatId;
private String streamableShortCode;
private boolean isGfycat;
private boolean isRedgifs;
private boolean isStreamable;
private boolean loadGfyOrRedgifsVideoSuccess;
private String permalink;
private String flair;
@ -172,8 +174,10 @@ public class Post implements Parcelable {
videoUrl = in.readString();
videoDownloadUrl = in.readString();
gfycatId = in.readString();
streamableShortCode = in.readString();
isGfycat = in.readByte() != 0;
isRedgifs = in.readByte() != 0;
isStreamable = in.readByte() != 0;
loadGfyOrRedgifsVideoSuccess = in.readByte() != 0;
permalink = in.readString();
flair = in.readString();
@ -321,6 +325,14 @@ public class Post implements Parcelable {
this.gfycatId = gfycatId;
}
public String getStreamableShortCode() {
return streamableShortCode;
}
public void setStreamableShortCode(String shortCode) {
this.streamableShortCode = shortCode;
}
public boolean isGfycat() {
return isGfycat;
}
@ -337,6 +349,14 @@ public class Post implements Parcelable {
this.isRedgifs = isRedgifs;
}
public boolean isStreamable() {
return isStreamable;
}
public void setIsStreamable(boolean isStreamable) {
this.isStreamable = isStreamable;
}
public boolean isLoadGfycatOrRedgifsVideoSuccess() {
return loadGfyOrRedgifsVideoSuccess;
}
@ -532,8 +552,10 @@ public class Post implements Parcelable {
parcel.writeString(videoUrl);
parcel.writeString(videoDownloadUrl);
parcel.writeString(gfycatId);
parcel.writeString(streamableShortCode);
parcel.writeByte((byte) (isGfycat ? 1 : 0));
parcel.writeByte((byte) (isRedgifs ? 1 : 0));
parcel.writeByte((byte) (isStreamable ? 1 : 0));
parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0));
parcel.writeString(permalink);
parcel.writeString(flair);