Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3281 from gringofe/fix-gfycat-redgifs-migration
Browse files Browse the repository at this point in the history
Fix gifdeliverynetwork redirection
  • Loading branch information
ccrama authored Nov 8, 2020
2 parents 3fdce9d + 92e1671 commit b31e865
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions app/src/main/java/me/ccrama/redditslide/util/GifUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand All @@ -493,22 +521,21 @@ 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);
HttpURLConnection ucon = (HttpURLConnection) newUrl.openConnection();
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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b31e865

Please sign in to comment.