Upload
This commit is contained in:
51
src/main/java/tech/nevets/rickrollprot/CheckRoute.java
Normal file
51
src/main/java/tech/nevets/rickrollprot/CheckRoute.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package tech.nevets.rickrollprot;
|
||||
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
import spark.Route;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class CheckRoute implements Route {
|
||||
// Route: "/api/check/:link"
|
||||
@Override
|
||||
public Object handle(Request req, Response res) throws Exception {
|
||||
boolean isRickRoll = false;
|
||||
String link = URLDecoder.decode(req.params("link"), StandardCharsets.UTF_8);
|
||||
String response = makeGetRequest(link);
|
||||
if (response == null) {
|
||||
res.status(400);
|
||||
return "Invalid Link";
|
||||
}
|
||||
if (response.matches("(?i)never.*?gonna.*?Give.*?You.*?Up")
|
||||
|| response.matches("(?i)rick.*?astley")
|
||||
|| response.matches("(?i)rick.*?roll")
|
||||
|| response.contains("nggyu")) {
|
||||
isRickRoll = true;
|
||||
}
|
||||
return "{\"isRickRoll\":" + isRickRoll + "}";
|
||||
}
|
||||
|
||||
private static String makeGetRequest(String link) {
|
||||
try {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(new URI(link))
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.headers().firstValue("Location").isPresent()) {
|
||||
return makeGetRequest(response.headers().firstValue("Location").get());
|
||||
}
|
||||
return response.body();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/main/java/tech/nevets/rickrollprot/Server.java
Normal file
11
src/main/java/tech/nevets/rickrollprot/Server.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package tech.nevets.rickrollprot;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class Server {
|
||||
public static void main(String[] args) {
|
||||
port(8080);
|
||||
|
||||
get("/api/check/:link", new CheckRoute());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user