forked from j62/ctbrec
Make Showup login work with minimal browser
This commit is contained in:
parent
5f4e17c2d2
commit
cf165b7fc0
|
@ -34,6 +34,7 @@ public class ShowupElectronLoginDialog {
|
|||
config.put("url", URL);
|
||||
config.put("w", 640);
|
||||
config.put("h", 480);
|
||||
config.put("userAgent", Config.getInstance().getSettings().httpUserAgent);
|
||||
JSONObject msg = new JSONObject();
|
||||
msg.put("config", config);
|
||||
browser.run(msg, msgHandler);
|
||||
|
@ -50,7 +51,6 @@ public class ShowupElectronLoginDialog {
|
|||
System.err.println(line); // NOSONAR
|
||||
} else {
|
||||
JSONObject json = new JSONObject(line);
|
||||
//LOG.debug("Browser: {}", json.toString(2));
|
||||
if(json.has("url")) {
|
||||
String url = json.getString("url");
|
||||
LOG.debug(url);
|
||||
|
@ -66,33 +66,31 @@ public class ShowupElectronLoginDialog {
|
|||
} catch(Exception e) {
|
||||
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);
|
||||
browser.executeJavaScript("user.openLoginPopUp();");
|
||||
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')");
|
||||
return;
|
||||
} catch(Exception e) {
|
||||
LOG.warn("Couldn't auto fill username and password for Showup", e);
|
||||
} else if(url.equals(URL + '/') && firstCall) {
|
||||
firstCall = false;
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
browser.executeJavaScript("user.openLoginPopUp();");
|
||||
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')");
|
||||
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");
|
||||
for (int i = 0; i < _cookies.length(); i++) {
|
||||
JSONObject cookie = _cookies.getJSONObject(i);
|
||||
JSONArray cookies = json.getJSONArray("cookies");
|
||||
for (int i = 0; i < cookies.length(); i++) {
|
||||
JSONObject cookie = cookies.getJSONObject(i);
|
||||
if(cookie.getString("domain").contains(DOMAIN)) {
|
||||
Builder b = new Cookie.Builder()
|
||||
.path(cookie.getString("path"))
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -196,7 +198,7 @@ public abstract class HttpClient {
|
|||
|
||||
File cookieFile = new File(Config.getInstance().getConfigDir(), "cookies-" + name + ".json");
|
||||
try(FileOutputStream fout = new FileOutputStream(cookieFile)) {
|
||||
fout.write(json.getBytes("utf-8"));
|
||||
fout.write(json.getBytes(UTF_8));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't persist cookies for {}", name, e);
|
||||
|
@ -211,7 +213,7 @@ public abstract class HttpClient {
|
|||
return;
|
||||
}
|
||||
byte[] jsonBytes = Files.readAllBytes(cookieFile.toPath());
|
||||
String json = new String(jsonBytes, "utf-8");
|
||||
String json = new String(jsonBytes, UTF_8);
|
||||
|
||||
Map<String, List<Cookie>> cookies = cookieJar.getCookies();
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
|
@ -267,7 +269,6 @@ public abstract class HttpClient {
|
|||
}
|
||||
|
||||
public WebSocket newWebSocket(Request request, WebSocketListener l) {
|
||||
//Request request = new Request.Builder().url(url).build();
|
||||
return client.newWebSocket(request, l);
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
|
|||
protected State state = State.UNKNOWN;
|
||||
private int playlistEmptyCount = 0;
|
||||
|
||||
public AbstractHlsDownload(HttpClient client) {
|
||||
protected AbstractHlsDownload(HttpClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,16 +10,12 @@ import java.util.Map;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
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;
|
||||
|
||||
|
@ -77,39 +73,53 @@ 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();
|
||||
return checkLoginSuccess();
|
||||
// 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();
|
||||
// Request req = new Request.Builder()
|
||||
// .url(Showup.BASE_URL)
|
||||
// .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 {
|
||||
// return false;
|
||||
// }
|
||||
// } else {
|
||||
// throw new HttpException(response.code(), response.message());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public boolean checkLoginSuccess() throws IOException {
|
||||
Request req = new Request.Builder()
|
||||
.url(Showup.BASE_URL)
|
||||
.header(ACCEPT_LANGUAGE, "pl")
|
||||
.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 {
|
||||
return false;
|
||||
}
|
||||
loggedIn = responseBody.contains("Wyloguj");
|
||||
return loggedIn;
|
||||
} else {
|
||||
throw new HttpException(response.code(), response.message());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkLoginSuccess() {
|
||||
return loggedIn;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package ctbrec.sites.showup;
|
||||
|
||||
import static ctbrec.Model.State.*;
|
||||
import static ctbrec.io.HttpConstants.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
@ -19,6 +22,8 @@ import ctbrec.AbstractModel;
|
|||
import ctbrec.Config;
|
||||
import ctbrec.Model;
|
||||
import ctbrec.recorder.download.Download;
|
||||
import ctbrec.recorder.download.HttpHeaderFactory;
|
||||
import ctbrec.recorder.download.HttpHeaderFactoryImpl;
|
||||
import ctbrec.recorder.download.StreamSource;
|
||||
|
||||
public class ShowupModel extends AbstractModel {
|
||||
|
@ -131,4 +136,21 @@ public class ShowupModel extends AbstractModel {
|
|||
return new ShowupMergedDownload(getSite().getHttpClient());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaderFactory getHttpHeaderFactory() {
|
||||
HttpHeaderFactoryImpl fac = new HttpHeaderFactoryImpl();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put(ACCEPT, "*/*");
|
||||
headers.put(ACCEPT_LANGUAGE, "pl");
|
||||
headers.put(CONNECTION, KEEP_ALIVE);
|
||||
if (getSite() != null) {
|
||||
headers.put(REFERER, getSite().getBaseUrl());
|
||||
}
|
||||
headers.put(USER_AGENT, Config.getInstance().getSettings().httpUserAgent);
|
||||
fac.setMasterPlaylistHeaders(headers);
|
||||
fac.setSegmentPlaylistHeaders(headers);
|
||||
fac.setSegmentHeaders(headers);
|
||||
return fac;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue