1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 13:26:59 +00:00

Use Uri.parse for IntentUtils.parseUrl

Fixes #1178
This commit is contained in:
tcely 2021-05-02 20:10:58 -04:00
parent ad293b6e85
commit 53ce38f5cd
No known key found for this signature in database
GPG Key ID: E361829E1B3040DB
2 changed files with 58 additions and 1 deletions

View File

@ -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);
}

View File

@ -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<String> 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/");
}