From ca8e2d58de9c3e4ad305c38842397217e866ca1d Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Fri, 31 Jul 2020 12:33:38 -0400 Subject: [PATCH] check files in username folders --- .../instagrabber/asyncs/DiscoverFetcher.java | 22 ++++++++++--------- .../instagrabber/asyncs/PostFetcher.java | 21 ++++++++++-------- .../instagrabber/asyncs/PostsFetcher.java | 7 +++--- .../java/awais/instagrabber/utils/Utils.java | 6 +++-- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java index 18eb08f2..a5d276a1 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/DiscoverFetcher.java @@ -24,6 +24,7 @@ import awais.instagrabber.utils.Constants; 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; @@ -46,7 +47,6 @@ public final class DiscoverFetcher extends AsyncTask discoverItemModels = fetchItems(downloadDir, customDir, null, maxId); + final ArrayList discoverItemModels = fetchItems(customDir, null, maxId); if (discoverItemModels != null) { result = discoverItemModels.toArray(new DiscoverItemModel[0]); if (result.length > 0) { @@ -67,7 +67,7 @@ public final class DiscoverFetcher extends AsyncTask fetchItems(final File downloadDir, final File customDir, + private ArrayList fetchItems(final File customDir, ArrayList discoverItemModels, final String maxId) { try { final String url = "https://www.instagram.com/explore/grid/?is_prefetch=false&omit_cover_media=true&module=explore_popular" + @@ -99,7 +99,7 @@ public final class DiscoverFetcher extends AsyncTask 50) this.isFirst = false; - discoverItemModels = fetchItems(downloadDir, customDir, discoverItemModels, + discoverItemModels = fetchItems(customDir, discoverItemModels, "&max_id=" + (lastId++)); } } else { @@ -149,7 +149,7 @@ public final class DiscoverFetcher extends AsyncTask conn.connect(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { - // to check if file exists - final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download"); - File customDir = null; - if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) { - final String customPath = Utils.settingsHelper.getString(FOLDER_PATH); - if (!Utils.isEmpty(customPath)) customDir = new File(customPath); - } final JSONObject media = new JSONObject(Utils.readFromConnection(conn)).getJSONObject("graphql") .getJSONObject("shortcode_media"); final String username = media.has("owner") ? media.getJSONObject("owner").getString(Constants.EXTRAS_USERNAME) : null; + // to check if file exists + 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); + if (!Utils.isEmpty(customPath)) customDir = new File(customPath); + } + final long timestamp = media.getLong("taken_at_timestamp"); final boolean isVideo = media.has("is_video") && media.optBoolean("is_video"); @@ -95,7 +98,7 @@ public final class PostFetcher extends AsyncTask postModel.setCommentsCount(commentsCount); postModel.setCommentsEndCursor(endCursor); - Utils.checkExistence(downloadDir, customDir, username, false, postModel); + Utils.checkExistence(downloadDir, customDir, false, postModel); result = new ViewerPostModel[]{postModel}; @@ -119,7 +122,7 @@ public final class PostFetcher extends AsyncTask media.optJSONObject("location")); postModels[i].setSliderDisplayUrl(node.getString("display_url")); - Utils.checkExistence(downloadDir, customDir, username, true, postModels[i]); + Utils.checkExistence(downloadDir, customDir, true, postModels[i]); } postModels[0].setCommentsCount(commentsCount); diff --git a/app/src/main/java/awais/instagrabber/asyncs/PostsFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/PostsFetcher.java index 5a144d11..26458c64 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/PostsFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/PostsFetcher.java @@ -19,6 +19,7 @@ import awais.instagrabber.utils.Constants; 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; @@ -78,7 +79,8 @@ public final class PostsFetcher extends AsyncTask { if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { // to check if file exists - final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download"); + 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); @@ -126,8 +128,7 @@ public final class PostsFetcher extends AsyncTask { mediaNode.getLong("taken_at_timestamp"), mediaNode.optBoolean("viewer_has_liked"), mediaNode.optBoolean("viewer_has_saved"), mediaNode.getJSONObject("edge_liked_by").getLong("count")); - Utils.checkExistence(downloadDir, customDir, username, isSlider, models[i]); - Utils.checkExistence(downloadDir, customDir, "@"+username, isSlider, models[i]); + Utils.checkExistence(downloadDir, customDir, isSlider, models[i]); } if (models[models.length - 1] != null) diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index 73799437..30e7b4f8 100755 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -827,12 +827,14 @@ public final class Utils { return sb.toString(); } - public static void batchDownload(@NonNull final Context context, @Nullable final String username, final DownloadMethod method, + public static void batchDownload(@NonNull final Context context, @Nullable String username, final DownloadMethod method, final List itemsToDownload) { if (settingsHelper == null) settingsHelper = new SettingsHelper(context); if (itemsToDownload == null || itemsToDownload.size() < 1) return; + if (username.charAt(0) == '@') username = username.substring(1); + if (ContextCompat.checkSelfPermission(context, Utils.PERMS[0]) == PackageManager.PERMISSION_GRANTED) batchDownloadImpl(context, username, method, itemsToDownload); else if (context instanceof Activity) @@ -996,7 +998,7 @@ public final class Utils { return extension; } - public static void checkExistence(final File downloadDir, final File customDir, final String username, final boolean isSlider, + public static void checkExistence(final File downloadDir, final File customDir, final boolean isSlider, @NonNull final BasePostModel model) { boolean exists = false;