forked from j62/ctbrec
1
0
Fork 0

Login every 30 min to LiveJasmin so that the session does not expire

This commit is contained in:
0xboobface 2019-01-09 20:35:31 +01:00
parent 196b82dfa3
commit bf6b715642
5 changed files with 34 additions and 110 deletions

View File

@ -24,7 +24,7 @@ public class LiveJasminElectronLoginDialog {
private CookieJar cookieJar;
private ExternalBrowser browser;
public LiveJasminElectronLoginDialog(CookieJar cookieJar) throws IOException {
public LiveJasminElectronLoginDialog(CookieJar cookieJar) throws Exception {
this.cookieJar = cookieJar;
browser = ExternalBrowser.getInstance();
try {
@ -37,6 +37,8 @@ public class LiveJasminElectronLoginDialog {
browser.run(msg.toString(), msgHandler);
} catch (InterruptedException e) {
throw new IOException("Couldn't wait for login dialog", e);
} catch (IOException e) {
LOG.debug("Error while starting the browser or communication to it", e);
} finally {
browser.close();
browser.release();

View File

@ -3,6 +3,7 @@ package ctbrec.ui.sites.jasmin;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,6 +21,7 @@ public class LiveJasminSiteUi implements SiteUI {
private LiveJasmin liveJasmin;
private LiveJasminTabProvider tabProvider;
private LiveJasminConfigUi configUi;
private long lastLoginTime = 0;
public LiveJasminSiteUi(LiveJasmin liveJasmin) {
this.liveJasmin = liveJasmin;
@ -39,16 +41,23 @@ public class LiveJasminSiteUi implements SiteUI {
@Override
public synchronized boolean login() throws IOException {
// renew login every 30 min
long now = System.currentTimeMillis();
boolean renew = false;
if((now - lastLoginTime) > TimeUnit.MINUTES.toMillis(30)) {
renew = true;
}
boolean automaticLogin = liveJasmin.login();
if(automaticLogin) {
if(automaticLogin && !renew) {
return true;
} else {
lastLoginTime = System.currentTimeMillis();
BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
new Thread (() -> {
// login with external browser window
try {
//LiveJasminElectronLoginDialog dialog =
new LiveJasminElectronLoginDialog(liveJasmin.getHttpClient().getCookieJar());
} catch (Exception e1) {
LOG.error("Error logging in with external browser", e1);

View File

@ -17,6 +17,8 @@ import ctbrec.io.HttpException;
import ctbrec.sites.jasmin.LiveJasmin;
import ctbrec.sites.jasmin.LiveJasminModel;
import ctbrec.ui.PaginatedScheduledService;
import ctbrec.ui.SiteUI;
import ctbrec.ui.SiteUiFactory;
import javafx.concurrent.Task;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
@ -38,8 +40,11 @@ public class LiveJasminUpdateService extends PaginatedScheduledService {
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
@Override
public List<Model> call() throws IOException {
public List<Model> call() throws IOException, NotLoggedInExcetion {
//String _url = url + ((page-1) * 36); // TODO find out how to switch pages
if(!SiteUiFactory.getUi(liveJasmin).login()) {
throw new NotLoggedInExcetion();
}
// sort by popularity
CookieJarImpl cookieJar = liveJasmin.getHttpClient().getCookieJar();
@ -60,6 +65,7 @@ public class LiveJasminUpdateService extends PaginatedScheduledService {
.addHeader("X-Requested-With", "XMLHttpRequest")
.build();
try (Response response = liveJasmin.getHttpClient().execute(request)) {
LOG.debug("Response {} {}", response.code(), response.message());
if (response.isSuccessful()) {
String body = response.body().string();
List<Model> models = new ArrayList<>();
@ -81,6 +87,14 @@ public class LiveJasminUpdateService extends PaginatedScheduledService {
model.setOnlineState(LiveJasminModel.mapStatus(m.optInt("status")));
models.add(model);
}
} else if(json.optString("error").equals("Please login.")) {
SiteUI siteUI = SiteUiFactory.getUi(liveJasmin);
if(siteUI.login()) {
return call();
} else {
LOG.error("Request failed:\n{}", body);
throw new IOException("Response was not successful");
}
} else {
LOG.error("Request failed:\n{}", body);
throw new IOException("Response was not successful");

View File

@ -0,0 +1,5 @@
package ctbrec.ui.sites.jasmin;
public class NotLoggedInExcetion extends Exception {
}

View File

@ -28,52 +28,6 @@ public class LiveJasminHttpClient extends HttpClient {
return true;
}
// loadMainPage(); // to get initial cookies
// Cookie captchaCookie = new Cookie.Builder()
// .domain("livejasmin.com")
// .name("captchaRequired")
// .value("0")
// .build();
// getCookieJar().saveFromResponse(HttpUrl.parse("https://livejasmin.com"), Collections.singletonList(captchaCookie));
// getCookieJar().saveFromResponse(HttpUrl.parse("https://www.livejasmin.com"), Collections.singletonList(captchaCookie));
// getCookieJar().saveFromResponse(HttpUrl.parse("https://m.livejasmin.com"), Collections.singletonList(captchaCookie));
// Map<String, String> formParams = getLoginFormParameters();
// getCookieJar().saveFromResponse(HttpUrl.parse("https://livejasmin.com"), Collections.singletonList(captchaCookie));
// getCookieJar().saveFromResponse(HttpUrl.parse("https://m.livejasmin.com"), Collections.singletonList(captchaCookie));
// String action = formParams.get("action");
// formParams.remove("action");
// Builder formBuilder = new FormBody.Builder();
// for (Entry<String, String> param : formParams.entrySet()) {
// formBuilder.add(param.getKey(), param.getValue());
// }
// formBuilder.add("username", Config.getInstance().getSettings().livejasminUsername);
// formBuilder.add("password", Config.getInstance().getSettings().livejasminPassword);
// FormBody form = formBuilder.build();
// Buffer b = new Buffer();
// form.writeTo(b);
// LOG.debug("Form: {}", b.readUtf8());
// Map<String, List<Cookie>> cookies = getCookieJar().getCookies();
// for (Entry<String, List<Cookie>> domain : cookies.entrySet()) {
// LOG.debug("{}", domain.getKey());
// List<Cookie> cks = domain.getValue();
// for (Cookie cookie : cks) {
// LOG.debug(" {}", cookie);
// }
// }
// Request request = new Request.Builder()
// .url(LiveJasmin.BASE_URL + action)
// .header("User-Agent", USER_AGENT)
// .header("Accept", "*/*")
// .header("Accept-Language", "en")
// .header("Referer", LiveJasmin.BASE_URL + "/en/girls/")
// .header("X-Requested-With", "XMLHttpRequest")
// .post(form)
// .build();
// try(Response response = execute(request)) {
// System.out.println("login " + response.code() + " - " + response.message());
// System.out.println("login " + response.body().string());
// }
boolean cookiesWorked = checkLoginSuccess();
if (cookiesWorked) {
loggedIn = true;
@ -84,67 +38,12 @@ public class LiveJasminHttpClient extends HttpClient {
return false;
}
// private void loadMainPage() throws IOException {
// Request request = new Request.Builder()
// .url(LiveJasmin.BASE_URL)
// .header("User-Agent", USER_AGENT)
// .build();
// try(Response response = execute(request)) {
// }
// }
//
// private Map<String, String> getLoginFormParameters() throws IOException {
// long ts = System.currentTimeMillis();
// String url = LiveJasmin.BASE_URL + "/en/auth/overlay/get-login-block?_dc="+ts;
// Request request = new Request.Builder()
// .url(url)
// .addHeader("User-Agent", USER_AGENT)
// .addHeader("Accept", "application/json, text/javascript, */*")
// .addHeader("Accept-Language", "en")
// .addHeader("Referer", LiveJasmin.BASE_URL)
// .addHeader("X-Requested-With", "XMLHttpRequest")
// .build();
// try(Response response = execute(request)) {
// if(response.isSuccessful()) {
// String body = response.body().string();
// JSONObject json = new JSONObject(body);
// if(json.optBoolean("success")) {
// JSONObject data = json.getJSONObject("data");
// String content = data.getString("content");
// Map<String, String> params = new HashMap<>();
// Element form = HtmlParser.getTag(content, "form");
// params.put("action", form.attr("action"));
// Elements hiddenInputs = HtmlParser.getTags(content, "input[type=hidden]");
// for (Element input : hiddenInputs) {
// String name = input.attr("name");
// String value = input.attr("value");
// params.put(name, value);
// }
// params.put("keepmeloggedin", "1");
// params.put("captcha", "");
// params.remove("captcha_needed");
// return params;
// } else {
// throw new IOException("Request was not successful: " + body);
// }
// } else {
// throw new HttpException(response.code(), response.message());
// }
// }
// }
public boolean checkLoginSuccess() throws IOException {
OkHttpClient temp = client.newBuilder()
.followRedirects(false)
.followSslRedirects(false)
.build();
// getCookieJar().getCookies().entrySet().forEach((e) -> {
// e.getValue().forEach(c -> {
// LOG.debug("LOGIN CHECK: Cookie {} {}={}", e.getKey(), c.name(), c.value());
// });
// });
String url = "https://m.livejasmin.com/en/member/favourite/get-favourite-list?ajax=1";
Request request = new Request.Builder()
.url(url)
@ -159,11 +58,6 @@ public class LiveJasminHttpClient extends HttpClient {
if(response.isSuccessful()) {
return true;
} else {
// if(response.code() >= 300 && response.code() < 400) {
// for (String name : response.headers().names()) {
// LOG.debug("{}: {}", name, response.header(name));
// }
// }
return false;
}
}