forked from j62/ctbrec
1
0
Fork 0

Implement login for Flirt4Free

This commit is contained in:
0xboobface 2019-04-15 19:16:04 +02:00
parent 8910dd8c4a
commit 50c38d16a7
1 changed files with 20 additions and 41 deletions

View File

@ -1,18 +1,14 @@
package ctbrec.sites.flirt4free;
import java.io.IOException;
import java.util.Objects;
import java.util.List;
import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.sites.camsoda.Camsoda;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
@ -20,7 +16,6 @@ import okhttp3.Response;
public class Flirt4FreeHttpClient extends HttpClient {
private static final transient Logger LOG = LoggerFactory.getLogger(Flirt4FreeHttpClient.class);
private String csrfToken = null;
public Flirt4FreeHttpClient() {
super("flirt4free");
@ -39,29 +34,26 @@ public class Flirt4FreeHttpClient extends HttpClient {
return true;
}
String url = Flirt4Free.BASE_URI + "/api/v1/auth/login";
String url = Flirt4Free.BASE_URI + "/my-account/";
FormBody body = new FormBody.Builder()
.add("username", Config.getInstance().getSettings().camsodaUsername)
.add("password", Config.getInstance().getSettings().camsodaPassword)
.add("a", "login")
.add("small", "0")
.add("acctCheck", "1")
.add("login[remember_me]", "1")
.add("send_to_page", Flirt4Free.BASE_URI)
.add("query_string", "")
.add("login[username]", Config.getInstance().getSettings().flirt4freeUsername)
.add("login[password]", Config.getInstance().getSettings().flirt4freePassword)
.build();
Request request = new Request.Builder()
.url(url)
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.post(body)
.build();
try (Response response = execute(request)) {
if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
if (resp.has("error")) {
String error = resp.getString("error");
if (Objects.equals(error, "Please confirm that you are not a robot.")) {
// return loginWithDialog();
throw new IOException("CamSoda requested to solve a captcha. Please try again in a while (maybe 15 min).");
} else {
throw new IOException(resp.getString("error"));
}
} else {
return true;
}
List<String> cookies = response.headers("Set-Cookie");
return cookies.toString().contains("has_logged_in=1") && cookies.toString().contains("CHAT_USER=" + Config.getInstance().getSettings().flirt4freeUsername);
} else {
throw new HttpException(response.code(), response.message());
}
@ -73,31 +65,18 @@ public class Flirt4FreeHttpClient extends HttpClient {
* @throws IOException
*/
public boolean checkLoginSuccess() throws IOException {
String url = Camsoda.BASE_URI + "/api/v1/user/current";
Request request = new Request.Builder().url(url).build();
String url = Flirt4Free.BASE_URI + "/my-account/";
Request request = new Request.Builder()
.url(url)
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.build();
try(Response response = execute(request)) {
if(response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
return resp.optBoolean("status");
String body = response.body().string();
return body.contains("username-logged-in");
} else {
return false;
}
}
}
protected String getCsrfToken() throws IOException {
if(csrfToken == null) {
String url = Camsoda.BASE_URI;
Request request = new Request.Builder().url(url).build();
try(Response response = execute(request)) {
if(response.isSuccessful()) {
Element meta = HtmlParser.getTag(response.body().string(), "meta[name=\"_token\"]");
csrfToken = meta.attr("content");
} else {
throw new HttpException(response.code(), response.message());
}
}
}
return csrfToken;
}
}