mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-02-01 14:47:32 +01:00
feat(Sync for Reddit): Add Fix /s/ links
patch
This commit is contained in:
parent
85504f6f65
commit
a8c82ad27b
@ -0,0 +1,42 @@
|
|||||||
|
package app.revanced.integrations.syncforreddit;
|
||||||
|
|
||||||
|
import android.os.StrictMode;
|
||||||
|
import app.revanced.integrations.shared.Logger;
|
||||||
|
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public final class FixSLinksPatch {
|
||||||
|
public static String resolveSLink(String link) {
|
||||||
|
if (link.matches(".*reddit\\.com/r/[^/]+/s/[^/]+")) {
|
||||||
|
Logger.printInfo(() -> "Resolving " + link);
|
||||||
|
try {
|
||||||
|
URL url = new URL(link);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setInstanceFollowRedirects(false);
|
||||||
|
connection.setRequestMethod("HEAD");
|
||||||
|
|
||||||
|
// Disable strict mode in order to allow network access on the main thread.
|
||||||
|
// This is not ideal, but it's the easiest solution for now.
|
||||||
|
final var currentPolicy = StrictMode.getThreadPolicy();
|
||||||
|
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||||
|
StrictMode.setThreadPolicy(policy);
|
||||||
|
|
||||||
|
connection.connect();
|
||||||
|
String location = connection.getHeaderField("location");
|
||||||
|
connection.disconnect();
|
||||||
|
|
||||||
|
// Restore the original strict mode policy.
|
||||||
|
StrictMode.setThreadPolicy(currentPolicy);
|
||||||
|
|
||||||
|
Logger.printInfo(() -> "Resolved " + link + " -> " + location);
|
||||||
|
|
||||||
|
return location;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Logger.printException(() -> "Failed to resolve " + link, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return link;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user