From 53ce38f5cd2b2dd70577d93298892c6e9c7e7df6 Mon Sep 17 00:00:00 2001 From: tcely Date: Sun, 2 May 2021 20:10:58 -0400 Subject: [PATCH 1/6] Use Uri.parse for IntentUtils.parseUrl Fixes #1178 --- .../instagrabber/activities/MainActivity.java | 8 +++ .../awais/instagrabber/utils/IntentUtils.java | 51 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index ad153f80..82276c1a 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -613,6 +613,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage if (url == null) return; // Log.d(TAG, url); final IntentModel intentModel = IntentUtils.parseUrl(url); + + // Temporarily log URLs that parse in a new way, then use the old parser result + final IntentModel intentModelOld = IntentUtils.parseUrlOld(url); + if (intentModelOld != null && (intentModel == null || (intentModelOld.getType() != intentModel.getType()) || (intentModelOld.getText() != intentModel.getText()))) { + Log.w(TAG, url); + intentModel = intentModelOld; + } + if (intentModel == null) return; showView(intentModel); } diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java index 45af5970..f3bed94b 100644 --- a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -1,17 +1,66 @@ package awais.instagrabber.utils; +import android.net.Uri; import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.util.List; + import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.enums.IntentModelType; public final class IntentUtils { @Nullable - public static IntentModel parseUrl(@NonNull String url) { + public static IntentModel parseUrl(@NonNull final String url) { + try { + final Uri parsedUrl = Uri.parse(url).normalizeScheme(); + } catch (NullPointerException e) { + return null; + } + + // final String domain = parsedUrl.getHost().replaceFirst("^www\\.", ""); + // final boolean isHttpsUri = "https".equals(parsedUrl.getScheme()); + + final List paths = parsedUrl.getPathSegments(); + String path = paths.get(0); + + String text = null; + IntentModelType type = IntentModelType.UNKNOWN; + if (1 == paths.size()) { + text = path; + type = IntentModelType.USERNAME; + } else if ("_u".equals(path) || "u".equals(path)) { + text = paths.get(1); + type = IntentModelType.USERNAME; + } else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) { + text = paths.get(1); + type = IntentModelType.POST; + } else if (2 >= paths.size() && "explore".equals(path)) { + path = paths.get(1); + + if ("locations".equals(path)) { + text = paths.get(2); + type = IntentModelType.LOCATION; + } + + if ("tags".equals(path)) { + text = paths.get(2); + type = IntentModelType.HASHTAG; + } + } + + if (TextUtils.isEmpty(text)) { + return null; + } + + return new IntentModel(type, text); +} + + @Nullable + public static IntentModel parseUrlOld(@NonNull String url) { if (url.contains("instagr.am/")) { url = url.replaceFirst("s?://(?:www\\.)?instagr\\.am/", "s://www.instagram.com/"); } From 8637fceab08374215dd7bdaaa4f3fc0cae3b3ef1 Mon Sep 17 00:00:00 2001 From: tcely Date: Sun, 2 May 2021 20:35:06 -0400 Subject: [PATCH 2/6] Removed logging of parsed differences --- .../java/awais/instagrabber/activities/MainActivity.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 82276c1a..ad153f80 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -613,14 +613,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage if (url == null) return; // Log.d(TAG, url); final IntentModel intentModel = IntentUtils.parseUrl(url); - - // Temporarily log URLs that parse in a new way, then use the old parser result - final IntentModel intentModelOld = IntentUtils.parseUrlOld(url); - if (intentModelOld != null && (intentModel == null || (intentModelOld.getType() != intentModel.getType()) || (intentModelOld.getText() != intentModel.getText()))) { - Log.w(TAG, url); - intentModel = intentModelOld; - } - if (intentModel == null) return; showView(intentModel); } From be252f9a428c42396b8dacf9b9b386d0758a90ff Mon Sep 17 00:00:00 2001 From: tcely Date: Sun, 2 May 2021 20:45:05 -0400 Subject: [PATCH 3/6] Removed old unused functions --- .../awais/instagrabber/utils/IntentUtils.java | 66 ------------------- 1 file changed, 66 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java index f3bed94b..394cb79f 100644 --- a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -57,71 +57,5 @@ public final class IntentUtils { } return new IntentModel(type, text); -} - - @Nullable - public static IntentModel parseUrlOld(@NonNull String url) { - if (url.contains("instagr.am/")) { - url = url.replaceFirst("s?://(?:www\\.)?instagr\\.am/", "s://www.instagram.com/"); - } - final int wwwDel = url.contains("www.") ? 4 : 0; - final boolean isHttps = url.startsWith("https"); - - IntentModelType type = IntentModelType.UNKNOWN; - if (url.contains("instagram.com/")) { - url = url.substring((isHttps ? 22 : 21) + wwwDel); - - // final char firstChar = url.charAt(0); - if (url.startsWith("p/") || url.startsWith("reel/") || url.startsWith("tv/")) { - url = url.substring(url.startsWith("p/") ? 2 : (url.startsWith("tv/") ? 3 : 5)); - type = IntentModelType.POST; - } else if (url.startsWith("explore/tags/")) { - url = url.substring(13); - type = IntentModelType.HASHTAG; - } else if (url.startsWith("explore/locations/")) { - url = url.substring(18); - type = IntentModelType.LOCATION; - } else if (url.startsWith("_u/")) { // usually exists in embeds - url = url.substring(3); - type = IntentModelType.USERNAME; - } - url = cleanString(url); - if (TextUtils.isEmpty(url)) return null; - else if (type == IntentModelType.UNKNOWN){ - type = IntentModelType.USERNAME; - } - } else if (url.contains("ig.me/u/")) { - url = url.substring((isHttps ? 16 : 15) + wwwDel); - url = cleanString(url); - type = IntentModelType.USERNAME; - - } else return null; - - final int clipLen = url.length() - 1; - if (url.charAt(clipLen) == '/') - url = url.substring(0, clipLen); - - if (type == IntentModelType.LOCATION && url.contains("/")) { - url = url.substring(0, url.indexOf("/")); - } - - if (!url.contains("/")) return new IntentModel(type, url); - return null; - } - - @NonNull - public static String cleanString(@NonNull final String clipString) { - final int queryIndex = clipString.indexOf('?'); - final int paramIndex = clipString.indexOf('#'); - int startIndex = -1; - if (queryIndex > 0 && paramIndex > 0) { - if (queryIndex < paramIndex) startIndex = queryIndex; - else if (paramIndex < queryIndex) startIndex = paramIndex; - } else if (queryIndex == -1 && paramIndex >= 0) { - startIndex = paramIndex; - } else if (paramIndex == -1 && queryIndex >= 0) { - startIndex = queryIndex; - } - return startIndex != -1 ? clipString.substring(0, startIndex) : clipString; } } From 94ea635ca41797e04e82496cf42c8251e2707a0f Mon Sep 17 00:00:00 2001 From: tcely Date: Sun, 2 May 2021 22:22:39 -0400 Subject: [PATCH 4/6] Removed try/catch for null url --- app/src/main/java/awais/instagrabber/utils/IntentUtils.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java index 394cb79f..74e7aa52 100644 --- a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -15,11 +15,7 @@ public final class IntentUtils { @Nullable public static IntentModel parseUrl(@NonNull final String url) { - try { - final Uri parsedUrl = Uri.parse(url).normalizeScheme(); - } catch (NullPointerException e) { - return null; - } + final Uri parsedUrl = Uri.parse(url).normalizeScheme(); // final String domain = parsedUrl.getHost().replaceFirst("^www\\.", ""); // final boolean isHttpsUri = "https".equals(parsedUrl.getScheme()); From 9a889ef385aa7b6bc2ae14b1d93bda0da04e9045 Mon Sep 17 00:00:00 2001 From: tcely Date: Wed, 19 May 2021 15:19:44 -0400 Subject: [PATCH 5/6] fix size comparison & handle empty paths list --- .../main/java/awais/instagrabber/utils/IntentUtils.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java index 74e7aa52..6fc8358b 100644 --- a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -21,8 +21,12 @@ public final class IntentUtils { // final boolean isHttpsUri = "https".equals(parsedUrl.getScheme()); final List paths = parsedUrl.getPathSegments(); - String path = paths.get(0); + if (paths.isEmpty()) { + return null; + } + + String path = paths.get(0); String text = null; IntentModelType type = IntentModelType.UNKNOWN; if (1 == paths.size()) { @@ -34,7 +38,7 @@ public final class IntentUtils { } else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) { text = paths.get(1); type = IntentModelType.POST; - } else if (2 >= paths.size() && "explore".equals(path)) { + } else if (2 < paths.size() && "explore".equals(path)) { path = paths.get(1); if ("locations".equals(path)) { From 42dfe01220e4cc2b68e543cf1a0dd2d208b7b14c Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Wed, 19 May 2021 17:00:36 -0400 Subject: [PATCH 6/6] "ig.me/u/..." doesn't exist anymore? so removed it --- app/src/main/java/awais/instagrabber/utils/IntentUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java index 6fc8358b..c1d2e482 100644 --- a/app/src/main/java/awais/instagrabber/utils/IntentUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/IntentUtils.java @@ -32,7 +32,7 @@ public final class IntentUtils { if (1 == paths.size()) { text = path; type = IntentModelType.USERNAME; - } else if ("_u".equals(path) || "u".equals(path)) { + } else if ("_u".equals(path)) { text = paths.get(1); type = IntentModelType.USERNAME; } else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) {