From 808c841e6547f90587a1d15b3982932321cec73b Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sat, 19 Jun 2021 17:25:40 -0400 Subject: [PATCH] possible anonymous fix? --- .../repositories/GraphQLService.kt | 6 ++-- .../webservices/GraphQLRepository.kt | 31 ++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/repositories/GraphQLService.kt b/app/src/main/java/awais/instagrabber/repositories/GraphQLService.kt index 4be6009f..0e57efad 100644 --- a/app/src/main/java/awais/instagrabber/repositories/GraphQLService.kt +++ b/app/src/main/java/awais/instagrabber/repositories/GraphQLService.kt @@ -8,15 +8,15 @@ interface GraphQLService { @GET("/graphql/query/") suspend fun fetch(@QueryMap(encoded = true) queryParams: Map): String - @GET("/{username}/?__a=1") + @GET("/{username}/") suspend fun getUser(@Path("username") username: String): String @GET("/p/{shortcode}/?__a=1") suspend fun getPost(@Path("shortcode") shortcode: String): String - @GET("/explore/tags/{tag}/?__a=1") + @GET("/explore/tags/{tag}/") suspend fun getTag(@Path("tag") tag: String): String - @GET("/explore/locations/{locationId}/?__a=1") + @GET("/explore/locations/{locationId}/") suspend fun getLocation(@Path("locationId") locationId: Long): String } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/webservices/GraphQLRepository.kt b/app/src/main/java/awais/instagrabber/webservices/GraphQLRepository.kt index 5c7405cf..d4ec39a4 100644 --- a/app/src/main/java/awais/instagrabber/webservices/GraphQLRepository.kt +++ b/app/src/main/java/awais/instagrabber/webservices/GraphQLRepository.kt @@ -1,16 +1,17 @@ package awais.instagrabber.webservices import android.util.Log -import awais.instagrabber.models.enums.FollowingType import awais.instagrabber.repositories.GraphQLService import awais.instagrabber.repositories.responses.* import awais.instagrabber.utils.Constants import awais.instagrabber.utils.ResponseBodyUtils import awais.instagrabber.utils.extensions.TAG +import com.google.gson.Gson import org.json.JSONException import org.json.JSONObject import java.util.* + class GraphQLRepository(private val service: GraphQLService) { // TODO convert string response to a response class @@ -179,8 +180,16 @@ class GraphQLRepository(private val service: GraphQLService) { username: String, ): User { val response = service.getUser(username) - val body = JSONObject(response) - val userJson = body.getJSONObject("graphql").getJSONObject(Constants.EXTRAS_USER) + val body = JSONObject(response + .split("", false, 2).get(0) + .trim().replace(Regex("};$"), "}")) + val userJson = body + .getJSONObject("entry_data") + .getJSONArray("ProfilePage") + .getJSONObject(0) + .getJSONObject("graphql") + .getJSONObject(Constants.EXTRAS_USER) val isPrivate = userJson.getBoolean("is_private") val id = userJson.optLong(Constants.EXTRAS_ID, 0) val timelineMedia = userJson.getJSONObject("edge_owner_to_timeline_media") @@ -231,7 +240,13 @@ class GraphQLRepository(private val service: GraphQLService) { tag: String, ): Hashtag { val response = service.getTag(tag) - val body = JSONObject(response) + val body = JSONObject(response + .split("", false, 2).get(0) + .trim().replace(Regex("};$"), "}")) + .getJSONObject("entry_data") + .getJSONArray("TagPage") + .getJSONObject(0) .getJSONObject("graphql") .getJSONObject(Constants.EXTRAS_HASHTAG) val timelineMedia = body.getJSONObject("edge_hashtag_to_media") @@ -249,7 +264,13 @@ class GraphQLRepository(private val service: GraphQLService) { locationId: Long, ): Location { val response = service.getLocation(locationId) - val body = JSONObject(response) + val body = JSONObject(response + .split("", false, 2).get(0) + .trim().replace(Regex("};$"), "}")) + .getJSONObject("entry_data") + .getJSONArray("LocationsPage") + .getJSONObject(0) .getJSONObject("graphql") .getJSONObject(Constants.EXTRAS_LOCATION) // val timelineMedia = body.getJSONObject("edge_location_to_media")