hashtag backend fix

This commit is contained in:
Austin Huang 2021-03-16 21:16:06 -04:00
parent 6a524b7616
commit 05156442cc
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
3 changed files with 35 additions and 53 deletions

View File

@ -416,11 +416,12 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
hashtagDetailsBinding.btnFollowTag.setOnClickListener(v -> { hashtagDetailsBinding.btnFollowTag.setOnClickListener(v -> {
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
final String ua = settingsHelper.getString(Constants.BROWSER_UA); final long userId = CookieUtils.getUserIdFromCookie(cookie);
if (csrfToken != null) { final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
if (csrfToken != null && userId != null && deviceUuid != null) {
hashtagDetailsBinding.btnFollowTag.setClickable(false); hashtagDetailsBinding.btnFollowTag.setClickable(false);
if (!hashtagModel.getFollowing()) { if (!hashtagModel.getFollowing()) {
tagsService.follow(ua, hashtag.substring(1), csrfToken, new ServiceCallback<Boolean>() { tagsService.follow(hashtag.substring(1), csrfToken, userId, deviceUuid, new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
hashtagDetailsBinding.btnFollowTag.setClickable(true); hashtagDetailsBinding.btnFollowTag.setClickable(true);
@ -448,7 +449,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
}); });
return; return;
} }
tagsService.unfollow(ua, hashtag.substring(1), csrfToken, new ServiceCallback<Boolean>() { tagsService.unfollow(hashtag.substring(1), csrfToken, userId, deviceUuid, new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
hashtagDetailsBinding.btnFollowTag.setClickable(true); hashtagDetailsBinding.btnFollowTag.setClickable(true);

View File

@ -4,6 +4,8 @@ import java.util.Map;
import awais.instagrabber.repositories.responses.TagFeedResponse; import awais.instagrabber.repositories.responses.TagFeedResponse;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.Header; import retrofit2.http.Header;
import retrofit2.http.POST; import retrofit2.http.POST;
@ -11,15 +13,14 @@ import retrofit2.http.Path;
import retrofit2.http.QueryMap; import retrofit2.http.QueryMap;
public interface TagsRepository { public interface TagsRepository {
@FormUrlEncoded
@POST("/web/tags/follow/{tag}/") @POST("/api/v1/tags/follow/{tag}/")
Call<String> follow(@Header("User-Agent") String userAgent, Call<String> follow(@FieldMap final Map<String, String> signedForm,
@Header("x-csrftoken") String csrfToken,
@Path("tag") String tag); @Path("tag") String tag);
@POST("/web/tags/unfollow/{tag}/") @FormUrlEncoded
Call<String> unfollow(@Header("User-Agent") String userAgent, @POST("/api/v1/tags/unfollow/{tag}/")
@Header("x-csrftoken") String csrfToken, Call<String> unfollow(@FieldMap final Map<String, String> signedForm,
@Path("tag") String tag); @Path("tag") String tag);
@GET("/api/v1/feed/tag/{tag}/") @GET("/api/v1/feed/tag/{tag}/")

View File

@ -9,10 +9,14 @@ import com.google.common.collect.ImmutableMap;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import awais.instagrabber.repositories.TagsRepository; import awais.instagrabber.repositories.TagsRepository;
import awais.instagrabber.repositories.responses.PostsFetchResponse; import awais.instagrabber.repositories.responses.PostsFetchResponse;
import awais.instagrabber.repositories.responses.TagFeedResponse; import awais.instagrabber.repositories.responses.TagFeedResponse;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -24,14 +28,9 @@ public class TagsService extends BaseService {
private static TagsService instance; private static TagsService instance;
private final TagsRepository webRepository;
private final TagsRepository repository; private final TagsRepository repository;
private TagsService() { private TagsService() {
final Retrofit webRetrofit = getRetrofitBuilder()
.baseUrl("https://www.instagram.com/")
.build();
webRepository = webRetrofit.create(TagsRepository.class);
final Retrofit retrofit = getRetrofitBuilder() final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com/") .baseUrl("https://i.instagram.com/")
.build(); .build();
@ -45,11 +44,17 @@ public class TagsService extends BaseService {
return instance; return instance;
} }
public void follow(@NonNull final String ua, public void follow(@NonNull final String tag,
@NonNull final String tag,
@NonNull final String csrfToken, @NonNull final String csrfToken,
@NonNull final long userId,
@NonNull final String deviceUuid,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
final Call<String> request = webRepository.follow(ua, csrfToken, tag); final Map<String, Object> form = new HashMap<>(3);
form.put("_csrftoken", csrfToken);
form.put("_uid", userId);
form.put("_uuid", deviceUuid);
final Map<String, String> signedForm = Utils.sign(form);
final Call<String> request = repository.follow(signedForm, tag);
request.enqueue(new Callback<String>() { request.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) {
@ -75,11 +80,17 @@ public class TagsService extends BaseService {
}); });
} }
public void unfollow(@NonNull final String ua, public void unfollow(@NonNull final String tag,
@NonNull final String tag,
@NonNull final String csrfToken, @NonNull final String csrfToken,
@NonNull final long userId,
@NonNull final String deviceUuid,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
final Call<String> request = webRepository.unfollow(ua, csrfToken, tag); final Map<String, Object> form = new HashMap<>(3);
form.put("_csrftoken", csrfToken);
form.put("_uid", userId);
form.put("_uuid", deviceUuid);
final Map<String, String> signedForm = Utils.sign(form);
final Call<String> request = repository.unfollow(signedForm, tag);
request.enqueue(new Callback<String>() { request.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> 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<FeedModel> items = parseItems(itemsJson);
// return new PostsFetchResponse(
// items,
// moreAvailable,
// nextMaxId
// );
// }
// private List<FeedModel> parseItems(final JSONArray items) throws JSONException {
// if (items == null) {
// return Collections.emptyList();
// }
// final List<FeedModel> 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;
// }
} }