From 4b6ab15e26d0a13555fc9509547925d9425986d8 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 24 May 2021 22:49:32 +0900 Subject: [PATCH] Fix a variable name, add logging to upload video request and handle non json errors correctly. --- .../instagrabber/models/UploadVideoOptions.kt | 2 +- .../instagrabber/utils/MediaUploadHelper.kt | 39 +++++++++---------- .../instagrabber/utils/MediaUploader.java | 1 + .../interceptors/IgErrorsInterceptor.java | 15 ++++++- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/models/UploadVideoOptions.kt b/app/src/main/java/awais/instagrabber/models/UploadVideoOptions.kt index 1c5a25a8..d0baa4eb 100644 --- a/app/src/main/java/awais/instagrabber/models/UploadVideoOptions.kt +++ b/app/src/main/java/awais/instagrabber/models/UploadVideoOptions.kt @@ -11,7 +11,7 @@ data class UploadVideoOptions( val height: Int = 0, val isSideCar: Boolean = false, // Stories - val isForAlbum: Boolean = false, + val forAlbum: Boolean = false, val isDirect: Boolean = false, val isDirectVoice: Boolean = false, val isForDirectStory: Boolean = false, diff --git a/app/src/main/java/awais/instagrabber/utils/MediaUploadHelper.kt b/app/src/main/java/awais/instagrabber/utils/MediaUploadHelper.kt index 31ed2f67..cdbdca8a 100644 --- a/app/src/main/java/awais/instagrabber/utils/MediaUploadHelper.kt +++ b/app/src/main/java/awais/instagrabber/utils/MediaUploadHelper.kt @@ -30,27 +30,24 @@ private fun createPhotoRuploadParams(options: UploadPhotoOptions): Map { - val retryContextString = retryContextString - return listOfNotNull( - "retry_context" to retryContextString, - "media_type" to "2", - "xsharing_user_ids" to "[]", - "upload_id" to options.uploadId, - "upload_media_width" to options.width.toString(), - "upload_media_height" to options.height.toString(), - "upload_media_duration_ms" to options.duration.toString(), - if (options.isSideCar) "is_sidecar" to "1" else null, - if (options.isForAlbum) "for_album" to "1" else null, - if (options.isDirect) "direct_v2" to "1" else null, - *(if (options.isForDirectStory) arrayOf( - "for_direct_story" to "1", - "content_tags" to "" - ) else emptyArray()), - if (options.isIgtvVideo) "is_igtv_video" to "1" else null, - if (options.isDirectVoice) "is_direct_voice" to "1" else null, - ).toMap() -} +private fun createVideoRuploadParams(options: UploadVideoOptions): Map = listOfNotNull( + "retry_context" to retryContextString, + "media_type" to "2", + "xsharing_user_ids" to "[]", + "upload_id" to options.uploadId, + "upload_media_width" to options.width.toString(), + "upload_media_height" to options.height.toString(), + "upload_media_duration_ms" to options.duration.toString(), + if (options.isSideCar) "is_sidecar" to "1" else null, + if (options.forAlbum) "for_album" to "1" else null, + if (options.isDirect) "direct_v2" to "1" else null, + *(if (options.isForDirectStory) arrayOf( + "for_direct_story" to "1", + "content_tags" to "" + ) else emptyArray()), + if (options.isIgtvVideo) "is_igtv_video" to "1" else null, + if (options.isDirectVoice) "is_direct_voice" to "1" else null, +).toMap() val retryContextString: String get() { diff --git a/app/src/main/java/awais/instagrabber/utils/MediaUploader.java b/app/src/main/java/awais/instagrabber/utils/MediaUploader.java index 2b197e8c..3045e7e5 100644 --- a/app/src/main/java/awais/instagrabber/utils/MediaUploader.java +++ b/app/src/main/java/awais/instagrabber/utils/MediaUploader.java @@ -110,6 +110,7 @@ public final class MediaUploader { @NonNull final OnMediaUploadCompleteListener listener) { try { final OkHttpClient client = new OkHttpClient.Builder() + // .addInterceptor(new LoggingInterceptor()) .addInterceptor(new AddCookiesInterceptor()) .followRedirects(false) .followSslRedirects(false) diff --git a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java index cf08b9dc..83c91d0d 100644 --- a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -9,6 +9,7 @@ import androidx.annotation.NonNull; import androidx.annotation.StringRes; import androidx.fragment.app.FragmentManager; +import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; @@ -70,8 +71,18 @@ public class IgErrorsInterceptor implements Interceptor { try { final String bodyString = body.string(); Log.d(TAG, "checkError: " + bodyString); - final JSONObject jsonObject = new JSONObject(bodyString); - String message = jsonObject.optString("message"); + JSONObject jsonObject = null; + try { + jsonObject = new JSONObject(bodyString); + } catch (JSONException e) { + Log.e(TAG, "checkError: ", e); + } + String message; + if (jsonObject != null) { + message = jsonObject.optString("message"); + } else { + message = bodyString; + } if (!TextUtils.isEmpty(message)) { message = message.toLowerCase(); switch (message) {