2020-11-12 21:19:45 +00:00
|
|
|
package me.kavin.piped.utils;
|
|
|
|
|
2022-02-02 21:05:22 +00:00
|
|
|
import me.kavin.piped.consts.Constants;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2021-03-04 14:14:52 +00:00
|
|
|
import java.net.URLDecoder;
|
2020-11-12 21:19:45 +00:00
|
|
|
import java.net.URLEncoder;
|
2022-02-02 21:05:22 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-11-12 21:19:45 +00:00
|
|
|
|
|
|
|
public class URLUtils {
|
|
|
|
|
|
|
|
public static String silentEncode(String s) {
|
2021-02-24 09:52:29 +00:00
|
|
|
try {
|
2022-02-02 21:05:22 +00:00
|
|
|
return URLEncoder.encode(s, StandardCharsets.UTF_8);
|
2021-02-24 09:52:29 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
return s;
|
2020-11-12 21:19:45 +00:00
|
|
|
}
|
2021-03-04 14:14:52 +00:00
|
|
|
|
|
|
|
public static String silentDecode(String s) {
|
|
|
|
try {
|
2022-02-02 21:05:22 +00:00
|
|
|
return URLDecoder.decode(s, StandardCharsets.UTF_8);
|
2021-03-04 14:14:52 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2022-02-02 21:05:22 +00:00
|
|
|
|
|
|
|
public static String substringYouTube(String s) {
|
|
|
|
return StringUtils.isEmpty(s) ? null : StringUtils.substringAfter(s, "youtube.com");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String rewriteURL(final String old) {
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(old)) return null;
|
|
|
|
|
|
|
|
URL url = null;
|
|
|
|
try {
|
|
|
|
url = new URL(old);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
ExceptionHandler.handle(e);
|
|
|
|
}
|
|
|
|
assert url != null;
|
|
|
|
|
|
|
|
final String host = url.getHost();
|
|
|
|
|
|
|
|
String query = url.getQuery();
|
|
|
|
|
|
|
|
boolean hasQuery = query != null;
|
|
|
|
|
|
|
|
String path = url.getPath();
|
|
|
|
|
|
|
|
if (path.contains("=")) {
|
|
|
|
path = StringUtils.substringBefore(path, "=") + "=" + StringUtils.substringAfter(path, "=").replace("-rj", "-rw");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Constants.PROXY_PART + path + (hasQuery ? "?" + query + "&host=" : "?host=") + silentEncode(host);
|
|
|
|
|
|
|
|
}
|
2020-11-12 21:19:45 +00:00
|
|
|
}
|