From 3baa82090e553422d0db0fc420310599f8e2607d Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 26 Jan 2021 21:21:48 -0500 Subject: [PATCH 1/4] restore hd avatar --- .../instagrabber/asyncs/CommentsFetcher.java | 6 +++--- .../dialogs/ProfilePicDialogFragment.java | 2 +- .../responses/HdProfilePicUrlInfo.java | 16 ++++++++++++++++ .../repositories/responses/User.java | 9 ++++++++- .../instagrabber/utils/ResponseBodyUtils.java | 2 +- .../instagrabber/webservices/GraphQLService.java | 2 ++ .../instagrabber/webservices/StoriesService.java | 2 ++ 7 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/repositories/responses/HdProfilePicUrlInfo.java diff --git a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java index 3d1516e6..6b9929c5 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java @@ -115,7 +115,7 @@ public final class CommentsFetcher extends AsyncTask Date: Sun, 7 Feb 2021 17:45:16 -0500 Subject: [PATCH 2/4] #512, #575, #583, #656, #665 --- app/build.gradle | 2 +- .../instagrabber/asyncs/ProfileFetcher.java | 10 ++- .../dialogs/ProfilePicDialogFragment.java | 5 +- .../fragments/CollectionPostsFragment.java | 10 ++- .../fragments/CommentsViewerFragment.java | 20 ++++- .../fragments/FollowViewerFragment.java | 86 ++++--------------- .../fragments/HashTagFragment.java | 3 +- .../fragments/LocationFragment.java | 3 +- .../fragments/main/ProfileFragment.java | 30 +++++-- .../settings/MorePreferencesFragment.java | 7 +- .../instagrabber/webservices/UserService.java | 2 +- app/src/main/res/values/strings.xml | 5 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 14 files changed, 88 insertions(+), 101 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8083b4e2..7db7b24c 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -62,7 +62,7 @@ dependencies { def nav_version = '2.3.2' def exoplayer_version = '2.12.0' - implementation 'com.google.android.material:material:1.3.0-beta01' + implementation 'com.google.android.material:material:1.3.0' implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version" implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version" diff --git a/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java index 46559dfb..5da3b5aa 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java @@ -15,7 +15,7 @@ import awais.instagrabber.webservices.GraphQLService; import awais.instagrabber.webservices.ServiceCallback; import awais.instagrabber.webservices.UserService; -public final class ProfileFetcher extends AsyncTask { +public final class ProfileFetcher extends AsyncTask { private static final String TAG = ProfileFetcher.class.getSimpleName(); private final UserService userService; private final GraphQLService graphQLService; @@ -36,12 +36,11 @@ public final class ProfileFetcher extends AsyncTask { @Nullable @Override - protected String doInBackground(final Void... voids) { + protected Void doInBackground(final Void... voids) { if (isLoggedIn) { userService.getUsernameInfo(userName, new ServiceCallback() { @Override public void onSuccess(final User user) { - Log.d("austin_debug", user.getUsername() + " " + userName); userService.getUserFriendship(user.getPk(), new ServiceCallback() { @Override public void onSuccess(final FriendshipStatus status) { @@ -52,6 +51,7 @@ public final class ProfileFetcher extends AsyncTask { @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error", t); + fetchListener.onFailure(t); } }); } @@ -59,6 +59,7 @@ public final class ProfileFetcher extends AsyncTask { @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error", t); + fetchListener.onFailure(t); } }); } @@ -72,10 +73,11 @@ public final class ProfileFetcher extends AsyncTask { @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error", t); + fetchListener.onFailure(t); } }); } - return "yeah"; + return null; } @Override diff --git a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java index dbc7d223..09862f49 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java @@ -127,7 +127,10 @@ public class ProfilePicDialogFragment extends DialogFragment { @Override public void onFailure(final Throwable t) { final Context context = getContext(); - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} getDialog().dismiss(); } }); diff --git a/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java b/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java index ee154b63..bfedd70e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/CollectionPostsFragment.java @@ -307,7 +307,10 @@ public class CollectionPostsFragment extends Fragment implements SwipeRefreshLay @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error deleting collection", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); }) @@ -334,7 +337,10 @@ public class CollectionPostsFragment extends Fragment implements SwipeRefreshLay @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error editing collection", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); }) diff --git a/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java index 18cc9762..d40f8608 100644 --- a/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java @@ -371,7 +371,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error liking comment", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); return; @@ -389,7 +392,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error unliking comment", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); break; @@ -411,7 +417,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error translating comment", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); break; @@ -432,7 +441,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl @Override public void onFailure(final Throwable t) { Log.e(TAG, "Error deleting comment", t); - Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + try { + Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} } }); break; diff --git a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java index 115f5849..8112d497 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java @@ -44,7 +44,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh private final ArrayList followersModels = new ArrayList<>(); private final ArrayList allFollowing = new ArrayList<>(); - private boolean moreAvailable = true, isFollowersList, isCompare = false, loading = false, shouldRefresh = true; + private boolean moreAvailable = true, isFollowersList, isCompare = false, loading = false, shouldRefresh = true, searching = false; private long profileId; private String username; private String namePost; @@ -65,7 +65,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh final ServiceCallback followingFetchCb = new ServiceCallback() { @Override public void onSuccess(final FriendshipListFetchResponse result) { - if (result != null) { + if (result != null && isCompare) { followingModels.addAll(result.getItems()); if (!isFollowersList) followModels.addAll(result.getItems()); if (result.isMoreAvailable()) { @@ -78,7 +78,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh if (!isFollowersList) moreAvailable = false; showCompare(); } - } else binding.swipeRefreshLayout.setRefreshing(false); + } else if (isCompare) binding.swipeRefreshLayout.setRefreshing(false); } @Override @@ -94,7 +94,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh final ServiceCallback followersFetchCb = new ServiceCallback() { @Override public void onSuccess(final FriendshipListFetchResponse result) { - if (result != null) { + if (result != null && isCompare) { followersModels.addAll(result.getItems()); if (isFollowersList) followModels.addAll(result.getItems()); if (result.isMoreAvailable()) { @@ -107,7 +107,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh if (isFollowersList) moreAvailable = false; showCompare(); } - } + } else if (isCompare) binding.swipeRefreshLayout.setRefreshing(false); } @Override @@ -237,12 +237,12 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh }; layoutManager = new LinearLayoutManager(getContext()); lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> { - if (!TextUtils.isEmpty(endCursor)) { + if (!TextUtils.isEmpty(endCursor) && !searching) { binding.swipeRefreshLayout.setRefreshing(true); layoutManager.setStackFromEnd(true); friendshipService.getList(isFollowersList, profileId, endCursor, cb); + endCursor = null; } - endCursor = null; }); binding.rvFollow.addOnScrollListener(lazyLoader); binding.rvFollow.setLayoutManager(layoutManager); @@ -303,68 +303,6 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh final SearchView searchView = (SearchView) menuSearch.getActionView(); searchView.setQueryHint(getResources().getString(R.string.action_search)); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { - // private final Filter filter = new Filter() { - // private final ArrayList searchFollowModels = new ArrayList<>(followModels.size() / 2); - // private final ArrayList searchFollowingModels = new ArrayList<>(followingModels.size() / 2); - // private final ArrayList searchFollowersModels = new ArrayList<>(followersModels.size() / 2); - // private final ArrayList searchAllFollowing = new ArrayList<>(allFollowing.size() / 2); - // - // @Nullable - // @Override - // protected FilterResults performFiltering(@NonNull final CharSequence constraint) { - // searchFollowModels.clear(); - // searchFollowingModels.clear(); - // searchFollowersModels.clear(); - // searchAllFollowing.clear(); - // - // final int followModelsSize = followModels.size(); - // final int followingModelsSize = followingModels.size(); - // final int followersModelsSize = followersModels.size(); - // final int allFollowingSize = allFollowing.size(); - // - // int maxSize = followModelsSize; - // if (maxSize < followingModelsSize) maxSize = followingModelsSize; - // if (maxSize < followersModelsSize) maxSize = followersModelsSize; - // if (maxSize < allFollowingSize) maxSize = allFollowingSize; - // - // final String query = constraint.toString().toLowerCase(); - // FollowModel followModel; - // while (maxSize != -1) { - // if (maxSize < followModelsSize) { - // followModel = followModels.get(maxSize); - // if (Utils.hasKey(query, followModel.getUsername(), followModel.getFullName())) - // searchFollowModels.add(followModel); - // } - // - // if (maxSize < followingModelsSize) { - // followModel = followingModels.get(maxSize); - // if (Utils.hasKey(query, followModel.getUsername(), followModel.getFullName())) - // searchFollowingModels.add(followModel); - // } - // - // if (maxSize < followersModelsSize) { - // followModel = followersModels.get(maxSize); - // if (Utils.hasKey(query, followModel.getUsername(), followModel.getFullName())) - // searchFollowersModels.add(followModel); - // } - // - // if (maxSize < allFollowingSize) { - // followModel = allFollowing.get(maxSize); - // if (Utils.hasKey(query, followModel.getUsername(), followModel.getFullName())) - // searchAllFollowing.add(followModel); - // } - // - // --maxSize; - // } - // - // return null; - // } - // - // @Override - // protected void publishResults(final CharSequence query, final FilterResults results) { - // refreshAdapter(searchFollowModels, searchFollowingModels, searchFollowersModels, searchAllFollowing); - // } - // }; @Override public boolean onQueryTextSubmit(final String query) { @@ -373,9 +311,15 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public boolean onQueryTextChange(final String query) { - // if (Utils.isEmpty(query)) refreshAdapter(followModels, followingModels, followersModels, allFollowing); + if (TextUtils.isEmpty(query)) { + searching = false; + // refreshAdapter(followModels, followingModels, followersModels, allFollowing); + } // else filter.filter(query.toLowerCase()); - if (adapter != null) adapter.getFilter().filter(query); + if (adapter != null) { + searching = true; + adapter.getFilter().filter(query); + } return true; } }); diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index ce3b1cf9..845da1c6 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -376,7 +376,8 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe final Context context = getContext(); if (context == null) return; if (hashtagModel == null) { - Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, R.string.error_loading_hashtag, Toast.LENGTH_SHORT).show(); + binding.swipeRefreshLayout.setEnabled(false); return; } setTitle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index 67b7e5d7..49e23159 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -385,7 +385,8 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR if (locationModel == null) { final Context context = getContext(); if (context == null) return; - Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, R.string.error_loading_location, Toast.LENGTH_SHORT).show(); + binding.swipeRefreshLayout.setEnabled(false); return; } setTitle(); diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index d0567bfb..55e52f1b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -538,10 +538,26 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private void fetchProfileDetails() { if (TextUtils.isEmpty(username)) return; - new ProfileFetcher(username.trim().substring(1), isLoggedIn, profileModel -> { - if (getContext() == null) return; - this.profileModel = profileModel; - setProfileDetails(); + new ProfileFetcher(username.trim().substring(1), isLoggedIn, new FetchListener() { + @Override + public void onResult(final User user) { + if (getContext() == null) return; + profileModel = user; + setProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error fetching profile", t); + final Context context = getContext(); + try { + if (t == null) Toast.makeText(context, + isLoggedIn ? R.string.error_loading_profile_loggedin : R.string.error_loading_profile, + Toast.LENGTH_LONG).show(); + else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } + catch(final Throwable e) {} + } }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -902,7 +918,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe @Override public void onSuccess(final FriendshipChangeResponse result) { // Log.d(TAG, "Unfollow success: " + result); - onRefresh(); + fetchProfileDetails(); } @Override @@ -920,7 +936,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe @Override public void onSuccess(final FriendshipChangeResponse result) { // Log.d(TAG, "Unfollow success: " + result); - onRefresh(); + fetchProfileDetails(); } @Override @@ -935,7 +951,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe @Override public void onSuccess(final FriendshipChangeResponse result) { // Log.d(TAG, "Follow success: " + result); - onRefresh(); + fetchProfileDetails(); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java index ad4d401b..55088c8b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java @@ -64,7 +64,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { screen.addPreference(accountCategory); if (isLoggedIn) { accountCategory.setSummary(R.string.account_hint); - accountCategory.addPreference(getAccountSwitcherPreference(cookie)); + accountCategory.addPreference(getAccountSwitcherPreference(cookie, context)); accountCategory.addPreference(getPreference(R.string.logout, R.string.logout_summary, R.drawable.ic_logout_24, preference -> { if (getContext() == null) return false; CookieUtils.setupCookies("LOGOUT"); @@ -79,7 +79,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { public void onSuccess(@NonNull final List accounts) { if (!isLoggedIn) { if (accounts.size() > 0) { - accountCategory.addPreference(getAccountSwitcherPreference(null)); + accountCategory.addPreference(getAccountSwitcherPreference(null, context)); } // Need to show something to trigger login activity accountCategory.addPreference(getPreference(R.string.add_account, R.drawable.ic_add, preference -> { @@ -235,8 +235,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment { } } - private AccountSwitcherPreference getAccountSwitcherPreference(final String cookie) { - final Context context = getContext(); + private AccountSwitcherPreference getAccountSwitcherPreference(final String cookie, final Context context) { if (context == null) return null; return new AccountSwitcherPreference(context, cookie, accountRepository, v -> showAccountSwitcherDialog()); } diff --git a/app/src/main/java/awais/instagrabber/webservices/UserService.java b/app/src/main/java/awais/instagrabber/webservices/UserService.java index 0aff3f32..52bb064d 100644 --- a/app/src/main/java/awais/instagrabber/webservices/UserService.java +++ b/app/src/main/java/awais/instagrabber/webservices/UserService.java @@ -62,7 +62,7 @@ public class UserService extends BaseService { public void onResponse(@NonNull final Call call, @NonNull final Response response) { final WrappedUser user = response.body(); if (user == null) { - callback.onSuccess(null); + callback.onFailure(null); return; } callback.onSuccess(user.getUser()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4f007c79..4c1c4078 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -36,7 +36,10 @@ Other members will know you viewed it Enable activity notifications Feed stories sort - Error loading profile!\nTry logging in and search again. + Error loading profile! Is the username valid? If so, you may be ratelimited. + Error loading profile! Is the username valid? Or did they block you? + Error loading hashtag! Is the name valid? + Error loading hashtag! Is the URL valid? Error creating Download folder(s). Save to custom folder Select folder diff --git a/build.gradle b/build.gradle index cdffced1..1c106f22 100755 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.1.2' def nav_version = "2.3.2" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa54c83c..6b0a820c 100755 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionSha256Sum=0080de8491f0918e4f529a6db6820fa0b9e818ee2386117f4394f95feb1d5583 -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip +distributionSha256Sum=1433372d903ffba27496f8d5af24265310d2da0d78bf6b4e5138831d4fe066e9 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip # https://gradle.org/releases/ # https://gradle.org/release-checksums/ zipStoreBase=GRADLE_USER_HOME From 06a6b07a53fbfc70ff04bfa4c89f3b143e4da4fa Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 11 Feb 2021 14:47:43 -0500 Subject: [PATCH 3/4] cover lunar new year reel exception --- .../webservices/StoriesService.java | 93 ++++++++++--------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java index 108bf86c..6cd3e43e 100644 --- a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java @@ -113,52 +113,55 @@ public class StoriesService extends BaseService { for (int i = 0; i < feedStoriesReel.length(); ++i) { final JSONObject node = feedStoriesReel.getJSONObject(i); final JSONObject userJson = node.getJSONObject(node.has("user") ? "user" : "owner"); - final User user = new User(userJson.getLong("pk"), - userJson.getString("username"), - userJson.optString("full_name"), - userJson.optBoolean("is_private"), - userJson.getString("profile_pic_url"), - null, - new FriendshipStatus( - false, - false, - false, - false, - false, - false, - false, - false, - false, - false - ), - userJson.optBoolean("is_verified"), - false, - false, - false, - false, - null, - null, - 0, - 0, - 0, - 0, - null, - null, - 0, - null, - null - ); - final String id = node.getString("id"); - final long timestamp = node.getLong("latest_reel_media"); - final int mediaCount = node.getInt("media_count"); - final boolean fullyRead = !node.isNull("seen") && node.getLong("seen") == timestamp; - final JSONObject itemJson = node.has("items") ? node.getJSONArray("items").optJSONObject(0) : null; - final boolean isBestie = node.optBoolean("has_besties_media", false); - StoryModel firstStoryModel = null; - if (itemJson != null) { - firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, false, null); + try { + final User user = new User(userJson.getLong("pk"), + userJson.getString("username"), + userJson.optString("full_name"), + userJson.optBoolean("is_private"), + userJson.getString("profile_pic_url"), + null, + new FriendshipStatus( + false, + false, + false, + false, + false, + false, + false, + false, + false, + false + ), + userJson.optBoolean("is_verified"), + false, + false, + false, + false, + null, + null, + 0, + 0, + 0, + 0, + null, + null, + 0, + null, + null + ); + final String id = node.getString("id"); + final long timestamp = node.getLong("latest_reel_media"); + final int mediaCount = node.getInt("media_count"); + final boolean fullyRead = !node.isNull("seen") && node.getLong("seen") == timestamp; + final JSONObject itemJson = node.has("items") ? node.getJSONArray("items").optJSONObject(0) : null; + final boolean isBestie = node.optBoolean("has_besties_media", false); + StoryModel firstStoryModel = null; + if (itemJson != null) { + firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, false, null); + } + feedStoryModels.add(new FeedStoryModel(id, user, fullyRead, timestamp, firstStoryModel, mediaCount, false, isBestie)); } - 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 } final JSONArray broadcasts = new JSONObject(body).getJSONArray("broadcasts"); for (int i = 0; i < broadcasts.length(); ++i) { From c6bbc621f7ebb959573be23cb4413daed4e0e14a Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sat, 13 Feb 2021 13:26:02 -0500 Subject: [PATCH 4/4] disable btndm --- app/build.gradle | 2 +- .../asyncs/CreateThreadAction.java | 91 ++++++++++--------- .../fragments/StoryViewerFragment.java | 8 +- .../fragments/main/ProfileFragment.java | 44 +++++---- 4 files changed, 83 insertions(+), 62 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7db7b24c..70943d90 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { targetSdkVersion 29 versionCode 58 - versionName '19.1.0' + versionName '19.1.0-a1' multiDexEnabled true diff --git a/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java b/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java index a38eee76..ba35ef7e 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java +++ b/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java @@ -3,77 +3,82 @@ package awais.instagrabber.asyncs; import android.os.AsyncTask; import android.util.Log; -import org.json.JSONObject; +import androidx.annotation.NonNull; import java.io.DataOutputStream; -import java.net.HttpURLConnection; -import java.net.URL; +import java.io.IOException; +import java.util.Collections; +import java.util.Locale; +import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.NetworkUtils; import awais.instagrabber.utils.Utils; +import awais.instagrabber.webservices.DirectMessagesService; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import static awais.instagrabber.utils.Utils.settingsHelper; -public class CreateThreadAction extends AsyncTask { +public class CreateThreadAction extends AsyncTask { private static final String TAG = "CommentAction"; private final String cookie; private final long userId; private final OnTaskCompleteListener onTaskCompleteListener; + private final DirectMessagesService directMessagesService; public CreateThreadAction(final String cookie, final long userId, final OnTaskCompleteListener onTaskCompleteListener) { this.cookie = cookie; this.userId = userId; this.onTaskCompleteListener = onTaskCompleteListener; + directMessagesService = DirectMessagesService.getInstance(CookieUtils.getCsrfTokenFromCookie(cookie), + CookieUtils.getUserIdFromCookie(cookie), + Utils.settingsHelper.getString(Constants.DEVICE_UUID)); } - protected String doInBackground(Void... lmao) { - final String url = "https://i.instagram.com/api/v1/direct_v2/create_group_thread/"; - HttpURLConnection urlConnection = null; - try { - urlConnection = (HttpURLConnection) new URL(url).openConnection(); - urlConnection.setRequestMethod("POST"); - urlConnection.setRequestProperty("User-Agent", Utils.settingsHelper.getString(Constants.APP_UA)); - urlConnection.setUseCaches(false); - final String urlParameters = Utils.sign("{\"_csrftoken\":\"" + cookie.split("csrftoken=")[1].split(";")[0] - + "\",\"_uid\":\"" + CookieUtils.getUserIdFromCookie(cookie) - + "\",\"__uuid\":\"" + settingsHelper.getString(Constants.DEVICE_UUID) - + "\",\"recipient_users\":\"[" + userId // <- string of array of number (not joking) - + "]\"}"); - urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - if (urlParameters != null) { - urlConnection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length); + protected Void doInBackground(Void... lmao) { + final Call createThreadRequest = directMessagesService.createThread(Collections.singletonList(userId), null); + createThreadRequest.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, @NonNull final Response response) { + if (!response.isSuccessful()) { + if (response.errorBody() != null) { + try { + final String string = response.errorBody().string(); + final String msg = String.format(Locale.US, + "onResponse: url: %s, responseCode: %d, errorBody: %s", + call.request().url().toString(), + response.code(), + string); + Log.e(TAG, msg); + } catch (IOException e) { + Log.e(TAG, "onResponse: ", e); + } + } + Log.e(TAG, "onResponse: request was not successful and response error body was null"); + } + onTaskCompleteListener.onTaskComplete(response.body()); + if (response.body() == null) { + Log.e(TAG, "onResponse: thread is null"); + } } - urlConnection.setDoOutput(true); - DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); - wr.writeBytes(urlParameters); - wr.flush(); - wr.close(); - urlConnection.connect(); - if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { - return new JSONObject(NetworkUtils.readFromConnection(urlConnection)).getString("thread_id"); + + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + onTaskCompleteListener.onTaskComplete(null); } - } catch (Throwable ex) { - Log.e(TAG, "reply (CT): " + ex); - } finally { - if (urlConnection != null) { - urlConnection.disconnect(); - } - } + }); return null; } - @Override - protected void onPostExecute(final String threadId) { - if (threadId == null || onTaskCompleteListener == null) { - return; - } - onTaskCompleteListener.onTaskComplete(threadId); - } +// @Override +// protected void onPostExecute() { +// } public interface OnTaskCompleteListener { - void onTaskComplete(final String threadId); + void onTaskComplete(final DirectThread thread); } } diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java index 337f2754..c7345228 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java @@ -216,10 +216,14 @@ public class StoryViewerFragment extends Fragment { new AlertDialog.Builder(context) .setTitle(R.string.reply_story) .setView(input) - .setPositiveButton(R.string.confirm, (d, w) -> new CreateThreadAction(cookie, currentStory.getUserId(), threadId -> { + .setPositiveButton(R.string.confirm, (d, w) -> new CreateThreadAction(cookie, currentStory.getUserId(), thread -> { + if (thread == null) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + return; + } try { final Call request = directMessagesService - .broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(threadId), + .broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(thread.getThreadId()), input.getText().toString(), currentStory.getStoryMediaId(), String.valueOf(currentStory.getUserId())); diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 55e52f1b..b66f626c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -56,6 +56,7 @@ import awais.instagrabber.R; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.FeedAdapterV2; import awais.instagrabber.adapters.HighlightsAdapter; +import awais.instagrabber.asyncs.CreateThreadAction; import awais.instagrabber.asyncs.ProfileFetcher; import awais.instagrabber.asyncs.ProfilePostFetchService; import awais.instagrabber.asyncs.UsernameFetcher; @@ -305,9 +306,9 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); fragmentActivity = (MainActivity) requireActivity(); - friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, userId); - storiesService = StoriesService.getInstance(); - mediaService = MediaService.getInstance(null, null, 0); + friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, userId) : null; + storiesService = isLoggedIn ? StoriesService.getInstance() : null; + mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null; accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext())); favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); setHasOptionsMenu(true); @@ -802,18 +803,19 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private void setupButtons(final long profileId, final long myId) { profileDetailsBinding.btnTagged.setVisibility(isReallyPrivate() ? View.GONE : View.VISIBLE); + profileDetailsBinding.btnDM.setVisibility(View.GONE); // temporary measure if (isLoggedIn) { if (Objects.equals(profileId, myId)) { profileDetailsBinding.btnTagged.setVisibility(View.VISIBLE); profileDetailsBinding.btnSaved.setVisibility(View.VISIBLE); profileDetailsBinding.btnLiked.setVisibility(View.VISIBLE); - profileDetailsBinding.btnDM.setVisibility(View.GONE); +// profileDetailsBinding.btnDM.setVisibility(View.GONE); profileDetailsBinding.btnSaved.setText(R.string.saved); return; } profileDetailsBinding.btnSaved.setVisibility(View.GONE); profileDetailsBinding.btnLiked.setVisibility(View.GONE); - profileDetailsBinding.btnDM.setVisibility(View.VISIBLE); // maybe there is a judgment mechanism? +// profileDetailsBinding.btnDM.setVisibility(View.VISIBLE); profileDetailsBinding.btnFollow.setVisibility(View.VISIBLE); final Context context = getContext(); if (context == null) return; @@ -977,17 +979,27 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe PostItemType.TAGGED); NavHostFragment.findNavController(this).navigate(action); }); - profileDetailsBinding.btnDM.setOnClickListener(v -> { - profileDetailsBinding.btnDM.setEnabled(false); - // new CreateThreadAction(cookie, profileModel.getPk(), threadId -> { - // if (isAdded()) { - // final NavDirections action = ProfileFragmentDirections - // .actionProfileFragmentToDMThreadFragment(threadId, profileModel.getUsername()); - // NavHostFragment.findNavController(this).navigate(action); - // } - // profileDetailsBinding.btnDM.setEnabled(true); - // }).execute(); - }); +// profileDetailsBinding.btnDM.setOnClickListener(v -> { +// profileDetailsBinding.btnDM.setEnabled(false); +// new CreateThreadAction(cookie, profileModel.getPk(), thread -> { +// if (thread == null) { +// Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); +// profileDetailsBinding.btnDM.setEnabled(true); +// return; +// } +// if (isAdded()) { +// final Bundle bundle = new Bundle(); +// bundle.putString("threadId", thread.getThreadId()); +// bundle.putString("title", thread.getThreadTitle()); +// if (isAdded()) { +// final NavDirections action = ProfileFragmentDirections +// .actionProfileFragmentToDMThreadFragment(thread.getThreadId(), profileModel.getUsername()); +// NavHostFragment.findNavController(this).navigate(action); +// } +// } +// profileDetailsBinding.btnDM.setEnabled(true); +// }).execute(); +// }); profileDetailsBinding.mainProfileImage.setOnClickListener(v -> { if (!hasStories) { // show profile pic