108 lines
3.6 KiB
Java
108 lines
3.6 KiB
Java
package ctbrec.sites.fc2live;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import static ctbrec.io.HttpConstants.*;
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.json.JSONObject;
|
|
import ctbrec.Config;
|
|
import ctbrec.io.CookieJarImpl;
|
|
import ctbrec.io.HttpClient;
|
|
import ctbrec.io.HttpException;
|
|
import okhttp3.FormBody;
|
|
import okhttp3.Request;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.Response;
|
|
import okhttp3.WebSocket;
|
|
import okhttp3.WebSocketListener;
|
|
|
|
@Slf4j
|
|
public class Fc2HttpClient extends HttpClient {
|
|
public Fc2HttpClient(Config config) {
|
|
super("fc2live", config);
|
|
}
|
|
|
|
@Override
|
|
protected CookieJarImpl createCookieJar() {
|
|
return new Fc2CookieJar();
|
|
}
|
|
|
|
@Override
|
|
public boolean login() throws IOException {
|
|
if (loggedIn) {
|
|
return true;
|
|
}
|
|
|
|
if(checkLogin()) {
|
|
loggedIn = true;
|
|
log.debug("Logged in with cookies");
|
|
return true;
|
|
}
|
|
|
|
String username = Config.getInstance().getSettings().fc2liveUsername;
|
|
String password = Config.getInstance().getSettings().fc2livePassword;
|
|
RequestBody body = new FormBody.Builder()
|
|
.add("email", username)
|
|
.add("pass", password)
|
|
.add("image.x", "0")
|
|
.add("image.y", "0")
|
|
.add("done", "")
|
|
.build();
|
|
Request req = new Request.Builder()
|
|
.url("https://secure.id.fc2.com/index.php?mode=login&switch_language=en")
|
|
.header(REFERER, "https://fc2.com/en/login.php")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.post(body)
|
|
.build();
|
|
try(Response resp = execute(req)) {
|
|
if(resp.isSuccessful()) {
|
|
String page = resp.body().string();
|
|
log.debug(page);
|
|
if(page.contains("Invalid e-mail address or password")) {
|
|
return false;
|
|
} else {
|
|
log.debug("Calling https://secure.id.fc2.com/?login=done");
|
|
req = new Request.Builder()
|
|
.url("https://secure.id.fc2.com/?login=done")
|
|
.header(REFERER, "https://secure.id.fc2.com/index.php?mode=login&switch_language=en")
|
|
.build();
|
|
try (Response resp2 = execute(req)) {
|
|
if (resp.isSuccessful()) {
|
|
log.debug("Login complete");
|
|
loggedIn = true;
|
|
return true;
|
|
} else {
|
|
log.debug("Login failed");
|
|
loggedIn = false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
log.error("Login failed {} {}", resp.code(), resp.message());
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean checkLogin() throws IOException {
|
|
Request req = new Request.Builder().url(Fc2Live.BASE_URL + "/api/favoriteManager.php").build();
|
|
try (Response response = execute(req)) {
|
|
if (response.isSuccessful()) {
|
|
String content = response.body().string();
|
|
JSONObject json = new JSONObject(content);
|
|
return json.optInt("status") == 1;
|
|
} else {
|
|
throw new HttpException(response.code(), response.message());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public WebSocket newWebSocket(Request req, WebSocketListener webSocketListener) {
|
|
return client.newWebSocket(req, webSocketListener);
|
|
}
|
|
}
|