Try to make showup external login work
This commit is contained in:
parent
dfb1083c45
commit
ece3355e1e
|
@ -1,10 +1,7 @@
|
||||||
package ctbrec.ui.sites.showup;
|
package ctbrec.ui.sites.showup;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -27,6 +24,7 @@ public class ShowupElectronLoginDialog {
|
||||||
public static final String URL = Showup.BASE_URL;
|
public static final String URL = Showup.BASE_URL;
|
||||||
private CookieJar cookieJar;
|
private CookieJar cookieJar;
|
||||||
private ExternalBrowser browser;
|
private ExternalBrowser browser;
|
||||||
|
private boolean firstCall = true;
|
||||||
|
|
||||||
public ShowupElectronLoginDialog(CookieJar cookieJar) throws IOException {
|
public ShowupElectronLoginDialog(CookieJar cookieJar) throws IOException {
|
||||||
this.cookieJar = cookieJar;
|
this.cookieJar = cookieJar;
|
||||||
|
@ -55,6 +53,7 @@ public class ShowupElectronLoginDialog {
|
||||||
//LOG.debug("Browser: {}", json.toString(2));
|
//LOG.debug("Browser: {}", json.toString(2));
|
||||||
if(json.has("url")) {
|
if(json.has("url")) {
|
||||||
String url = json.getString("url");
|
String url = json.getString("url");
|
||||||
|
LOG.debug(url);
|
||||||
if(url.contains("/site/accept_rules")) {
|
if(url.contains("/site/accept_rules")) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
|
@ -68,28 +67,25 @@ public class ShowupElectronLoginDialog {
|
||||||
LOG.warn("Couldn't auto fill username and password for Showup", e);
|
LOG.warn("Couldn't auto fill username and password for Showup", e);
|
||||||
}
|
}
|
||||||
} else if(url.equals(URL + '/')) {
|
} else if(url.equals(URL + '/')) {
|
||||||
try {
|
if(firstCall) {
|
||||||
Thread.sleep(500);
|
firstCall = false;
|
||||||
String[] simplify = new String[] {
|
try {
|
||||||
//"document.ready(function() {$('a[class~=\"login-btn\"]').click()});",
|
Thread.sleep(500);
|
||||||
"console.log('Hello CTBREC');",
|
browser.executeJavaScript("user.openLoginPopUp();");
|
||||||
"user.openLoginPopUp();"
|
String username = Config.getInstance().getSettings().showupUsername;
|
||||||
};
|
if (username != null && !username.trim().isEmpty()) {
|
||||||
for (String js : simplify) {
|
browser.executeJavaScript("$('input[name=\"email\"]').attr('value','" + username + "')");
|
||||||
browser.executeJavaScript(js);
|
}
|
||||||
|
String password = Config.getInstance().getSettings().showupPassword;
|
||||||
|
if (password != null && !password.trim().isEmpty()) {
|
||||||
|
password = password.replace("'", "\\'");
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
String username = Config.getInstance().getSettings().showupUsername;
|
|
||||||
if (username != null && !username.trim().isEmpty()) {
|
|
||||||
browser.executeJavaScript("$('input[name=\"email\"]').attr('value','" + username + "')");
|
|
||||||
}
|
|
||||||
String password = Config.getInstance().getSettings().showupPassword;
|
|
||||||
if (password != null && !password.trim().isEmpty()) {
|
|
||||||
password = password.replace("'", "\\'");
|
|
||||||
browser.executeJavaScript("$('input[name=\"password\"]').attr('value','" + password + "')");
|
|
||||||
}
|
|
||||||
browser.executeJavaScript("$('input[name=\"remember\"]').attr('value','true')");
|
|
||||||
} catch(Exception e) {
|
|
||||||
LOG.warn("Couldn't auto fill username and password for Showup", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,20 +110,27 @@ public class ShowupElectronLoginDialog {
|
||||||
b.secure();
|
b.secure();
|
||||||
}
|
}
|
||||||
Cookie c = b.build();
|
Cookie c = b.build();
|
||||||
|
LOG.debug("Adding cookie {}={}", c.name(), c.value());
|
||||||
cookieJar.saveFromResponse(HttpUrl.parse(Showup.BASE_URL), Collections.singletonList(c));
|
cookieJar.saveFromResponse(HttpUrl.parse(Showup.BASE_URL), Collections.singletonList(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// if(url.equals(URL + '/')) {
|
||||||
if (Objects.equals(new URL(url).getPath(), "/site/log_in")) {
|
// try {
|
||||||
browser.close();
|
// List<Cookie> cookies = cookieJar.loadForRequest(HttpUrl.parse(Showup.BASE_URL));
|
||||||
}
|
// for (Cookie cookie : cookies) {
|
||||||
} catch (MalformedURLException e) {
|
// if (Objects.equals(cookie.name(), "showup")) {
|
||||||
LOG.error("Couldn't parse new url {}", url, e);
|
// LOG.debug("showup={}", cookie.value());
|
||||||
} catch (IOException e) {
|
// browser.close();
|
||||||
LOG.error("Couldn't send shutdown request to external browser", e);
|
// }
|
||||||
}
|
// }
|
||||||
|
// } 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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ctbrec.sites.showup.Showup;
|
import ctbrec.sites.showup.Showup;
|
||||||
|
import ctbrec.sites.showup.ShowupHttpClient;
|
||||||
import ctbrec.ui.controls.Dialogs;
|
import ctbrec.ui.controls.Dialogs;
|
||||||
import ctbrec.ui.sites.AbstractSiteUi;
|
import ctbrec.ui.sites.AbstractSiteUi;
|
||||||
import ctbrec.ui.sites.ConfigUI;
|
import ctbrec.ui.sites.ConfigUI;
|
||||||
|
@ -40,7 +41,7 @@ public class ShowupSiteUi extends AbstractSiteUi {
|
||||||
@Override
|
@Override
|
||||||
public boolean login() throws IOException {
|
public boolean login() throws IOException {
|
||||||
boolean automaticLogin = site.login();
|
boolean automaticLogin = site.login();
|
||||||
if(automaticLogin) {
|
if (automaticLogin) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
|
BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
|
||||||
|
@ -67,15 +68,14 @@ public class ShowupSiteUi extends AbstractSiteUi {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShowupHttpClient httpClient = (ShowupHttpClient)site.getHttpClient();
|
ShowupHttpClient httpClient = (ShowupHttpClient)site.getHttpClient();
|
||||||
// boolean loggedIn = httpClient.checkLoginSuccess();
|
boolean loggedIn = httpClient.checkLoginSuccess();
|
||||||
// if(loggedIn) {
|
if(loggedIn) {
|
||||||
// LOG.info("Logged in. User ID is {}", httpClient.getUserId());
|
LOG.info("Logged in");
|
||||||
// } else {
|
} else {
|
||||||
// LOG.info("Login failed");
|
LOG.info("Login failed");
|
||||||
// }
|
}
|
||||||
// return loggedIn;
|
return loggedIn;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class ShowupTabProvider extends TabProvider {
|
||||||
tabs.add(createTab("Women", "female"));
|
tabs.add(createTab("Women", "female"));
|
||||||
tabs.add(createTab("Men", "male"));
|
tabs.add(createTab("Men", "male"));
|
||||||
tabs.add(createTab("All", "all"));
|
tabs.add(createTab("All", "all"));
|
||||||
//tabs.add(new ShowupFollowedTab("Favorites", site));
|
tabs.add(new ShowupFollowedTab("Favorites", site));
|
||||||
return tabs;
|
return tabs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package ctbrec.sites.showup;
|
||||||
import static ctbrec.io.HttpConstants.*;
|
import static ctbrec.io.HttpConstants.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -10,18 +12,15 @@ import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Settings;
|
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
import okhttp3.Cookie;
|
import okhttp3.Cookie;
|
||||||
import okhttp3.FormBody;
|
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
@ -29,6 +28,7 @@ public class ShowupHttpClient extends HttpClient {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ShowupHttpClient.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ShowupHttpClient.class);
|
||||||
private String csrfToken;
|
private String csrfToken;
|
||||||
|
private boolean loggedIn = false;
|
||||||
|
|
||||||
protected ShowupHttpClient() {
|
protected ShowupHttpClient() {
|
||||||
super("showup");
|
super("showup");
|
||||||
|
@ -79,33 +79,35 @@ public class ShowupHttpClient extends HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean login() throws IOException {
|
public synchronized boolean login() throws IOException {
|
||||||
Settings settings = Config.getInstance().getSettings();
|
if (loggedIn) {
|
||||||
FormBody body = new FormBody.Builder()
|
return true;
|
||||||
.add("is_ajax", "1")
|
}
|
||||||
.add("email", settings.showupUsername)
|
|
||||||
.add("password", settings.showupPassword)
|
// boolean cookiesWorked = checkLoginSuccess();
|
||||||
.add("remember", "1")
|
// if (cookiesWorked) {
|
||||||
.build();
|
// loggedIn = true;
|
||||||
|
// LOG.debug("Logged in with cookies");
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkLoginSuccess() throws IOException {
|
||||||
Request req = new Request.Builder()
|
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(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||||
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
|
||||||
.header(REFERER, Showup.BASE_URL + '/')
|
.header(REFERER, Showup.BASE_URL + '/')
|
||||||
.header(ORIGIN, Showup.BASE_URL)
|
|
||||||
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
|
||||||
.post(body)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = execute(req)) {
|
try (Response response = execute(req)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful() && response.code() == 200) {
|
||||||
String responseBody = response.body().string();
|
Files.write(Paths.get("/tmp/messages.html"), response.body().bytes());
|
||||||
if (responseBody.startsWith("{")) {
|
//return true;
|
||||||
JSONObject json = new JSONObject(responseBody);
|
return false;
|
||||||
return json.optString("status").equalsIgnoreCase("success");
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(response.code(), response.message());
|
throw new HttpException(response.code(), response.message());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue