Created
October 26, 2018 14:11
-
-
Save Slaynash/18d53023f4bf9cfb278470aa16f47487 to your computer and use it in GitHub Desktop.
[Java][MyAnimeList] Connection API workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSocket; | |
public class TestConnect { | |
private static String username = ""; | |
private static String password = ""; | |
private static String cookies; | |
private static String csrf_token; | |
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InterruptedException { | |
System.out.println(" ----------- Connecting to webserver ----------- "); | |
SSLSocket socket = (SSLSocket) SSLContext.getDefault().getSocketFactory().createSocket("myanimelist.net", 443); | |
System.out.println(" -- Connected ! -- "); | |
BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
PrintWriter outputStream = new PrintWriter(new BufferedOutputStream(socket.getOutputStream())); | |
System.out.println(" ----------- Fetching MALSESSIONID and csrf_token ----------- "); | |
String malSessionId = getSessionId(inputStream, outputStream); | |
System.out.println(); | |
if(malSessionId == null) { | |
System.err.println(" -- Unable to fetch session id. Please verify your internet connection -- "); | |
return; | |
} | |
System.out.println(" -- Data fetched ! -- "); | |
System.out.println("MALSESSIONID : " + malSessionId); | |
System.out.println("csrf_token : " + csrf_token); | |
System.out.println(); | |
System.out.println(" ----------- Trying to login ----------- "); | |
malSessionId = tryLogin(malSessionId, inputStream, outputStream); | |
if(malSessionId == null) { | |
System.err.println(" -- Unable to login -- "); | |
return; | |
} | |
System.out.println(); | |
System.out.println(" -- Logged in sucessfully ! -- "); | |
System.out.println(); | |
System.out.println("Logged MALSESSIONID : " + malSessionId); | |
System.out.println(); | |
socket.close(); | |
System.out.println(" ----------- Disconnected from webserver ----------- "); | |
} | |
public static String getSessionId(BufferedReader input, PrintWriter output) throws IOException { | |
System.out.println("Sending GET request to https://myanimelist.net/"); | |
output.print("GET / HTTP/1.1\r\n"); | |
output.print("User-Agent: Slaynash's MAL API\r\n"); | |
output.print("Host: myanimelist.net\r\n"); | |
output.print("Connection: keep-alive\r\n"); | |
output.print("\r\n"); | |
output.flush(); | |
WebResponse wr = WaitForResponse(input); | |
System.out.println("Return code: " + wr.returnCode); | |
System.out.println("Fetching headers"); | |
List<String> setcookies = wr.headers.get("Set-Cookie"); | |
String msi = null; | |
cookies = null; | |
for(String setcookie : setcookies) { | |
cookies = cookies == null ? setcookie.split(";", 2)[0] : (cookies +"; " + setcookie.split(";", 2)[0]); | |
if(setcookie.startsWith("MALSESSIONID")) | |
msi = setcookie.substring("MALSESSIONID=".length()).split(";", 2)[0]; | |
} | |
System.out.println("Done"); | |
System.out.println("Cookie: " + cookies); | |
csrf_token = wr.data.toString().split("csrf_token", 2)[1].split("'", 4)[2]; | |
System.out.println("csrf_token: " + csrf_token); | |
return msi; | |
} | |
public static String tryLogin(String malSessionId, BufferedReader input, PrintWriter output) throws IOException { | |
System.out.println("Sending POST request to https://myanimelist.net/login.php"); | |
String data = "user_name="+username; | |
data += "&password="+password; | |
data += "&sublogin=Login"; | |
data += "&cookie=1"; | |
data += "&submit=1"; | |
data += "&csrf_token=" + csrf_token; | |
output.print("POST /login.php HTTP/1.1\r\n"); | |
output.print("User-Agent: Slaynash's MAL API\r\n"); | |
output.print("Host: myanimelist.net\r\n"); | |
output.print("Content-Type: application/x-www-form-urlencoded\r\n"); | |
output.print("Content-Length: " + data.getBytes().length + "\r\n"); | |
output.print("Connection: keep-alive\r\n"); | |
System.out.println("MSI: " + malSessionId); | |
output.print("Cookie: MALSESSIONID=" + malSessionId + "\r\n"); | |
output.print("\r\n"); | |
output.print(data); | |
output.flush(); | |
WebResponse wr = WaitForResponse(input); | |
System.out.println("Return code: " + wr.returnCode); | |
System.out.println("Fetching headers"); | |
List<String> setcookies = wr.headers.get("Set-Cookie"); | |
String msi = null; | |
cookies = null; | |
for(String setcookie : setcookies) { | |
cookies = cookies == null ? setcookie.split(";", 2)[0] : (cookies +"; " + setcookie.split(";", 2)[0]); | |
if(setcookie.startsWith("MALSESSIONID")) | |
msi = setcookie.substring("MALSESSIONID=".length()).split(";", 2)[0]; | |
} | |
System.out.println("Done"); | |
System.out.println("Cookie: " + cookies); | |
return msi; | |
} | |
public static WebResponse WaitForResponse(BufferedReader input) throws IOException { | |
WebResponse wr = new WebResponse(); | |
String[] responseCodeRaw = input.readLine().split(" ", 3); | |
wr.version = responseCodeRaw[0]; System.out.println("Version: " + wr.version); | |
wr.returnCode = Integer.parseInt(responseCodeRaw[1]); System.out.println("Return Code: " + wr.returnCode); | |
wr.returnMessage = responseCodeRaw[2]; System.out.println("Return Message: " + wr.returnMessage); | |
String ln; | |
while(!(ln = input.readLine()).equals("")) { | |
wr.addHeader(ln); | |
} | |
int length = 0; | |
do { | |
length = Integer.parseInt(input.readLine(), 16); | |
char[] dataArray = new char[length]; | |
int read = 0; | |
do { | |
read += input.read(dataArray, read, length - read); | |
} while(read != length); | |
wr.data += new String(dataArray); | |
input.readLine(); | |
} while(length > 0); | |
return wr; | |
} | |
public static class WebResponse{ | |
public String version; | |
public int returnCode; | |
public String returnMessage; | |
public Map<String, List<String>> headers = new HashMap<>(); | |
public String data = ""; | |
public void addHeader(String header) { | |
String[] parts = header.split(":", 2); | |
if(!headers.containsKey(parts[0].trim())) | |
headers.put(parts[0].trim(), new ArrayList<>()); | |
headers.get(parts[0].trim()).add(parts[1].trim()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment