From 53ce38f5cd2b2dd70577d93298892c6e9c7e7df6 Mon Sep 17 00:00:00 2001 From: tcely Date: Sun, 2 May 2021 20:10:58 -0400 Subject: [PATCH] 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/"); }