Try to make showup external login work

This commit is contained in:
0xb00bface 2020-10-31 22:08:59 +01:00
parent dfb1083c45
commit ece3355e1e
4 changed files with 73 additions and 68 deletions

View File

@ -1,10 +1,7 @@
package ctbrec.ui.sites.showup;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Consumer;
import org.json.JSONArray;
@ -27,6 +24,7 @@ public class ShowupElectronLoginDialog {
public static final String URL = Showup.BASE_URL;
private CookieJar cookieJar;
private ExternalBrowser browser;
private boolean firstCall = true;
public ShowupElectronLoginDialog(CookieJar cookieJar) throws IOException {
this.cookieJar = cookieJar;
@ -55,6 +53,7 @@ public class ShowupElectronLoginDialog {
//LOG.debug("Browser: {}", json.toString(2));
if(json.has("url")) {
String url = json.getString("url");
LOG.debug(url);
if(url.contains("/site/accept_rules")) {
try {
Thread.sleep(500);
@ -68,16 +67,11 @@ public class ShowupElectronLoginDialog {
LOG.warn("Couldn't auto fill username and password for Showup", e);
}
} else if(url.equals(URL + '/')) {
if(firstCall) {
firstCall = false;
try {
Thread.sleep(500);
String[] simplify = new String[] {
//"document.ready(function() {$('a[class~=\"login-btn\"]').click()});",
"console.log('Hello CTBREC');",
"user.openLoginPopUp();"
};
for (String js : simplify) {
browser.executeJavaScript(js);
}
browser.executeJavaScript("user.openLoginPopUp();");
String username = Config.getInstance().getSettings().showupUsername;
if (username != null && !username.trim().isEmpty()) {
browser.executeJavaScript("$('input[name=\"email\"]').attr('value','" + username + "')");
@ -88,10 +82,12 @@ public class ShowupElectronLoginDialog {
browser.executeJavaScript("$('input[name=\"password\"]').attr('value','" + password + "')");
}
browser.executeJavaScript("$('input[name=\"remember\"]').attr('value','true')");
return;
} catch(Exception e) {
LOG.warn("Couldn't auto fill username and password for Showup", e);
}
}
}
if(json.has("cookies")) {
JSONArray _cookies = json.getJSONArray("cookies");
@ -114,20 +110,27 @@ public class ShowupElectronLoginDialog {
b.secure();
}
Cookie c = b.build();
LOG.debug("Adding cookie {}={}", c.name(), c.value());
cookieJar.saveFromResponse(HttpUrl.parse(Showup.BASE_URL), Collections.singletonList(c));
}
}
}
try {
if (Objects.equals(new URL(url).getPath(), "/site/log_in")) {
browser.close();
}
} catch (MalformedURLException e) {
LOG.error("Couldn't parse new url {}", url, e);
} catch (IOException e) {
LOG.error("Couldn't send shutdown request to external browser", e);
}
// if(url.equals(URL + '/')) {
// try {
// List<Cookie> cookies = cookieJar.loadForRequest(HttpUrl.parse(Showup.BASE_URL));
// for (Cookie cookie : cookies) {
// if (Objects.equals(cookie.name(), "showup")) {
// LOG.debug("showup={}", cookie.value());
// browser.close();
// }
// }
// } catch (MalformedURLException e) {
// LOG.error("Couldn't parse new url {}", url, e);
// } catch (IOException e) {
// LOG.error("Couldn't send shutdown request to external browser", e);
// }
// }
}
}
};

View File

@ -8,6 +8,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.sites.showup.Showup;
import ctbrec.sites.showup.ShowupHttpClient;
import ctbrec.ui.controls.Dialogs;
import ctbrec.ui.sites.AbstractSiteUi;
import ctbrec.ui.sites.ConfigUI;
@ -67,15 +68,14 @@ public class ShowupSiteUi extends AbstractSiteUi {
throw new IOException(e);
}
// ShowupHttpClient httpClient = (ShowupHttpClient)site.getHttpClient();
// boolean loggedIn = httpClient.checkLoginSuccess();
// if(loggedIn) {
// LOG.info("Logged in. User ID is {}", httpClient.getUserId());
// } else {
// LOG.info("Login failed");
// }
// return loggedIn;
return true;
ShowupHttpClient httpClient = (ShowupHttpClient)site.getHttpClient();
boolean loggedIn = httpClient.checkLoginSuccess();
if(loggedIn) {
LOG.info("Logged in");
} else {
LOG.info("Login failed");
}
return loggedIn;
}
}

View File

@ -23,7 +23,7 @@ public class ShowupTabProvider extends TabProvider {
tabs.add(createTab("Women", "female"));
tabs.add(createTab("Men", "male"));
tabs.add(createTab("All", "all"));
//tabs.add(new ShowupFollowedTab("Favorites", site));
tabs.add(new ShowupFollowedTab("Favorites", site));
return tabs;
}

View File

@ -3,6 +3,8 @@ package ctbrec.sites.showup;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -10,18 +12,15 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Objects;
import ctbrec.Config;
import ctbrec.Settings;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import okhttp3.Cookie;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
@ -29,6 +28,7 @@ public class ShowupHttpClient extends HttpClient {
private static final Logger LOG = LoggerFactory.getLogger(ShowupHttpClient.class);
private String csrfToken;
private boolean loggedIn = false;
protected ShowupHttpClient() {
super("showup");
@ -79,33 +79,35 @@ public class ShowupHttpClient extends HttpClient {
}
@Override
public boolean login() throws IOException {
Settings settings = Config.getInstance().getSettings();
FormBody body = new FormBody.Builder()
.add("is_ajax", "1")
.add("email", settings.showupUsername)
.add("password", settings.showupPassword)
.add("remember", "1")
.build();
public synchronized boolean login() throws IOException {
if (loggedIn) {
return true;
}
// boolean cookiesWorked = checkLoginSuccess();
// if (cookiesWorked) {
// loggedIn = true;
// LOG.debug("Logged in with cookies");
// return true;
// }
return false;
}
public boolean checkLoginSuccess() throws IOException {
Request req = new Request.Builder()
.url(Showup.BASE_URL)
.url(Showup.BASE_URL + "/site/messages")
//.url("http://dingens.showup.tv:1234/site/messages")
.header(ACCEPT, "*/*")
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header(REFERER, Showup.BASE_URL + '/')
.header(ORIGIN, Showup.BASE_URL)
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
.post(body)
.build();
try (Response response = execute(req)) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
if (responseBody.startsWith("{")) {
JSONObject json = new JSONObject(responseBody);
return json.optString("status").equalsIgnoreCase("success");
} else {
if (response.isSuccessful() && response.code() == 200) {
Files.write(Paths.get("/tmp/messages.html"), response.body().bytes());
//return true;
return false;
}
} else {
throw new HttpException(response.code(), response.message());
}