From 05156442cc8c0433d2a63e8c8cd96d508c345cb9 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 16 Mar 2021 21:16:06 -0400 Subject: [PATCH] hashtag backend fix --- .../fragments/HashTagFragment.java | 9 +-- .../repositories/TagsRepository.java | 15 +++-- .../instagrabber/webservices/TagsService.java | 64 +++++++------------ 3 files changed, 35 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index 96d99644..2144b08d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -416,11 +416,12 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe hashtagDetailsBinding.btnFollowTag.setOnClickListener(v -> { final String cookie = settingsHelper.getString(Constants.COOKIE); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); - final String ua = settingsHelper.getString(Constants.BROWSER_UA); - if (csrfToken != null) { + final long userId = CookieUtils.getUserIdFromCookie(cookie); + final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); + if (csrfToken != null && userId != null && deviceUuid != null) { hashtagDetailsBinding.btnFollowTag.setClickable(false); if (!hashtagModel.getFollowing()) { - tagsService.follow(ua, hashtag.substring(1), csrfToken, new ServiceCallback() { + tagsService.follow(hashtag.substring(1), csrfToken, userId, deviceUuid, new ServiceCallback() { @Override public void onSuccess(final Boolean result) { hashtagDetailsBinding.btnFollowTag.setClickable(true); @@ -448,7 +449,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe }); return; } - tagsService.unfollow(ua, hashtag.substring(1), csrfToken, new ServiceCallback() { + tagsService.unfollow(hashtag.substring(1), csrfToken, userId, deviceUuid, new ServiceCallback() { @Override public void onSuccess(final Boolean result) { hashtagDetailsBinding.btnFollowTag.setClickable(true); diff --git a/app/src/main/java/awais/instagrabber/repositories/TagsRepository.java b/app/src/main/java/awais/instagrabber/repositories/TagsRepository.java index 5d316b97..d5e7443e 100644 --- a/app/src/main/java/awais/instagrabber/repositories/TagsRepository.java +++ b/app/src/main/java/awais/instagrabber/repositories/TagsRepository.java @@ -4,6 +4,8 @@ import java.util.Map; import awais.instagrabber.repositories.responses.TagFeedResponse; import retrofit2.Call; +import retrofit2.http.FieldMap; +import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.Header; import retrofit2.http.POST; @@ -11,15 +13,14 @@ import retrofit2.http.Path; import retrofit2.http.QueryMap; public interface TagsRepository { - - @POST("/web/tags/follow/{tag}/") - Call follow(@Header("User-Agent") String userAgent, - @Header("x-csrftoken") String csrfToken, + @FormUrlEncoded + @POST("/api/v1/tags/follow/{tag}/") + Call follow(@FieldMap final Map signedForm, @Path("tag") String tag); - @POST("/web/tags/unfollow/{tag}/") - Call unfollow(@Header("User-Agent") String userAgent, - @Header("x-csrftoken") String csrfToken, + @FormUrlEncoded + @POST("/api/v1/tags/unfollow/{tag}/") + Call unfollow(@FieldMap final Map signedForm, @Path("tag") String tag); @GET("/api/v1/feed/tag/{tag}/") diff --git a/app/src/main/java/awais/instagrabber/webservices/TagsService.java b/app/src/main/java/awais/instagrabber/webservices/TagsService.java index 532a61d4..61f31261 100644 --- a/app/src/main/java/awais/instagrabber/webservices/TagsService.java +++ b/app/src/main/java/awais/instagrabber/webservices/TagsService.java @@ -9,10 +9,14 @@ import com.google.common.collect.ImmutableMap; import org.json.JSONException; import org.json.JSONObject; +import java.util.HashMap; +import java.util.Map; + import awais.instagrabber.repositories.TagsRepository; import awais.instagrabber.repositories.responses.PostsFetchResponse; import awais.instagrabber.repositories.responses.TagFeedResponse; import awais.instagrabber.utils.TextUtils; +import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -24,14 +28,9 @@ public class TagsService extends BaseService { private static TagsService instance; - private final TagsRepository webRepository; private final TagsRepository repository; private TagsService() { - final Retrofit webRetrofit = getRetrofitBuilder() - .baseUrl("https://www.instagram.com/") - .build(); - webRepository = webRetrofit.create(TagsRepository.class); final Retrofit retrofit = getRetrofitBuilder() .baseUrl("https://i.instagram.com/") .build(); @@ -45,11 +44,17 @@ public class TagsService extends BaseService { return instance; } - public void follow(@NonNull final String ua, - @NonNull final String tag, + public void follow(@NonNull final String tag, @NonNull final String csrfToken, + @NonNull final long userId, + @NonNull final String deviceUuid, final ServiceCallback callback) { - final Call request = webRepository.follow(ua, csrfToken, tag); + final Map form = new HashMap<>(3); + form.put("_csrftoken", csrfToken); + form.put("_uid", userId); + form.put("_uuid", deviceUuid); + final Map signedForm = Utils.sign(form); + final Call request = repository.follow(signedForm, tag); request.enqueue(new Callback() { @Override public void onResponse(@NonNull final Call call, @NonNull final Response response) { @@ -75,11 +80,17 @@ public class TagsService extends BaseService { }); } - public void unfollow(@NonNull final String ua, - @NonNull final String tag, + public void unfollow(@NonNull final String tag, @NonNull final String csrfToken, + @NonNull final long userId, + @NonNull final String deviceUuid, final ServiceCallback callback) { - final Call request = webRepository.unfollow(ua, csrfToken, tag); + final Map form = new HashMap<>(3); + form.put("_csrftoken", csrfToken); + form.put("_uid", userId); + form.put("_uuid", deviceUuid); + final Map signedForm = Utils.sign(form); + final Call request = repository.unfollow(signedForm, tag); request.enqueue(new Callback() { @Override public void onResponse(@NonNull final Call call, @NonNull final Response response) { @@ -139,35 +150,4 @@ public class TagsService extends BaseService { } }); } - - // private PostsFetchResponse parseResponse(@NonNull final String body) throws JSONException { - // final JSONObject root = new JSONObject(body); - // final boolean moreAvailable = root.optBoolean("more_available"); - // final String nextMaxId = root.optString("next_max_id"); - // final JSONArray itemsJson = root.optJSONArray("items"); - // final List items = parseItems(itemsJson); - // return new PostsFetchResponse( - // items, - // moreAvailable, - // nextMaxId - // ); - // } - - // private List parseItems(final JSONArray items) throws JSONException { - // if (items == null) { - // return Collections.emptyList(); - // } - // final List feedModels = new ArrayList<>(); - // for (int i = 0; i < items.length(); i++) { - // final JSONObject itemJson = items.optJSONObject(i); - // if (itemJson == null) { - // continue; - // } - // final FeedModel feedModel = ResponseBodyUtils.parseItem(itemJson); - // if (feedModel != null) { - // feedModels.add(feedModel); - // } - // } - // return feedModels; - // } }