From a08167381f25366f2180a33ecfdfa7c217e3ec75 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 3 Nov 2020 20:30:07 -0500 Subject: [PATCH] cleanup --- .../instagrabber/asyncs/i/iLikedFetcher.java | 140 ------------ .../instagrabber/asyncs/i/iPostFetcher.java | 205 ------------------ .../instagrabber/asyncs/i/iTopicFetcher.java | 69 ------ 3 files changed, 414 deletions(-) delete mode 100755 app/src/main/java/awais/instagrabber/asyncs/i/iLikedFetcher.java delete mode 100755 app/src/main/java/awais/instagrabber/asyncs/i/iPostFetcher.java delete mode 100755 app/src/main/java/awais/instagrabber/asyncs/i/iTopicFetcher.java diff --git a/app/src/main/java/awais/instagrabber/asyncs/i/iLikedFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/i/iLikedFetcher.java deleted file mode 100755 index 0c7b4af6..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/i/iLikedFetcher.java +++ /dev/null @@ -1,140 +0,0 @@ -package awais.instagrabber.asyncs.i; - -import android.os.AsyncTask; -import android.os.Environment; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.File; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import awais.instagrabber.BuildConfig; -import awais.instagrabber.interfaces.FetchListener; -import awais.instagrabber.models.PostModel; -import awais.instagrabber.models.enums.MediaItemType; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.DownloadUtils; -import awais.instagrabber.utils.NetworkUtils; -import awais.instagrabber.utils.ResponseBodyUtils; -import awais.instagrabber.utils.TextUtils; -import awais.instagrabber.utils.Utils; -import awaisomereport.LogCollector; - -import static awais.instagrabber.utils.Constants.DOWNLOAD_USER_FOLDER; -import static awais.instagrabber.utils.Constants.FOLDER_PATH; -import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; -import static awais.instagrabber.utils.Utils.logCollector; - -public final class iLikedFetcher extends AsyncTask> { - private static final String TAG = "iLikedFetcher"; - - private final String endCursor; - private final FetchListener> fetchListener; - - public iLikedFetcher(final FetchListener> fetchListener) { - this.endCursor = ""; - this.fetchListener = fetchListener; - } - - public iLikedFetcher(final String endCursor, final FetchListener> fetchListener) { - this.endCursor = endCursor == null ? "" : endCursor; - this.fetchListener = fetchListener; - } - - @Override - protected List doInBackground(final Void... voids) { - final String url = "https://i.instagram.com/api/v1/feed/liked/?max_id=" + endCursor; - - List result = new ArrayList<>(); - try { - final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); - conn.setUseCaches(false); - conn.setRequestProperty("User-Agent", Constants.I_USER_AGENT); - conn.connect(); - - if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { - final JSONObject body = new JSONObject(NetworkUtils.readFromConnection(conn)); - - final String endCursor; - final boolean hasNextPage; - - if (body.has("more_available")) { - hasNextPage = body.optBoolean("more_available"); - endCursor = hasNextPage ? body.optString("next_max_id") : null; - } else { - hasNextPage = false; - endCursor = null; - } - - final JSONArray edges = body.getJSONArray("items"); - for (int i = 0; i < edges.length(); ++i) { - final JSONObject mediaNode = edges.getJSONObject(i); - - final boolean isSlider = mediaNode.has("carousel_media_count"); - final boolean isVideo = mediaNode.has("video_duration"); - - final MediaItemType itemType; - if (isSlider) itemType = MediaItemType.MEDIA_TYPE_SLIDER; - else if (isVideo) itemType = MediaItemType.MEDIA_TYPE_VIDEO; - else itemType = MediaItemType.MEDIA_TYPE_IMAGE; - - final PostModel model = new PostModel( - itemType, - mediaNode.getString(Constants.EXTRAS_ID), - isSlider ? ResponseBodyUtils.getHighQualityImage(mediaNode.getJSONArray("carousel_media") - .getJSONObject(0)) - : ResponseBodyUtils.getHighQualityImage(mediaNode), - isSlider ? ResponseBodyUtils.getLowQualityImage(mediaNode.getJSONArray("carousel_media") - .getJSONObject(0)) - : ResponseBodyUtils.getLowQualityImage(mediaNode), - mediaNode.getString("code"), - mediaNode.isNull("caption") ? null : mediaNode.getJSONObject("caption").optString("text"), - mediaNode.getLong("taken_at"), - true, - mediaNode.optBoolean("has_viewer_saved") - // , mediaNode.getLong("like_count") - ); - result.add(model); - String username = mediaNode.getJSONObject("user").getString("username"); - final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download" + - (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/" + username) : "")); - File customDir = null; - if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) { - final String customPath = Utils.settingsHelper.getString(FOLDER_PATH + - (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) - ? ("/" + username) - : "")); - if (!TextUtils.isEmpty(customPath)) customDir = new File(customPath); - } - DownloadUtils.checkExistence(downloadDir, customDir, isSlider, model); - } - - final int length = result.size(); - if (length >= 1 && result.get(length - 1) != null) { - result.get(length - 1).setPageCursor(hasNextPage, endCursor); - } - } - - conn.disconnect(); - } catch (Exception e) { - if (logCollector != null) { - logCollector.appendException(e, LogCollector.LogFile.ASYNC_MAIN_POSTS_FETCHER, "doInBackground"); - } - if (BuildConfig.DEBUG) { - Log.e(TAG, "", e); - } - } - - return result; - } - - @Override - protected void onPostExecute(final List postModels) { - if (fetchListener != null) fetchListener.onResult(postModels); - } -} diff --git a/app/src/main/java/awais/instagrabber/asyncs/i/iPostFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/i/iPostFetcher.java deleted file mode 100755 index b1e0b2fd..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/i/iPostFetcher.java +++ /dev/null @@ -1,205 +0,0 @@ -package awais.instagrabber.asyncs.i; - -import android.os.AsyncTask; -import android.os.Environment; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.File; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import awais.instagrabber.BuildConfig; -import awais.instagrabber.interfaces.FetchListener; -import awais.instagrabber.models.FeedModel; -import awais.instagrabber.models.PostChild; -import awais.instagrabber.models.ProfileModel; -import awais.instagrabber.models.enums.MediaItemType; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.NetworkUtils; -import awais.instagrabber.utils.ResponseBodyUtils; -import awais.instagrabber.utils.TextUtils; -import awais.instagrabber.utils.Utils; -import awaisomereport.LogCollector; - -import static awais.instagrabber.utils.Constants.DOWNLOAD_USER_FOLDER; -import static awais.instagrabber.utils.Constants.FOLDER_PATH; -import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; -import static awais.instagrabber.utils.Utils.logCollector; - -public final class iPostFetcher extends AsyncTask { - private static final String TAG = "iPostFetcher"; - - private final String id; - private final FetchListener fetchListener; - - public iPostFetcher(final String id, final FetchListener fetchListener) { - this.id = id; - this.fetchListener = fetchListener; - } - - @Override - protected FeedModel doInBackground(final Void... voids) { - HttpURLConnection conn = null; - try { - conn = (HttpURLConnection) new URL("https://i.instagram.com/api/v1/media/" + id + "/info").openConnection(); - conn.setUseCaches(false); - conn.setRequestProperty("User-Agent", Constants.USER_AGENT); - conn.connect(); - - if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { - final JSONObject media = new JSONObject(NetworkUtils.readFromConnection(conn)) - .getJSONArray("items") - .getJSONObject(0); - ProfileModel profileModel = null; - if (media.has("user")) { - final JSONObject user = media.getJSONObject("user"); - final JSONObject friendshipStatus = user.optJSONObject("friendship_status"); - boolean following = false; - boolean isRestricted = false; - boolean outgoingRequest = false; - if (friendshipStatus != null) { - following = friendshipStatus.optBoolean("following"); - isRestricted = friendshipStatus.optBoolean("is_restricted"); - outgoingRequest = friendshipStatus.optBoolean("outgoing_request"); - } - profileModel = new ProfileModel( - user.optBoolean("is_private"), - user.optBoolean("is_private"), - user.optBoolean("is_verified"), - user.optString("pk"), - user.getString(Constants.EXTRAS_USERNAME), - user.optString("fullname"), - null, - null, - user.getString("profile_pic_url"), - null, - -1, - -1, - -1, - following, - isRestricted, - false, - outgoingRequest - ); - } - if (profileModel == null) { - return new FeedModel.Builder().build(); - } - - // to check if file exists - final boolean shouldDownloadToUserFolder = Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER); - final File downloadDir = new File( - Environment.getExternalStorageDirectory(), - "Download" + (shouldDownloadToUserFolder ? "/" + profileModel.getUsername() : "")); - File customDir = null; - if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) { - final String customPath = Utils.settingsHelper.getString(FOLDER_PATH) - + (shouldDownloadToUserFolder ? "/" + profileModel.getUsername() : ""); - if (!TextUtils.isEmpty(customPath)) customDir = new File(customPath); - } - - final long timestamp = media.getLong("taken_at"); - - final boolean isVideo = media.has("has_audio") && media.optBoolean("has_audio"); - final boolean isSlider = !media.isNull("carousel_media_count"); - - final MediaItemType mediaItemType; - if (isSlider) mediaItemType = MediaItemType.MEDIA_TYPE_SLIDER; - else if (isVideo) mediaItemType = MediaItemType.MEDIA_TYPE_VIDEO; - else mediaItemType = MediaItemType.MEDIA_TYPE_IMAGE; - - final String postCaption; - final JSONObject mediaToCaption = media.optJSONObject("caption"); - if (mediaToCaption == null) postCaption = null; - else postCaption = mediaToCaption.optString("text"); - - final long commentsCount = media.optLong("comment_count"); - - final JSONObject location = media.optJSONObject("location"); - String locationId = null; - String locationName = null; - if (location != null) { - locationName = location.optString("name"); - if (location.has("id")) { - locationId = location.getString("id"); - } else if (location.has("pk")) { - locationId = location.getString("pk"); - } - } - // final String locationString = location.optString("id") + "/" + location.optString("slug"); - final FeedModel.Builder feedModelBuilder = new FeedModel.Builder() - .setItemType(mediaItemType) - .setPostId(media.getString(Constants.EXTRAS_ID)) - .setDisplayUrl(isVideo ? ResponseBodyUtils.getHighQualityPost(media.optJSONArray("video_versions"), true, true, false) - : ResponseBodyUtils.getHighQualityImage(media)) - .setShortCode(media.getString("code")) - .setPostCaption(TextUtils.isEmpty(postCaption) ? null : postCaption) - .setProfileModel(profileModel) - .setViewCount(isVideo && media.has("view_count") - ? media.getLong("view_count") - : -1) - .setTimestamp(timestamp) - .setLiked(media.optBoolean("has_liked")) - .setBookmarked(media.optBoolean("has_viewer_saved")) - .setLikesCount(media.getLong("like_count")) - .setLocationName(locationName) - .setLocationId(locationId) - .setCommentsCount(commentsCount); - // DownloadUtils.checkExistence(downloadDir, customDir, false, postModel); - - if (isSlider) { - final JSONArray children = media.getJSONArray("carousel_media"); - final List postModels = new ArrayList<>(); - for (int i = 0; i < children.length(); ++i) { - final JSONObject childNode = children.getJSONObject(i); - final boolean isChildVideo = childNode.has("video_duration"); - postModels.add(new PostChild.Builder() - .setItemType(isChildVideo ? MediaItemType.MEDIA_TYPE_VIDEO - : MediaItemType.MEDIA_TYPE_IMAGE) - .setPostId(childNode.has(Constants.EXTRAS_ID) - ? childNode.getString(Constants.EXTRAS_ID) - : media.getString(Constants.EXTRAS_ID)) - // .setShortCode(childNode.optString(Constants.EXTRAS_SHORTCODE)) - .setDisplayUrl( - isChildVideo ? ResponseBodyUtils.getHighQualityPost( - childNode.optJSONArray("video_versions"), true, true, false) - : ResponseBodyUtils.getHighQualityImage(childNode)) - .setVideoViews(isChildVideo && childNode.has("video_view_count") - ? childNode.getLong("video_view_count") - : -1) - .build()); - // DownloadUtils.checkExistence(downloadDir, customDir, true, postModels[i]); - } - feedModelBuilder.setSliderItems(postModels); - } - return feedModelBuilder.build(); - } - } catch (Exception e) { - if (logCollector != null) - logCollector.appendException(e, LogCollector.LogFile.ASYNC_POST_FETCHER, "doInBackground (i)"); - if (BuildConfig.DEBUG) { - Log.e(TAG, "", e); - } - } finally { - if (conn != null) { - conn.disconnect(); - } - } - return null; - } - - @Override - protected void onPreExecute() { - if (fetchListener != null) fetchListener.doBefore(); - } - - @Override - protected void onPostExecute(final FeedModel feedModel) { - if (fetchListener != null) fetchListener.onResult(feedModel); - } -} diff --git a/app/src/main/java/awais/instagrabber/asyncs/i/iTopicFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/i/iTopicFetcher.java deleted file mode 100755 index 1fe62a4c..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/i/iTopicFetcher.java +++ /dev/null @@ -1,69 +0,0 @@ -package awais.instagrabber.asyncs.i; - -import android.os.AsyncTask; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.net.HttpURLConnection; -import java.net.URL; - -import awais.instagrabber.BuildConfig; -import awais.instagrabber.interfaces.FetchListener; -import awais.instagrabber.models.DiscoverTopicModel; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.LocaleUtils; -import awais.instagrabber.utils.NetworkUtils; -import awaisomereport.LogCollector; - -import static awais.instagrabber.utils.Utils.logCollector; - -public final class iTopicFetcher extends AsyncTask { - private final FetchListener fetchListener; - - public iTopicFetcher(final FetchListener fetchListener) { - this.fetchListener = fetchListener; - } - - @Override - protected DiscoverTopicModel doInBackground(final Void... voids) { - final String url = "https://i.instagram.com/api/v1/discover/topical_explore/"; - - DiscoverTopicModel result = null; - try { - final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); - conn.setUseCaches(false); - conn.setRequestProperty("User-Agent", Constants.I_USER_AGENT); - conn.setRequestProperty("Accept-Language", LocaleUtils.getCurrentLocale().getLanguage() + ",en-US;q=0.8"); - conn.connect(); - - if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { - final JSONObject body = new JSONObject(NetworkUtils.readFromConnection(conn)); - - final JSONArray edges = body.getJSONArray("clusters"); - String[] names = new String[edges.length()], ids = new String[edges.length()]; - for (int i = 0; i < names.length; ++i) { - final JSONObject mediaNode = edges.getJSONObject(i); - ids[i] = mediaNode.getString("id"); - names[i] = mediaNode.getString("title"); - } - - result = new DiscoverTopicModel(ids, names, body.getString("rank_token")); - } - - conn.disconnect(); - } catch (Exception e) { - if (logCollector != null) - logCollector.appendException(e, LogCollector.LogFile.ASYNC_DISCOVER_TOPICS_FETCHER, "doInBackground"); - if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); - } - - return result; - } - - @Override - protected void onPostExecute(final DiscoverTopicModel discoverTopicModel) { - if (fetchListener != null) fetchListener.onResult(discoverTopicModel); - } -}