1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-15 19:27:31 +00:00

check files in username folders

This commit is contained in:
Austin Huang 2020-07-31 12:33:38 -04:00
parent 69b0fb162a
commit ca8e2d58de
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
4 changed files with 32 additions and 24 deletions

View File

@ -24,6 +24,7 @@ import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awaisomereport.LogCollector; 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_PATH;
import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO;
import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.logCollector;
@ -46,7 +47,6 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
@Override @Override
protected final DiscoverItemModel[] doInBackground(final Void... voids) { protected final DiscoverItemModel[] doInBackground(final Void... voids) {
// to check if file exists // to check if file exists
final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download");
File customDir = null; File customDir = null;
if (settingsHelper.getBoolean(FOLDER_SAVE_TO)) { if (settingsHelper.getBoolean(FOLDER_SAVE_TO)) {
final String customPath = settingsHelper.getString(FOLDER_PATH); final String customPath = settingsHelper.getString(FOLDER_PATH);
@ -55,7 +55,7 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
DiscoverItemModel[] result = null; DiscoverItemModel[] result = null;
final ArrayList<DiscoverItemModel> discoverItemModels = fetchItems(downloadDir, customDir, null, maxId); final ArrayList<DiscoverItemModel> discoverItemModels = fetchItems(customDir, null, maxId);
if (discoverItemModels != null) { if (discoverItemModels != null) {
result = discoverItemModels.toArray(new DiscoverItemModel[0]); result = discoverItemModels.toArray(new DiscoverItemModel[0]);
if (result.length > 0) { if (result.length > 0) {
@ -67,7 +67,7 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
return result; return result;
} }
private ArrayList<DiscoverItemModel> fetchItems(final File downloadDir, final File customDir, private ArrayList<DiscoverItemModel> fetchItems(final File customDir,
ArrayList<DiscoverItemModel> discoverItemModels, final String maxId) { ArrayList<DiscoverItemModel> discoverItemModels, final String maxId) {
try { try {
final String url = "https://www.instagram.com/explore/grid/?is_prefetch=false&omit_cover_media=true&module=explore_popular" + 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<Void, Void, DiscoverItemMod
if ("media_grid".equals(layoutType)) { if ("media_grid".equals(layoutType)) {
final JSONArray medias = layoutContent.getJSONArray("medias"); final JSONArray medias = layoutContent.getJSONArray("medias");
for (int j = 0; j < medias.length(); ++j) for (int j = 0; j < medias.length(); ++j)
discoverItemModels.add(makeDiscoverModel(downloadDir, customDir, discoverItemModels.add(makeDiscoverModel(customDir,
medias.getJSONObject(j).getJSONObject("media"))); medias.getJSONObject(j).getJSONObject("media")));
} else { } else {
@ -108,13 +108,13 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
final JSONObject layoutItem = layoutContent.getJSONObject(isOneSide ? "one_by_two_item" : "two_by_two_item"); final JSONObject layoutItem = layoutContent.getJSONObject(isOneSide ? "one_by_two_item" : "two_by_two_item");
if (layoutItem.has("media")) if (layoutItem.has("media"))
discoverItemModels.add(makeDiscoverModel(downloadDir, customDir, discoverItemModels.add(makeDiscoverModel(customDir,
layoutItem.getJSONObject("media"))); layoutItem.getJSONObject("media")));
if (layoutContent.has("fill_items")) { if (layoutContent.has("fill_items")) {
final JSONArray fillItems = layoutContent.getJSONArray("fill_items"); final JSONArray fillItems = layoutContent.getJSONArray("fill_items");
for (int j = 0; j < fillItems.length(); ++j) for (int j = 0; j < fillItems.length(); ++j)
discoverItemModels.add(makeDiscoverModel(downloadDir, customDir, discoverItemModels.add(makeDiscoverModel(customDir,
fillItems.getJSONObject(j).getJSONObject("media"))); fillItems.getJSONObject(j).getJSONObject("media")));
} }
} }
@ -129,7 +129,7 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
if (this.isFirst) { if (this.isFirst) {
final int size = discoverItemModels.size(); final int size = discoverItemModels.size();
if (size > 50) this.isFirst = false; if (size > 50) this.isFirst = false;
discoverItemModels = fetchItems(downloadDir, customDir, discoverItemModels, discoverItemModels = fetchItems(customDir, discoverItemModels,
"&max_id=" + (lastId++)); "&max_id=" + (lastId++));
} }
} else { } else {
@ -149,7 +149,7 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
} }
@NonNull @NonNull
private DiscoverItemModel makeDiscoverModel(final File downloadDir, final File customDir, private DiscoverItemModel makeDiscoverModel(final File customDir,
@NonNull final JSONObject media) throws Exception { @NonNull final JSONObject media) throws Exception {
final JSONObject user = media.getJSONObject(Constants.EXTRAS_USER); final JSONObject user = media.getJSONObject(Constants.EXTRAS_USER);
final String username = user.getString(Constants.EXTRAS_USERNAME); final String username = user.getString(Constants.EXTRAS_USERNAME);
@ -176,8 +176,10 @@ public final class DiscoverFetcher extends AsyncTask<Void, Void, DiscoverItemMod
media.getString("code"), media.getString("code"),
Utils.getThumbnailUrl(media, mediaType)); Utils.getThumbnailUrl(media, mediaType));
Utils.checkExistence(downloadDir, customDir, username, final File downloadDir = new File(Environment.getExternalStorageDirectory(), "Download" +
mediaType == MediaItemType.MEDIA_TYPE_SLIDER, model); (Utils.settingsHelper.getBoolean(DOWNLOAD_USER_FOLDER) ? ("/"+username) : ""));
Utils.checkExistence(downloadDir, customDir, mediaType == MediaItemType.MEDIA_TYPE_SLIDER, model);
return model; return model;
} }

View File

@ -19,6 +19,7 @@ import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awaisomereport.LogCollector; 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_PATH;
import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO;
import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.logCollector;
@ -41,19 +42,21 @@ public final class PostFetcher extends AsyncTask<Void, Void, ViewerPostModel[]>
conn.connect(); conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 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") final JSONObject media = new JSONObject(Utils.readFromConnection(conn)).getJSONObject("graphql")
.getJSONObject("shortcode_media"); .getJSONObject("shortcode_media");
final String username = media.has("owner") ? media.getJSONObject("owner").getString(Constants.EXTRAS_USERNAME) : null; 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 long timestamp = media.getLong("taken_at_timestamp");
final boolean isVideo = media.has("is_video") && media.optBoolean("is_video"); final boolean isVideo = media.has("is_video") && media.optBoolean("is_video");
@ -95,7 +98,7 @@ public final class PostFetcher extends AsyncTask<Void, Void, ViewerPostModel[]>
postModel.setCommentsCount(commentsCount); postModel.setCommentsCount(commentsCount);
postModel.setCommentsEndCursor(endCursor); postModel.setCommentsEndCursor(endCursor);
Utils.checkExistence(downloadDir, customDir, username, false, postModel); Utils.checkExistence(downloadDir, customDir, false, postModel);
result = new ViewerPostModel[]{postModel}; result = new ViewerPostModel[]{postModel};
@ -119,7 +122,7 @@ public final class PostFetcher extends AsyncTask<Void, Void, ViewerPostModel[]>
media.optJSONObject("location")); media.optJSONObject("location"));
postModels[i].setSliderDisplayUrl(node.getString("display_url")); 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); postModels[0].setCommentsCount(commentsCount);

View File

@ -19,6 +19,7 @@ import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awaisomereport.LogCollector; 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_PATH;
import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO; import static awais.instagrabber.utils.Constants.FOLDER_SAVE_TO;
import static awais.instagrabber.utils.Utils.logCollector; import static awais.instagrabber.utils.Utils.logCollector;
@ -78,7 +79,8 @@ public final class PostsFetcher extends AsyncTask<Void, Void, PostModel[]> {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
// to check if file exists // 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; File customDir = null;
if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) { if (Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) {
final String customPath = Utils.settingsHelper.getString(FOLDER_PATH); final String customPath = Utils.settingsHelper.getString(FOLDER_PATH);
@ -126,8 +128,7 @@ public final class PostsFetcher extends AsyncTask<Void, Void, PostModel[]> {
mediaNode.getLong("taken_at_timestamp"), mediaNode.optBoolean("viewer_has_liked"), mediaNode.getLong("taken_at_timestamp"), mediaNode.optBoolean("viewer_has_liked"),
mediaNode.optBoolean("viewer_has_saved"), mediaNode.getJSONObject("edge_liked_by").getLong("count")); mediaNode.optBoolean("viewer_has_saved"), mediaNode.getJSONObject("edge_liked_by").getLong("count"));
Utils.checkExistence(downloadDir, customDir, username, isSlider, models[i]); Utils.checkExistence(downloadDir, customDir, isSlider, models[i]);
Utils.checkExistence(downloadDir, customDir, "@"+username, isSlider, models[i]);
} }
if (models[models.length - 1] != null) if (models[models.length - 1] != null)

View File

@ -827,12 +827,14 @@ public final class Utils {
return sb.toString(); 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<? extends BasePostModel> itemsToDownload) { final List<? extends BasePostModel> itemsToDownload) {
if (settingsHelper == null) settingsHelper = new SettingsHelper(context); if (settingsHelper == null) settingsHelper = new SettingsHelper(context);
if (itemsToDownload == null || itemsToDownload.size() < 1) return; 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) if (ContextCompat.checkSelfPermission(context, Utils.PERMS[0]) == PackageManager.PERMISSION_GRANTED)
batchDownloadImpl(context, username, method, itemsToDownload); batchDownloadImpl(context, username, method, itemsToDownload);
else if (context instanceof Activity) else if (context instanceof Activity)
@ -996,7 +998,7 @@ public final class Utils {
return extension; 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) { @NonNull final BasePostModel model) {
boolean exists = false; boolean exists = false;