From c57e3057329df21affc7a6877d06cfdf7649742c Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 22 Mar 2021 20:59:08 +0900 Subject: [PATCH 1/4] Create RetrofitFactory to reuse retrofit objects. --- .../instagrabber/InstaGrabberApplication.java | 10 +- .../instagrabber/utils/MediaUploader.java | 2 +- .../instagrabber/webservices/BaseService.java | 42 -------- .../webservices/CollectionService.java | 18 ++-- .../webservices/DirectMessagesService.java | 8 +- .../webservices/DiscoverService.java | 8 +- .../instagrabber/webservices/FeedService.java | 8 +- .../webservices/FriendshipService.java | 16 ++- .../instagrabber/webservices/GifService.java | 8 +- .../webservices/GraphQLService.java | 8 +- .../webservices/LocationService.java | 8 +- .../webservices/MediaService.java | 8 +- .../instagrabber/webservices/NewsService.java | 30 ++---- .../webservices/ProfileService.java | 14 ++- .../webservices/RetrofitFactory.java | 97 +++++++++++++++++++ .../webservices/StoriesService.java | 11 +-- .../instagrabber/webservices/TagsService.java | 8 +- .../instagrabber/webservices/UserService.java | 8 +- .../AddCookiesInterceptor.java | 2 +- .../LoggingInterceptor.java | 2 +- 20 files changed, 170 insertions(+), 146 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java rename app/src/main/java/awais/instagrabber/webservices/{ => interceptors}/AddCookiesInterceptor.java (96%) rename app/src/main/java/awais/instagrabber/webservices/{ => interceptors}/LoggingInterceptor.java (96%) diff --git a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java index b90c8343..de555f94 100644 --- a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java +++ b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java @@ -17,17 +17,19 @@ import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.LocaleUtils; import awais.instagrabber.utils.SettingsHelper; import awais.instagrabber.utils.TextUtils; +import awais.instagrabber.webservices.RetrofitFactory; import awaisomereport.CrashReporter; -//import awaisomereport.LogCollector; import static awais.instagrabber.utils.CookieUtils.NET_COOKIE_MANAGER; import static awais.instagrabber.utils.Utils.applicationHandler; import static awais.instagrabber.utils.Utils.cacheDir; import static awais.instagrabber.utils.Utils.clipboardManager; import static awais.instagrabber.utils.Utils.datetimeParser; -//import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.settingsHelper; +//import awaisomereport.LogCollector; +//import static awais.instagrabber.utils.Utils.logCollector; + public final class InstaGrabberApplication extends Application { private static final String TAG = "InstaGrabberApplication"; @@ -56,7 +58,7 @@ public final class InstaGrabberApplication extends Application { } if (!BuildConfig.DEBUG) CrashReporter.get(this).start(); -// logCollector = new LogCollector(this); + // logCollector = new LogCollector(this); CookieHandler.setDefault(NET_COOKIE_MANAGER); @@ -85,5 +87,7 @@ public final class InstaGrabberApplication extends Application { if (TextUtils.isEmpty(settingsHelper.getString(Constants.DEVICE_UUID))) { settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()); } + + RetrofitFactory.setup(this); } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/MediaUploader.java b/app/src/main/java/awais/instagrabber/utils/MediaUploader.java index f9c47a91..beb5c1af 100644 --- a/app/src/main/java/awais/instagrabber/utils/MediaUploader.java +++ b/app/src/main/java/awais/instagrabber/utils/MediaUploader.java @@ -17,7 +17,7 @@ import java.util.Map; import awais.instagrabber.models.UploadPhotoOptions; import awais.instagrabber.models.UploadVideoOptions; -import awais.instagrabber.webservices.AddCookiesInterceptor; +import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor; import okhttp3.Call; import okhttp3.Headers; import okhttp3.MediaType; diff --git a/app/src/main/java/awais/instagrabber/webservices/BaseService.java b/app/src/main/java/awais/instagrabber/webservices/BaseService.java index e30aa992..a6cdf060 100644 --- a/app/src/main/java/awais/instagrabber/webservices/BaseService.java +++ b/app/src/main/java/awais/instagrabber/webservices/BaseService.java @@ -1,50 +1,8 @@ package awais.instagrabber.webservices; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import java.io.File; - -import awais.instagrabber.BuildConfig; -import awais.instagrabber.repositories.responses.Caption; -import awais.instagrabber.utils.Utils; -import okhttp3.Cache; -import okhttp3.OkHttpClient; -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; -import retrofit2.converter.scalars.ScalarsConverterFactory; - public abstract class BaseService { private static final String TAG = "BaseService"; - private Retrofit.Builder builder; - private final int cacheSize = 10 * 1024 * 1024; // 10 MB - private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize); - - Retrofit.Builder getRetrofitBuilder() { - if (builder == null) { - final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() - .addInterceptor(new AddCookiesInterceptor()) - .followRedirects(false) - .followSslRedirects(false) - .cache(cache); - if (BuildConfig.DEBUG) { - // clientBuilder.addInterceptor(new LoggingInterceptor()); - } - final Gson gson = new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer()) - .setLenient() - .create(); - builder = new Retrofit.Builder() - .addConverterFactory(ScalarsConverterFactory.create()) - .addConverterFactory(GsonConverterFactory.create(gson)) - .client(clientBuilder.build()); - } - return builder; - } - // protected String userBreadcrumb(final int size) { // final long term = (random(2, 4) * 1000) + size + (random(15, 21) * 1000); // final float div = (float) size / random(2, 4); diff --git a/app/src/main/java/awais/instagrabber/webservices/CollectionService.java b/app/src/main/java/awais/instagrabber/webservices/CollectionService.java index faafb6d0..e0c50dc2 100644 --- a/app/src/main/java/awais/instagrabber/webservices/CollectionService.java +++ b/app/src/main/java/awais/instagrabber/webservices/CollectionService.java @@ -1,5 +1,7 @@ package awais.instagrabber.webservices; +import android.text.TextUtils; + import androidx.annotation.NonNull; import java.util.HashMap; @@ -14,7 +16,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class CollectionService extends BaseService { private static final String TAG = "ProfileService"; @@ -31,10 +32,9 @@ public class CollectionService extends BaseService { this.deviceUuid = deviceUuid; this.csrfToken = csrfToken; this.userId = userId; - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(CollectionRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(CollectionRepository.class); } public String getCsrfToken() { @@ -66,10 +66,10 @@ public class CollectionService extends BaseService { form.put("module_name", "feed_saved_add_to_collection"); final List ids; ids = posts.stream() - .map(Media::getPk) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - form.put("added_media_ids", "[" + String.join(",", ids) + "]"); + .map(Media::getPk) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + form.put("added_media_ids", "[" + TextUtils.join(",", ids) + "]"); changeCollection(collectionId, "edit", form, callback); } diff --git a/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java b/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java index 5e4afea5..0688c761 100644 --- a/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java @@ -41,7 +41,6 @@ import awais.instagrabber.repositories.responses.giphy.GiphyGif; import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import retrofit2.Call; -import retrofit2.Retrofit; public class DirectMessagesService extends BaseService { private static final String TAG = "DiscoverService"; @@ -59,10 +58,9 @@ public class DirectMessagesService extends BaseService { this.csrfToken = csrfToken; this.userId = userId; this.deviceUuid = deviceUuid; - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(DirectMessagesRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(DirectMessagesRepository.class); } public String getCsrfToken() { diff --git a/app/src/main/java/awais/instagrabber/webservices/DiscoverService.java b/app/src/main/java/awais/instagrabber/webservices/DiscoverService.java index 294a6a7d..880ae526 100644 --- a/app/src/main/java/awais/instagrabber/webservices/DiscoverService.java +++ b/app/src/main/java/awais/instagrabber/webservices/DiscoverService.java @@ -12,7 +12,6 @@ import awais.instagrabber.utils.TextUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class DiscoverService extends BaseService { @@ -23,10 +22,9 @@ public class DiscoverService extends BaseService { private static DiscoverService instance; private DiscoverService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(DiscoverRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(DiscoverRepository.class); } public static DiscoverService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/FeedService.java b/app/src/main/java/awais/instagrabber/webservices/FeedService.java index 2bd91690..8b1626ed 100644 --- a/app/src/main/java/awais/instagrabber/webservices/FeedService.java +++ b/app/src/main/java/awais/instagrabber/webservices/FeedService.java @@ -22,7 +22,6 @@ import awais.instagrabber.utils.TextUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class FeedService extends BaseService { private static final String TAG = "FeedService"; @@ -32,10 +31,9 @@ public class FeedService extends BaseService { private static FeedService instance; private FeedService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(FeedRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(FeedRepository.class); } public static FeedService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java b/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java index 7c5da8cf..ced49305 100644 --- a/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java +++ b/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java @@ -25,7 +25,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class FriendshipService extends BaseService { private static final String TAG = "FriendshipService"; @@ -42,10 +41,9 @@ public class FriendshipService extends BaseService { this.deviceUuid = deviceUuid; this.csrfToken = csrfToken; this.userId = userId; - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(FriendshipRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(FriendshipRepository.class); } public String getCsrfToken() { @@ -168,8 +166,8 @@ public class FriendshipService extends BaseService { form.put("_uuid", deviceUuid); form.put(story ? "target_reel_author_id" : "target_posts_author_id", String.valueOf(targetUserId)); final Call request = repository.changeMute(unmute ? - "unmute_posts_or_story_from_follow" : - "mute_posts_or_story_from_follow", + "unmute_posts_or_story_from_follow" : + "mute_posts_or_story_from_follow", form); request.enqueue(new Callback() { @Override @@ -198,8 +196,8 @@ public class FriendshipService extends BaseService { if (maxId != null) queryMap.put("max_id", maxId); final Call request = repository.getList( targetUserId, - follower ? "followers" : "following", - queryMap); + follower ? "followers" : "following", + queryMap); request.enqueue(new Callback() { @Override public void onResponse(@NonNull final Call call, @NonNull final Response response) { diff --git a/app/src/main/java/awais/instagrabber/webservices/GifService.java b/app/src/main/java/awais/instagrabber/webservices/GifService.java index 6485efd1..e783f689 100644 --- a/app/src/main/java/awais/instagrabber/webservices/GifService.java +++ b/app/src/main/java/awais/instagrabber/webservices/GifService.java @@ -3,7 +3,6 @@ package awais.instagrabber.webservices; import awais.instagrabber.repositories.GifRepository; import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse; import retrofit2.Call; -import retrofit2.Retrofit; public class GifService extends BaseService { @@ -12,10 +11,9 @@ public class GifService extends BaseService { private static GifService instance; private GifService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(GifRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(GifRepository.class); } public static GifService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java index 4cd1c1b6..e56f5581 100644 --- a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java +++ b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java @@ -28,7 +28,6 @@ import awais.instagrabber.utils.TextUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class GraphQLService extends BaseService { private static final String TAG = "GraphQLService"; @@ -39,10 +38,9 @@ public class GraphQLService extends BaseService { private static GraphQLService instance; private GraphQLService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://www.instagram.com") - .build(); - repository = retrofit.create(GraphQLRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofitWeb() + .create(GraphQLRepository.class); } public static GraphQLService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/LocationService.java b/app/src/main/java/awais/instagrabber/webservices/LocationService.java index 381ba440..2f8036a3 100644 --- a/app/src/main/java/awais/instagrabber/webservices/LocationService.java +++ b/app/src/main/java/awais/instagrabber/webservices/LocationService.java @@ -11,7 +11,6 @@ import awais.instagrabber.utils.TextUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class LocationService extends BaseService { private static final String TAG = "LocationService"; @@ -21,10 +20,9 @@ public class LocationService extends BaseService { private static LocationService instance; private LocationService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(LocationRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(LocationRepository.class); } public static LocationService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/MediaService.java b/app/src/main/java/awais/instagrabber/webservices/MediaService.java index e1990e87..d820b954 100644 --- a/app/src/main/java/awais/instagrabber/webservices/MediaService.java +++ b/app/src/main/java/awais/instagrabber/webservices/MediaService.java @@ -31,7 +31,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class MediaService extends BaseService { private static final String TAG = "MediaService"; @@ -51,10 +50,9 @@ public class MediaService extends BaseService { this.deviceUuid = deviceUuid; this.csrfToken = csrfToken; this.userId = userId; - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(MediaRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(MediaRepository.class); } public String getCsrfToken() { diff --git a/app/src/main/java/awais/instagrabber/webservices/NewsService.java b/app/src/main/java/awais/instagrabber/webservices/NewsService.java index fe54c13d..667fb647 100644 --- a/app/src/main/java/awais/instagrabber/webservices/NewsService.java +++ b/app/src/main/java/awais/instagrabber/webservices/NewsService.java @@ -1,39 +1,27 @@ package awais.instagrabber.webservices; -import android.util.Log; - import androidx.annotation.NonNull; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; -import awais.instagrabber.BuildConfig; -import awais.instagrabber.models.enums.NotificationType; import awais.instagrabber.repositories.NewsRepository; import awais.instagrabber.repositories.responses.AymlResponse; import awais.instagrabber.repositories.responses.AymlUser; -import awais.instagrabber.repositories.responses.NotificationCounts; -import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.repositories.responses.NewsInboxResponse; import awais.instagrabber.repositories.responses.Notification; import awais.instagrabber.repositories.responses.NotificationArgs; -import awais.instagrabber.repositories.responses.NotificationImage; +import awais.instagrabber.repositories.responses.NotificationCounts; import awais.instagrabber.repositories.responses.User; +import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class NewsService extends BaseService { private static final String TAG = "NewsService"; @@ -43,10 +31,9 @@ public class NewsService extends BaseService { private static NewsService instance; private NewsService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(NewsRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(NewsRepository.class); } public static NewsService getInstance() { @@ -131,7 +118,8 @@ public class NewsService extends BaseService { aymlUsers.addAll(oldSuggestions); } - final List newsItems = aymlUsers.stream() + final List newsItems = aymlUsers + .stream() .map(i -> { final User u = i.getUser(); return new Notification( @@ -173,7 +161,9 @@ public class NewsService extends BaseService { return; } - final List newsItems = body.getUsers().stream() + final List newsItems = body + .getUsers() + .stream() .map(u -> { return new Notification( new NotificationArgs( diff --git a/app/src/main/java/awais/instagrabber/webservices/ProfileService.java b/app/src/main/java/awais/instagrabber/webservices/ProfileService.java index 65f787a6..cc8199fd 100644 --- a/app/src/main/java/awais/instagrabber/webservices/ProfileService.java +++ b/app/src/main/java/awais/instagrabber/webservices/ProfileService.java @@ -23,7 +23,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class ProfileService extends BaseService { private static final String TAG = "ProfileService"; @@ -33,10 +32,9 @@ public class ProfileService extends BaseService { private static ProfileService instance; private ProfileService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(ProfileRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(ProfileRepository.class); } public static ProfileService getInstance() { @@ -104,9 +102,9 @@ public class ProfileService extends BaseService { posts = Collections.emptyList(); } else { posts = items.stream() - .map(WrappedMedia::getMedia) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + .map(WrappedMedia::getMedia) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } callback.onSuccess(new PostsFetchResponse( posts, diff --git a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java new file mode 100644 index 00000000..ce8cbfa7 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java @@ -0,0 +1,97 @@ +package awais.instagrabber.webservices; + +import android.app.Application; + +import androidx.annotation.NonNull; + +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.File; + +import awais.instagrabber.BuildConfig; +import awais.instagrabber.repositories.responses.Caption; +import awais.instagrabber.utils.Utils; +import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor; +import okhttp3.Cache; +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; +import retrofit2.converter.scalars.ScalarsConverterFactory; + +public final class RetrofitFactory { + private static final Object LOCK = new Object(); + + private static RetrofitFactory instance; + + private final Application application; + private final int cacheSize = 10 * 1024 * 1024; // 10 MB + private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize); + + private Retrofit.Builder builder; + private Retrofit retrofit; + private Retrofit retrofitWeb; + + public static void setup(@NonNull final Application application) { + if (instance == null) { + synchronized (LOCK) { + if (instance == null) { + instance = new RetrofitFactory(application); + } + } + } + } + + public static RetrofitFactory getInstance() { + if (instance == null) { + throw new RuntimeException("Setup not done!"); + } + return instance; + } + + private RetrofitFactory(@NonNull final Application application) { + this.application = application; + } + + private Retrofit.Builder getRetrofitBuilder() { + if (builder == null) { + final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() + .addInterceptor(new AddCookiesInterceptor()) + .followRedirects(false) + .followSslRedirects(false) + .cache(cache); + if (BuildConfig.DEBUG) { + // clientBuilder.addInterceptor(new LoggingInterceptor()); + } + final Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer()) + .setLenient() + .create(); + builder = new Retrofit.Builder() + .addConverterFactory(ScalarsConverterFactory.create()) + .addConverterFactory(GsonConverterFactory.create(gson)) + .client(clientBuilder.build()); + } + return builder; + } + + public Retrofit getRetrofit() { + if (retrofit == null) { + retrofit = getRetrofitBuilder() + .baseUrl("https://i.instagram.com") + .build(); + } + return retrofit; + } + + public Retrofit getRetrofitWeb() { + if (retrofitWeb == null) { + retrofitWeb = getRetrofitBuilder() + .baseUrl("https://www.instagram.com") + .build(); + } + return retrofitWeb; + } +} diff --git a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java index fd05af17..cc19b086 100644 --- a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java @@ -32,7 +32,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class StoriesService extends BaseService { private static final String TAG = "StoriesService"; @@ -50,10 +49,9 @@ public class StoriesService extends BaseService { this.csrfToken = csrfToken; this.userId = userId; this.deviceUuid = deviceUuid; - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(StoriesRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(StoriesRepository.class); } public String getCsrfToken() { @@ -190,8 +188,7 @@ public class StoriesService extends BaseService { firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, false, null); } feedStoryModels.add(new FeedStoryModel(id, user, fullyRead, timestamp, firstStoryModel, mediaCount, false, isBestie)); - } - catch (Exception e) {} // to cover promotional reels with non-long user pk's + } catch (Exception e) {} // to cover promotional reels with non-long user pk's } final JSONArray broadcasts = new JSONObject(body).getJSONArray("broadcasts"); for (int i = 0; i < broadcasts.length(); ++i) { diff --git a/app/src/main/java/awais/instagrabber/webservices/TagsService.java b/app/src/main/java/awais/instagrabber/webservices/TagsService.java index 615d72af..276f0dc7 100644 --- a/app/src/main/java/awais/instagrabber/webservices/TagsService.java +++ b/app/src/main/java/awais/instagrabber/webservices/TagsService.java @@ -21,7 +21,6 @@ import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class TagsService extends BaseService { @@ -32,10 +31,9 @@ public class TagsService extends BaseService { private final TagsRepository repository; private TagsService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com/") - .build(); - repository = retrofit.create(TagsRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(TagsRepository.class); } public static TagsService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/UserService.java b/app/src/main/java/awais/instagrabber/webservices/UserService.java index 52bb064d..1266662f 100644 --- a/app/src/main/java/awais/instagrabber/webservices/UserService.java +++ b/app/src/main/java/awais/instagrabber/webservices/UserService.java @@ -12,7 +12,6 @@ import awais.instagrabber.repositories.responses.WrappedUser; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -import retrofit2.Retrofit; public class UserService extends BaseService { private static final String TAG = UserService.class.getSimpleName(); @@ -22,10 +21,9 @@ public class UserService extends BaseService { private static UserService instance; private UserService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://i.instagram.com") - .build(); - repository = retrofit.create(UserRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofit() + .create(UserRepository.class); } public static UserService getInstance() { diff --git a/app/src/main/java/awais/instagrabber/webservices/AddCookiesInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java similarity index 96% rename from app/src/main/java/awais/instagrabber/webservices/AddCookiesInterceptor.java rename to app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java index 3b5f709f..610b5f89 100644 --- a/app/src/main/java/awais/instagrabber/webservices/AddCookiesInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/AddCookiesInterceptor.java @@ -1,4 +1,4 @@ -package awais.instagrabber.webservices; +package awais.instagrabber.webservices.interceptors; import androidx.annotation.NonNull; diff --git a/app/src/main/java/awais/instagrabber/webservices/LoggingInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.java similarity index 96% rename from app/src/main/java/awais/instagrabber/webservices/LoggingInterceptor.java rename to app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.java index c9358a72..e7bedd3b 100644 --- a/app/src/main/java/awais/instagrabber/webservices/LoggingInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/LoggingInterceptor.java @@ -1,4 +1,4 @@ -package awais.instagrabber.webservices; +package awais.instagrabber.webservices.interceptors; import android.util.Log; From 91e13a23ad288243a17ca59776063cbd5212d996 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 22 Mar 2021 22:02:48 +0900 Subject: [PATCH 2/4] Add ig error interceptor --- .../instagrabber/InstaGrabberApplication.java | 3 - .../instagrabber/activities/MainActivity.java | 2 + .../awais/instagrabber/utils/Constants.java | 1 + .../webservices/RetrofitFactory.java | 17 +-- .../interceptors/IgErrorsInterceptor.java | 108 ++++++++++++++++++ app/src/main/res/values/strings.xml | 6 + 6 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java diff --git a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java index de555f94..36807a6c 100644 --- a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java +++ b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.java @@ -17,7 +17,6 @@ import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.LocaleUtils; import awais.instagrabber.utils.SettingsHelper; import awais.instagrabber.utils.TextUtils; -import awais.instagrabber.webservices.RetrofitFactory; import awaisomereport.CrashReporter; import static awais.instagrabber.utils.CookieUtils.NET_COOKIE_MANAGER; @@ -87,7 +86,5 @@ public final class InstaGrabberApplication extends Application { if (TextUtils.isEmpty(settingsHelper.getString(Constants.DEVICE_UUID))) { settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()); } - - RetrofitFactory.setup(this); } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index df2369cc..66670534 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -83,6 +83,7 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; +import awais.instagrabber.webservices.RetrofitFactory; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -138,6 +139,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + RetrofitFactory.setup(this); binding = ActivityMainBinding.inflate(getLayoutInflater()); final String cookie = settingsHelper.getString(Constants.COOKIE); CookieUtils.setupCookies(cookie); diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index 0c92f0fd..4c064e81 100644 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -111,6 +111,7 @@ public final class Constants { public static final int SHOW_ACTIVITY_REQUEST_CODE = 1738; public static final int SHOW_DM_THREAD = 2000; public static final int DM_SYNC_SERVICE_REQUEST_CODE = 3000; + public static final int GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE = 7777; public static final String ACTION_SHOW_ACTIVITY = "show_activity"; public static final String ACTION_SHOW_DM_THREAD = "show_dm_thread"; diff --git a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java index ce8cbfa7..71c78a4e 100644 --- a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java +++ b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java @@ -1,7 +1,5 @@ package awais.instagrabber.webservices; -import android.app.Application; - import androidx.annotation.NonNull; import com.google.gson.FieldNamingPolicy; @@ -11,9 +9,11 @@ import com.google.gson.GsonBuilder; import java.io.File; import awais.instagrabber.BuildConfig; +import awais.instagrabber.activities.MainActivity; import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.utils.Utils; import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor; +import awais.instagrabber.webservices.interceptors.IgErrorsInterceptor; import okhttp3.Cache; import okhttp3.OkHttpClient; import retrofit2.Retrofit; @@ -25,7 +25,7 @@ public final class RetrofitFactory { private static RetrofitFactory instance; - private final Application application; + private final MainActivity mainActivity; private final int cacheSize = 10 * 1024 * 1024; // 10 MB private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize); @@ -33,11 +33,11 @@ public final class RetrofitFactory { private Retrofit retrofit; private Retrofit retrofitWeb; - public static void setup(@NonNull final Application application) { + public static void setup(@NonNull final MainActivity mainActivity) { if (instance == null) { synchronized (LOCK) { if (instance == null) { - instance = new RetrofitFactory(application); + instance = new RetrofitFactory(mainActivity); } } } @@ -50,20 +50,21 @@ public final class RetrofitFactory { return instance; } - private RetrofitFactory(@NonNull final Application application) { - this.application = application; + private RetrofitFactory(@NonNull final MainActivity mainActivity) { + this.mainActivity = mainActivity; } private Retrofit.Builder getRetrofitBuilder() { if (builder == null) { final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() - .addInterceptor(new AddCookiesInterceptor()) .followRedirects(false) .followSslRedirects(false) .cache(cache); if (BuildConfig.DEBUG) { // clientBuilder.addInterceptor(new LoggingInterceptor()); } + clientBuilder.addInterceptor(new AddCookiesInterceptor()) + .addInterceptor(new IgErrorsInterceptor(mainActivity)); final Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer()) diff --git a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java new file mode 100644 index 00000000..413f0e03 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -0,0 +1,108 @@ +package awais.instagrabber.webservices.interceptors; + +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.StringRes; + +import org.json.JSONObject; + +import java.io.IOException; + +import awais.instagrabber.R; +import awais.instagrabber.activities.MainActivity; +import awais.instagrabber.dialogs.ConfirmDialogFragment; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.TextUtils; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +public class IgErrorsInterceptor implements Interceptor { + private static final String TAG = IgErrorsInterceptor.class.getSimpleName(); + + private final MainActivity mainActivity; + + public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) { + this.mainActivity = mainActivity; + } + + @NonNull + @Override + public Response intercept(@NonNull final Chain chain) throws IOException { + final Request request = chain.request(); + final Response response = chain.proceed(request); + if (response.isSuccessful()) { + return response; + } + checkError(response); + return response; + } + + private void checkError(@NonNull final Response response) { + final int errorCode = response.code(); + switch (errorCode) { + case 429: // "429 Too Many Requests" + // ('Throttled by Instagram because of too many API requests.'); + showErrorDialog(R.string.throttle_error); + return; + case 431: // "431 Request Header Fields Too Large" + // show dialog? + Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process.")); + return; + } + final ResponseBody body = response.body(); + if (body == null) return; + try { + final String bodyString = body.string(); + final JSONObject jsonObject = new JSONObject(bodyString); + String message = jsonObject.optString("message", null); + if (!TextUtils.isEmpty(message)) { + message = message.toLowerCase(); + switch (message) { + case "user_has_logged_out": + showErrorDialog(R.string.account_logged_out); + return; + case "login_required": + showErrorDialog(R.string.login_required); + return; + case "not authorized to view user": // Do we handle this in profile view fragment? + case "challenge_required": // Since we make users login using browser, we should not be getting this error in api requests + default: + Log.e(TAG, "checkError: " + bodyString); + return; + } + } + final String errorType = jsonObject.optString("error_type", null); + if (TextUtils.isEmpty(errorType)) return; + if (errorType.equals("sentry_block")) { + showErrorDialog(R.string.sentry_block); + return; + } + if (errorType.equals("inactive user")) { + showErrorDialog(R.string.inactive_user); + } + } catch (Exception e) { + Log.e(TAG, "checkError: ", e); + } + } + + @NonNull + private String getMessage(final int errorCode, final String message) { + return String.format("code: %s, internalMessage: %s", errorCode, message); + } + + private void showErrorDialog(@StringRes final int messageResId) { + if (messageResId == 0) return; + final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( + Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE, + R.string.error, + messageResId, + R.string.ok, + 0, + 0 + ); + dialogFragment.show(mainActivity.getSupportFragmentManager(), "network_error_dialog"); + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f65ff8c9..af62d980 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -473,4 +473,10 @@ Removed keyword: %s from filter list Marked as seen Delete unsuccessful + Throttled by Instagram because of too many API requests. Wait for some time before retrying. + Error + This account has been logged out. + Login required! + Sentry block. + User is inactive! From 0d53e244e21aca6b4ee1e4adb5936244d3d15ff1 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 22 Mar 2021 23:46:47 +0900 Subject: [PATCH 3/4] Destroy references to solve leak --- .../instagrabber/activities/MainActivity.java | 1 + .../webservices/RetrofitFactory.java | 18 ++++++++++++++++-- .../interceptors/IgErrorsInterceptor.java | 7 ++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 66670534..8f16cd5d 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -248,6 +248,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage Log.e(TAG, "onDestroy: ", e); } unbindActivityCheckerService(); + RetrofitFactory.getInstance().destroy(); } @Override diff --git a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java index 71c78a4e..5062e5c7 100644 --- a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java +++ b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java @@ -25,10 +25,11 @@ public final class RetrofitFactory { private static RetrofitFactory instance; - private final MainActivity mainActivity; private final int cacheSize = 10 * 1024 * 1024; // 10 MB private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize); + private IgErrorsInterceptor igErrorsInterceptor; + private MainActivity mainActivity; private Retrofit.Builder builder; private Retrofit retrofit; private Retrofit retrofitWeb; @@ -56,6 +57,7 @@ public final class RetrofitFactory { private Retrofit.Builder getRetrofitBuilder() { if (builder == null) { + igErrorsInterceptor = new IgErrorsInterceptor(mainActivity); final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .followRedirects(false) .followSslRedirects(false) @@ -64,7 +66,7 @@ public final class RetrofitFactory { // clientBuilder.addInterceptor(new LoggingInterceptor()); } clientBuilder.addInterceptor(new AddCookiesInterceptor()) - .addInterceptor(new IgErrorsInterceptor(mainActivity)); + .addInterceptor(igErrorsInterceptor); final Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer()) @@ -95,4 +97,16 @@ public final class RetrofitFactory { } return retrofitWeb; } + + public void destroy() { + if (igErrorsInterceptor != null) { + igErrorsInterceptor.destroy(); + } + igErrorsInterceptor = null; + mainActivity = null; + retrofit = null; + retrofitWeb = null; + builder = null; + instance = null; + } } 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 413f0e03..7d424a2d 100644 --- a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -22,7 +22,7 @@ import okhttp3.ResponseBody; public class IgErrorsInterceptor implements Interceptor { private static final String TAG = IgErrorsInterceptor.class.getSimpleName(); - private final MainActivity mainActivity; + private MainActivity mainActivity; public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) { this.mainActivity = mainActivity; @@ -94,6 +94,7 @@ public class IgErrorsInterceptor implements Interceptor { } private void showErrorDialog(@StringRes final int messageResId) { + if (mainActivity == null) return; if (messageResId == 0) return; final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE, @@ -105,4 +106,8 @@ public class IgErrorsInterceptor implements Interceptor { ); dialogFragment.show(mainActivity.getSupportFragmentManager(), "network_error_dialog"); } + + public void destroy() { + mainActivity = null; + } } \ No newline at end of file From 07f41a8a8c524e4009ea1be8f7537f86573b6f80 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 30 Mar 2021 20:44:01 +0900 Subject: [PATCH 4/4] Add GraphQl error handling --- .../instagrabber/activities/MainActivity.java | 8 +++++-- .../instagrabber/webservices/NewsService.java | 8 ++----- .../webservices/SearchService.java | 19 +++++---------- .../interceptors/IgErrorsInterceptor.java | 23 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 2da9cb24..95c801ef 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -142,8 +142,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); RetrofitFactory.setup(this); + super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); final String cookie = settingsHelper.getString(Constants.COOKIE); CookieUtils.setupCookies(cookie); @@ -253,7 +253,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage Log.e(TAG, "onDestroy: ", e); } unbindActivityCheckerService(); - RetrofitFactory.getInstance().destroy(); + RetrofitFactory.getInstance().destroy(); } @Override @@ -853,4 +853,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage public Toolbar getToolbar() { return binding.toolbar; } + + public View getRootView() { + return binding.getRoot(); + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/webservices/NewsService.java b/app/src/main/java/awais/instagrabber/webservices/NewsService.java index a7664a8a..15b34def 100644 --- a/app/src/main/java/awais/instagrabber/webservices/NewsService.java +++ b/app/src/main/java/awais/instagrabber/webservices/NewsService.java @@ -13,15 +13,11 @@ import awais.instagrabber.repositories.NewsRepository; import awais.instagrabber.repositories.responses.AymlResponse; import awais.instagrabber.repositories.responses.AymlUser; import awais.instagrabber.repositories.responses.NewsInboxResponse; -import awais.instagrabber.repositories.responses.Notification; -import awais.instagrabber.repositories.responses.NotificationArgs; -import awais.instagrabber.repositories.responses.NotificationCounts; -import awais.instagrabber.repositories.responses.NewsInboxResponse; +import awais.instagrabber.repositories.responses.User; +import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.repositories.responses.notification.Notification; import awais.instagrabber.repositories.responses.notification.NotificationArgs; import awais.instagrabber.repositories.responses.notification.NotificationCounts; -import awais.instagrabber.repositories.responses.User; -import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.utils.Constants; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/awais/instagrabber/webservices/SearchService.java b/app/src/main/java/awais/instagrabber/webservices/SearchService.java index 39b5bd65..4144f85d 100644 --- a/app/src/main/java/awais/instagrabber/webservices/SearchService.java +++ b/app/src/main/java/awais/instagrabber/webservices/SearchService.java @@ -1,16 +1,10 @@ package awais.instagrabber.webservices; -import androidx.annotation.NonNull; - import com.google.common.collect.ImmutableMap; import awais.instagrabber.repositories.SearchRepository; import awais.instagrabber.repositories.responses.search.SearchResponse; -import awais.instagrabber.utils.TextUtils; import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; public class SearchService extends BaseService { private static final String TAG = "LocationService"; @@ -20,10 +14,9 @@ public class SearchService extends BaseService { private static SearchService instance; private SearchService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://www.instagram.com") - .build(); - repository = retrofit.create(SearchRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofitWeb() + .create(SearchRepository.class); } public static SearchService getInstance() { @@ -43,8 +36,8 @@ public class SearchService extends BaseService { builder.put("context", context); builder.put("count", "50"); return repository.search(isLoggedIn - ? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/" - : "https://www.instagram.com/web/search/topsearch/", - builder.build()); + ? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/" + : "https://www.instagram.com/web/search/topsearch/", + builder.build()); } } 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 7d424a2d..e27eeae2 100644 --- a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -1,10 +1,13 @@ package awais.instagrabber.webservices.interceptors; import android.util.Log; +import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.StringRes; +import com.google.android.material.snackbar.Snackbar; + import org.json.JSONObject; import java.io.IOException; @@ -51,6 +54,16 @@ public class IgErrorsInterceptor implements Interceptor { // show dialog? Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process.")); return; + case 404: + showErrorDialog(R.string.not_found); + return; + case 302: // redirect + final String location = response.header("location"); + if (location.equals("https://www.instagram.com/accounts/login/")) { + // rate limited + showErrorDialog(R.string.rate_limit); + } + return; } final ResponseBody body = response.body(); if (body == null) return; @@ -67,9 +80,13 @@ public class IgErrorsInterceptor implements Interceptor { case "login_required": showErrorDialog(R.string.login_required); return; + case "execution failure": + showSnackbar(message); + return; case "not authorized to view user": // Do we handle this in profile view fragment? case "challenge_required": // Since we make users login using browser, we should not be getting this error in api requests default: + showSnackbar(message); Log.e(TAG, "checkError: " + bodyString); return; } @@ -88,6 +105,12 @@ public class IgErrorsInterceptor implements Interceptor { } } + private void showSnackbar(final String message) { + final View view = mainActivity.getRootView(); + if (view == null) return; + Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); + } + @NonNull private String getMessage(final int errorCode, final String message) { return String.format("code: %s, internalMessage: %s", errorCode, message); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce22f77f..86b2d1ac 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -481,4 +481,6 @@ User is inactive! Barinsta Crash Report Select an email app to send crash logs + Not found! + Your IP has been rate limited by Instagram. Wait for an hour and try again.