From 92e1671514f713675febd2824e34e15c2975be10 Mon Sep 17 00:00:00 2001 From: gringofe Date: Fri, 23 Oct 2020 11:15:14 +0200 Subject: [PATCH] fix gifdelivernetwork redirection --- .../me/ccrama/redditslide/util/GifUtils.java | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/me/ccrama/redditslide/util/GifUtils.java b/app/src/main/java/me/ccrama/redditslide/util/GifUtils.java index 04ef3ac630..08f55be5e6 100644 --- a/app/src/main/java/me/ccrama/redditslide/util/GifUtils.java +++ b/app/src/main/java/me/ccrama/redditslide/util/GifUtils.java @@ -12,6 +12,7 @@ import android.os.AsyncTask; import android.os.Environment; import android.os.Looper; +import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; @@ -473,6 +474,33 @@ public static VideoType getVideoType(String url) { return VideoType.OTHER; } + /** + * Get an API response for a given host and gfy name + * + * @param host the host to send the req to + * @param name the name of the gfy + * @return the result + */ + JsonObject getApiResponse(String host, String name){ + String gfycatUrl = "https://api." + host + ".com/v1/gfycats" + name; + return HttpUtil.getJsonObject(client, gson, gfycatUrl); + } + + /** + * Get the correct mp4/mobile url from a given result JsonObject + * + * @param result the result to check + * @return the video url + */ + String getUrlFromApi(JsonObject result){ + if (result.getAsJsonObject("gfyItem").has("mobileUrl")) { + return result.getAsJsonObject("gfyItem").get("mobileUrl").getAsString(); + } else { + return result.getAsJsonObject("gfyItem").get("mp4Url").getAsString(); + } + } + + OkHttpClient client = Reddit.client; /** @@ -493,14 +521,13 @@ Uri loadGfycat(String name, String fullUrl, Gson gson) { if (name.contains("-")) { name = name.split("-")[0]; } - String gfycatUrl = "https://api." + host + ".com/v1/gfycats" + name; - final JsonObject result = HttpUtil.getJsonObject(client, gson, gfycatUrl); - String obj; + final JsonObject result = getApiResponse(host, name); if (result == null || result.get("gfyItem") == null || result.getAsJsonObject("gfyItem") .get("mp4Url") .isJsonNull()) { //If the result null, the gfycat link may be redirecting to gifdeliverynetwork which is powered by redgifs. - //Try getting the redirected url from gfycat and check if redirected url is gifdeliverynetwork and return the url + //Try getting the redirected url from gfycat and check if redirected url is gifdeliverynetwork and if it is, + // we fetch the actual .mp4/.webm url from the redgifs api if (result == null) { try { URL newUrl = new URL(fullUrl); @@ -508,7 +535,7 @@ Uri loadGfycat(String name, String fullUrl, Gson gson) { ucon.setInstanceFollowRedirects(false); String secondURL = new URL(ucon.getHeaderField("location")).toString(); if (secondURL.contains("gifdeliverynetwork")){ - return Uri.parse(secondURL); + return Uri.parse(getUrlFromApi(getApiResponse("redgifs", name))); } } catch (IOException e) { @@ -550,14 +577,9 @@ public void onClick(DialogInterface dialog, int which) { } return null; - } else { - if (result.getAsJsonObject("gfyItem").has("mobileUrl")) { - obj = result.getAsJsonObject("gfyItem").get("mobileUrl").getAsString(); - } else { - obj = result.getAsJsonObject("gfyItem").get("mp4Url").getAsString(); - } } - return Uri.parse(obj); + + return Uri.parse(getUrlFromApi(result)); } /*Loads a direct MP4, used for DASH mp4 or direct/imgur videos, currently unused