Update Redgifs API.

This commit is contained in:
Docile-Alligator 2022-08-15 21:58:39 +10:00
parent 8b8a9073dd
commit 69c4ab96b9
4 changed files with 35 additions and 13 deletions

View File

@ -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<String> 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<String> 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));
}
}
}

View File

@ -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();

View File

@ -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/";

View File

@ -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";
}