mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 22:57:29 +00:00
Merge pull request #1201 from tcely/issue-1178
Use Uri.parse for IntentUtils.parseUrl
This commit is contained in:
commit
a812161185
@ -1,78 +1,61 @@
|
|||||||
package awais.instagrabber.utils;
|
package awais.instagrabber.utils;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import awais.instagrabber.models.IntentModel;
|
import awais.instagrabber.models.IntentModel;
|
||||||
import awais.instagrabber.models.enums.IntentModelType;
|
import awais.instagrabber.models.enums.IntentModelType;
|
||||||
|
|
||||||
public final class IntentUtils {
|
public final class IntentUtils {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static IntentModel parseUrl(@NonNull String url) {
|
public static IntentModel parseUrl(@NonNull final String url) {
|
||||||
if (url.contains("instagr.am/")) {
|
final Uri parsedUrl = Uri.parse(url).normalizeScheme();
|
||||||
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;
|
// final String domain = parsedUrl.getHost().replaceFirst("^www\\.", "");
|
||||||
if (url.contains("instagram.com/")) {
|
// final boolean isHttpsUri = "https".equals(parsedUrl.getScheme());
|
||||||
url = url.substring((isHttps ? 22 : 21) + wwwDel);
|
|
||||||
|
|
||||||
// final char firstChar = url.charAt(0);
|
final List<String> paths = parsedUrl.getPathSegments();
|
||||||
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;
|
if (paths.isEmpty()) {
|
||||||
|
|
||||||
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
String path = paths.get(0);
|
||||||
public static String cleanString(@NonNull final String clipString) {
|
String text = null;
|
||||||
final int queryIndex = clipString.indexOf('?');
|
IntentModelType type = IntentModelType.UNKNOWN;
|
||||||
final int paramIndex = clipString.indexOf('#');
|
if (1 == paths.size()) {
|
||||||
int startIndex = -1;
|
text = path;
|
||||||
if (queryIndex > 0 && paramIndex > 0) {
|
type = IntentModelType.USERNAME;
|
||||||
if (queryIndex < paramIndex) startIndex = queryIndex;
|
} else if ("_u".equals(path)) {
|
||||||
else if (paramIndex < queryIndex) startIndex = paramIndex;
|
text = paths.get(1);
|
||||||
} else if (queryIndex == -1 && paramIndex >= 0) {
|
type = IntentModelType.USERNAME;
|
||||||
startIndex = paramIndex;
|
} else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) {
|
||||||
} else if (paramIndex == -1 && queryIndex >= 0) {
|
text = paths.get(1);
|
||||||
startIndex = queryIndex;
|
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;
|
||||||
}
|
}
|
||||||
return startIndex != -1 ? clipString.substring(0, startIndex) : clipString;
|
|
||||||
|
if ("tags".equals(path)) {
|
||||||
|
text = paths.get(2);
|
||||||
|
type = IntentModelType.HASHTAG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TextUtils.isEmpty(text)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new IntentModel(type, text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user