From 69c4ab96b99ec9573d47be9206e0da38bf19354f Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 15 Aug 2022 21:58:39 +1000 Subject: Update Redgifs API. --- .../FetchGfycatOrRedgifsVideoLinks.java | 26 +++++++++++++++++++--- .../activities/ViewVideoActivity.java | 17 +++++++------- .../infinityforreddit/utils/APIUtils.java | 3 +-- .../infinityforreddit/utils/JSONUtils.java | 2 ++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index 19e720b5..8040d466 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -22,13 +22,17 @@ public class FetchGfycatOrRedgifsVideoLinks { } public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit, - String gfycatId, + String gfycatId, boolean isGfycatVideo, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { executor.execute(() -> { try { Response response = gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).execute(); if (response.isSuccessful()) { - parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + if (isGfycatVideo) { + parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + } else { + parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + } } else { handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(response.code())); } @@ -49,7 +53,11 @@ public class FetchGfycatOrRedgifsVideoLinks { try { Response response = gfycatCall.execute(); if (response.isSuccessful()) { - parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + if (isGfycatVideo) { + parseGfycatVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + } else { + parseRedgifsVideoLinks(handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener); + } } else { if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatCall.clone(), @@ -92,4 +100,16 @@ public class FetchGfycatOrRedgifsVideoLinks { handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1)); } } + + private static void parseRedgifsVideoLinks(Handler handler, String response, + FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { + try { + String mp4 = new JSONObject(response).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.URLS_KEY) + .getString(JSONUtils.HD_KEY); + handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.success(mp4, mp4)); + } catch (JSONException e) { + e.printStackTrace(); + handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1)); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index b6627337..434390fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -529,9 +529,9 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe if (mVideoUri == null) { if (videoType == VIDEO_TYPE_GFYCAT) { - loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true); + loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, true, savedInstanceState, true); } else { - loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); + loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, false, savedInstanceState, false); } } else { dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, @@ -675,10 +675,11 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe return C.TRACK_TYPE_UNKNOWN; } - private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) { + private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, boolean isGfycatVideo, + Bundle savedInstanceState, boolean needErrorHandling) { progressBar.setVisibility(View.VISIBLE); FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(mExecutor, new Handler(), retrofit, gfycatId, - new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { + isGfycatVideo, new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { @Override public void success(String webm, String mp4) { progressBar.setVisibility(View.GONE); @@ -697,10 +698,10 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe if (videoType == VIDEO_TYPE_GFYCAT) { if (errorCode == 404 && needErrorHandling) { if (mSharedPreferences.getBoolean(SharedPreferencesUtils.AUTOMATICALLY_TRY_REDGIFS, true)) { - loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); + loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, false, savedInstanceState, false); } else { Snackbar.make(coordinatorLayout, R.string.load_video_in_redgifs, Snackbar.LENGTH_INDEFINITE).setAction(R.string.yes, - view -> loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false)).show(); + view -> loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, false, savedInstanceState, false)).show(); } } else { Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show(); @@ -738,7 +739,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe } else { videoFileName = "Redgifs-" + gfycatId + ".mp4"; } - loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true); + loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, true, savedInstanceState, true); } else if (post.isRedgifs()) { videoType = VIDEO_TYPE_REDGIFS; String gfycatId = post.getGfycatId(); @@ -750,7 +751,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe } else { videoFileName = "Redgifs-" + gfycatId + ".mp4"; } - loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); + loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, false, savedInstanceState, false); } else if (post.isStreamable()) { videoType = VIDEO_TYPE_STREAMABLE; String shortCode = post.getStreamableShortCode(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java index 0728f5e8..cb0c0e3c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java @@ -19,8 +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/v2/gifs/"; - public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v1/gfycats/"; + public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v2/gifs/"; public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/"; public static final String PUSHSHIFT_API_BASE_URI = "https://api.pushshift.io/"; public static final String REVEDDIT_API_BASE_URI = "https://api.reveddit.com/"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java index 1e886678..828f2dd1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java @@ -178,4 +178,6 @@ public class JSONUtils { public static final String FILES_KEY = "files"; public static final String MP4_MOBILE_KEY = "mp4-mobile"; public static final String STATUS_KEY = "status"; + public static final String URLS_KEY = "urls"; + public static final String HD_KEY = "hd"; } -- cgit v1.2.3