From af3ec4ba26acea2d5f8a585ba321de9dee68bd6b Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 21 Mar 2021 21:35:18 -0400 Subject: [PATCH] backup user argument for GraphQL feed --- .../instagrabber/asyncs/PostFetcher.java | 2 +- .../asyncs/ProfilePostFetchService.java | 2 +- .../instagrabber/utils/ResponseBodyUtils.java | 55 ++++++------------- .../webservices/GraphQLService.java | 22 ++++++-- 4 files changed, 37 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java index eef69f41..ddd670f1 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/PostFetcher.java @@ -133,7 +133,7 @@ public final class PostFetcher extends AsyncTask { // feedModelBuilder.setSliderItems(postModels); // } // return feedModelBuilder.build(); - return ResponseBodyUtils.parseGraphQLItem(media); + return ResponseBodyUtils.parseGraphQLItem(media, null); } } catch (Exception e) { // if (logCollector != null) { diff --git a/app/src/main/java/awais/instagrabber/asyncs/ProfilePostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/ProfilePostFetchService.java index e2cfa10c..02e0e27a 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/ProfilePostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/ProfilePostFetchService.java @@ -49,7 +49,7 @@ public class ProfilePostFetchService implements PostFetcher.PostFetchService { } }; if (isLoggedIn) profileService.fetchPosts(profileModel.getPk(), nextMaxId, cb); - else graphQLService.fetchProfilePosts(profileModel.getPk(), 30, nextMaxId, cb); + else graphQLService.fetchProfilePosts(profileModel.getPk(), 30, nextMaxId, profileModel, cb); } @Override diff --git a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java index 7a5b8361..21047942 100644 --- a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java @@ -675,7 +675,8 @@ public final class ResponseBodyUtils { // return feedModelBuilder.build(); // } - public static Media parseGraphQLItem(final JSONObject itemJson) throws JSONException { + // the "user" argument can be null, it's used because instagram redacts user details from responses + public static Media parseGraphQLItem(final JSONObject itemJson, final User backup) throws JSONException { if (itemJson == null) { return null; } @@ -728,41 +729,28 @@ public final class ResponseBodyUtils { width = dimensions.optInt("width"); } String thumbnailUrl = null; - try { - thumbnailUrl = feedItem.getJSONArray("display_resources") - .getJSONObject(0) - .getString("src"); - } catch (JSONException ignored) {} - // final FeedModel.Builder feedModelBuilder = new FeedModel.Builder() - // .setProfileModel(profileModel) - // .setItemType(isVideo ? MediaItemType.MEDIA_TYPE_VIDEO - // : MediaItemType.MEDIA_TYPE_IMAGE) - // .setViewCount(videoViews) - // .setPostId(feedItem.getString(Constants.EXTRAS_ID)) - // .setDisplayUrl(resourceUrl) - // .setThumbnailUrl(thumbnailUrl != null ? thumbnailUrl : displayUrl) - // .setShortCode(feedItem.getString(Constants.EXTRAS_SHORTCODE)) - // .setPostCaption(captionText) - // .setCommentsCount(commentsCount) - // .setTimestamp(feedItem.optLong("taken_at_timestamp", -1)) - // .setLiked(feedItem.optBoolean("viewer_has_liked")) - // .setBookmarked(feedItem.optBoolean("viewer_has_saved")) - // .setLikesCount(likesCount) - // .setLocationName(locationName) - // .setLocationId(String.valueOf(locationId)) - // .setImageHeight(height) - // .setImageWidth(width); + final JSONArray displayResources = feedItem.getJSONArray("display_resources"); + final List candidates = new ArrayList(); + for (int i = 0; i < displayResources.length(); i++) { + final JSONObject displayResource = displayResources.getJSONObject(i); + candidates.add(new MediaCandidate( + displayResource.getInt("config_width"), + displayResource.getInt("config_height"), + displayResource.getString("src") + )); + } + final ImageVersions2 imageVersions2 = new ImageVersions2(candidates); - User user = null; + User user = backup; long userId = -1; - if (feedItem.has("owner")) { + if (feedItem.has("owner") && user == null) { final JSONObject owner = feedItem.getJSONObject("owner"); final FriendshipStatus friendshipStatus = new FriendshipStatus( false, false, false, false, - owner.optBoolean("is_private"), + false, false, false, false, @@ -774,7 +762,7 @@ public final class ResponseBodyUtils { userId, owner.optString(Constants.EXTRAS_USERNAME), owner.optString("full_name"), - owner.optBoolean("is_private"), + false, owner.optString("profile_pic_url"), null, friendshipStatus, @@ -783,13 +771,6 @@ public final class ResponseBodyUtils { null, null, null, null); } final String id = feedItem.getString(Constants.EXTRAS_ID); - final ImageVersions2 imageVersions2 = new ImageVersions2( - Collections.singletonList(new MediaCandidate( - width, - height, - isVideo ? thumbnailUrl : resourceUrl - )) - ); VideoVersion videoVersion = null; if (isVideo) { videoVersion = new VideoVersion( @@ -821,7 +802,7 @@ public final class ResponseBodyUtils { for (int i = 0; i < children.length(); i++) { final JSONObject child = children.optJSONObject(i); if (child == null) continue; - final Media media = parseGraphQLItem(child); + final Media media = parseGraphQLItem(child, null); media.setIsSidecarChild(true); childItems.add(media); } diff --git a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java index 52e91af1..4cd1c1b6 100644 --- a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java +++ b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java @@ -56,6 +56,7 @@ public class GraphQLService extends BaseService { final String variables, final String arg1, final String arg2, + final User backup, final ServiceCallback callback) { final Map queryMap = new HashMap<>(); queryMap.put("query_hash", queryHash); @@ -66,7 +67,7 @@ public class GraphQLService extends BaseService { public void onResponse(@NonNull final Call call, @NonNull final Response response) { try { // Log.d(TAG, "onResponse: body: " + response.body()); - final PostsFetchResponse postsFetchResponse = parsePostResponse(response, arg1, arg2); + final PostsFetchResponse postsFetchResponse = parsePostResponse(response, arg1, arg2, backup); if (callback != null) { callback.onSuccess(postsFetchResponse); } @@ -96,6 +97,7 @@ public class GraphQLService extends BaseService { "\"after\":\"" + (maxId == null ? "" : maxId) + "\"}", Constants.EXTRAS_LOCATION, "edge_location_to_media", + null, callback); } @@ -108,12 +110,14 @@ public class GraphQLService extends BaseService { "\"after\":\"" + (maxId == null ? "" : maxId) + "\"}", Constants.EXTRAS_HASHTAG, "edge_hashtag_to_media", + null, callback); } public void fetchProfilePosts(final long profileId, final int postsPerPage, final String maxId, + final User backup, final ServiceCallback callback) { fetch("18a7b935ab438c4514b1f742d8fa07a7", "{\"id\":\"" + profileId + "\"," + @@ -121,6 +125,7 @@ public class GraphQLService extends BaseService { "\"after\":\"" + (maxId == null ? "" : maxId) + "\"}", Constants.EXTRAS_USER, "edge_owner_to_timeline_media", + backup, callback); } @@ -134,21 +139,28 @@ public class GraphQLService extends BaseService { "\"after\":\"" + (maxId == null ? "" : maxId) + "\"}", Constants.EXTRAS_USER, "edge_user_to_photos_of_you", + null, callback); } @NonNull - private PostsFetchResponse parsePostResponse(@NonNull final Response response, @NonNull final String arg1, @NonNull final String arg2) + private PostsFetchResponse parsePostResponse(@NonNull final Response response, + @NonNull final String arg1, + @NonNull final String arg2, + final User backup) throws JSONException { if (TextUtils.isEmpty(response.body())) { Log.e(TAG, "parseResponse: feed response body is empty with status code: " + response.code()); return new PostsFetchResponse(Collections.emptyList(), false, null); } - return parseResponseBody(response.body(), arg1, arg2); + return parseResponseBody(response.body(), arg1, arg2, backup); } @NonNull - private PostsFetchResponse parseResponseBody(@NonNull final String body, @NonNull final String arg1, @NonNull final String arg2) + private PostsFetchResponse parseResponseBody(@NonNull final String body, + @NonNull final String arg1, + @NonNull final String arg2, + final User backup) throws JSONException { final List items = new ArrayList<>(); final JSONObject timelineFeed = new JSONObject(body) @@ -174,7 +186,7 @@ public class GraphQLService extends BaseService { if (itemJson == null) { continue; } - final Media media = ResponseBodyUtils.parseGraphQLItem(itemJson); + final Media media = ResponseBodyUtils.parseGraphQLItem(itemJson, backup); if (media != null) { items.add(media); }