Fix Streamate login

This commit is contained in:
0xb00bface 2021-12-11 21:21:09 +01:00
parent c6f5750e1b
commit f36d833228
3 changed files with 69 additions and 60 deletions

View File

@ -22,6 +22,7 @@ public class HttpConstants {
public static final String USER_AGENT = "User-Agent";
public static final String XML_HTTP_REQUEST = "XMLHttpRequest";
public static final String X_CSRF_TOKEN = "X-CSRF-Token";
public static final String X_XSRF_TOKEN = "X-XSRF-Token";
public static final String X_REQUESTED_WITH = "X-Requested-With";
private HttpConstants() {}

View File

@ -1,21 +1,5 @@
package ctbrec.sites.streamate;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Model;
import ctbrec.StringUtil;
import ctbrec.io.HttpClient;
@ -23,11 +7,24 @@ import ctbrec.io.HttpException;
import ctbrec.sites.AbstractSite;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.io.HttpConstants.ORIGIN;
public class Streamate extends AbstractSite {
private static final Logger LOG = LoggerFactory.getLogger(Streamate.class);
public static final String API_URL = "https://streamate.com";
public static final String BASE_URL = "https://www.streamate.com";
public static final String NAIAD_URL = "https://member.naiadsystems.com/search/v3";
@ -60,34 +57,6 @@ public class Streamate extends AbstractSite {
@Override
public Double getTokenBalance() throws IOException {
// int userId = ((StreamateHttpClient)getHttpClient()).getUserId();
// String url = Streamate.BASE_URL + "/tools/amf.php";
// RequestBody body = new FormBody.Builder()
// .add("method", "ping")
// .add("args[]", Integer.toString(userId))
// .build();
// Request request = new Request.Builder()
// .url(url)
// .addHeader(USER_AGENT, getConfig().getSettings().httpUserAgent)
// .addHeader(ACCEPT, MIMETYPE_APPLICATION_JSON)
// .addHeader(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
// .addHeader(REFERER, Streamate.BASE_URL)
// .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
// .post(body)
// .build();
// try(Response response = getHttpClient().execute(request)) {
// if(response.isSuccessful()) {
// JSONObject json = new JSONObject(response.body().string());
// if(json.optString("status").equals("online")) {
// JSONObject userData = json.getJSONObject("userData");
// return userData.getInt("balance");
// } else {
// throw new IOException("Request was not successful: " + json.toString(2));
// }
// } else {
// throw new HttpException(response.code(), response.message());
// }
// }
return 0d;
}
@ -146,18 +115,19 @@ public class Streamate extends AbstractSite {
String url = NAIAD_URL + "/autocomplete?filters=gender:&performerCount=10&domain=streamate.com&tagCount=5&query=" + URLEncoder.encode(q, "utf-8");
LOG.debug("Search: {}", url);
String saKey = httpClient.getSaKey();
String smtid = UUID.randomUUID() + "G0211569057409";
Request req = httpClient.newRequestBuilder()
.url(url)
.header(ORIGIN, getBaseUrl())
.header("sakey", saKey)
.header("platform", "SCP")
.header("smtid", UUID.randomUUID().toString() + "G0211569057409")
.header("smeid", UUID.randomUUID().toString() + "G0211569057409")
.header("smvid", UUID.randomUUID().toString() + "G0211569057409")
.header("smtid", smtid)
.header("smeid", smtid)
.header("smvid", smtid)
.build();
try (Response response = getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = response.body().string();
String body = Objects.requireNonNull(response.body(), "HTTP body is null").string();
JSONObject json = new JSONObject(body);
if (json.has("performers")) {
List<Model> models = new ArrayList<>();

View File

@ -2,16 +2,16 @@ package ctbrec.sites.streamate;
import ctbrec.Config;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import okhttp3.*;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ctbrec.io.HttpConstants.*;
@ -21,10 +21,12 @@ public class StreamateHttpClient extends HttpClient {
private static final Logger LOG = LoggerFactory.getLogger(StreamateHttpClient.class);
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final String HTTP_BODY_IS_NULL = "HTTP response body is null";
private Long userId;
private String saKey = "";
private String userNickname = "";
private String xsrfToken = "";
public StreamateHttpClient(Config config) {
super("streamate", config);
@ -39,23 +41,52 @@ public class StreamateHttpClient extends HttpClient {
// try to load sakey from cookie
try {
Cookie cookie = getCookieJar().getCookie(HttpUrl.parse("https://www.streamate.com"), SAKEY_KEY);
Cookie cookie = getCookieJar().getCookie(HttpUrl.parse(Streamate.API_URL), SAKEY_KEY);
saKey = cookie.value();
} catch (NoSuchElementException e) {
// ignore
}
loadXsrfToken();
}
private void loadXsrfToken() {
// do a first request to get cookies and stuff
Request req = new Request.Builder() //
.url(Streamate.BASE_URL + "/initialData.js") //
.header(USER_AGENT, config.getSettings().httpUserAgent) //
.header(COOKIE, "smtid=" + UUID.randomUUID() + "; Xld_rct=1;") //
.header(REFERER, Streamate.BASE_URL)
.build();
try (Response resp = execute(req)) {
if (resp.code() == 200) {
String body = Objects.requireNonNull(resp.body(), HTTP_BODY_IS_NULL).string();
LOG.info("Initial request was fine, Extracting XSRF token");
Matcher m = Pattern.compile("\"xsrfToken\":\"(.*?)\"", Pattern.DOTALL).matcher(body);
if (m.find()) {
xsrfToken = m.group(1);
LOG.info("XSRF token is {}", xsrfToken);
} else {
LOG.warn("Couldn't find xsrf in initialData.js");
}
} else {
throw new HttpException(resp.code(), resp.message());
}
} catch (IOException e) {
LOG.error("Initial request failed", e);
}
}
@Override
public synchronized boolean login() throws IOException {
if(loggedIn) {
if (loggedIn) {
return true;
}
boolean cookiesWorked = checkLoginSuccess();
if(cookiesWorked) {
if (cookiesWorked) {
loggedIn = true;
LOG.debug("Logged in with cookies");
LOG.info("Logged in with cookies");
return true;
}
@ -74,12 +105,19 @@ public class StreamateHttpClient extends HttpClient {
loginRequest.put("tzOffsetMinutes", 0);
RequestBody body = RequestBody.Companion.create(loginRequest.toString(), JSON);
Request login = newRequestBuilder()
.url(Streamate.BASE_URL + "/api/member/login")
.url(Streamate.API_URL + "/api/member/login")
.header(USER_AGENT, config.getSettings().httpUserAgent)
.header(ORIGIN, Streamate.BASE_URL)
.header(REFERER, Streamate.BASE_URL + '/')
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
.header(CONTENT_TYPE, MIMETYPE_APPLICATION_JSON)
.header(X_XSRF_TOKEN, xsrfToken)
.post(body)
.build();
try (Response response = client.newCall(login).execute()) {
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
if(response.isSuccessful()) {
String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(content);
loggedIn = json.has(SAKEY_KEY);
saKey = json.optString(SAKEY_KEY);
@ -112,7 +150,7 @@ public class StreamateHttpClient extends HttpClient {
Request request = newRequestBuilder().url(url).build();
try (Response response = execute(request)) {
if (response.isSuccessful()) {
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
String content = Objects.requireNonNull(response.body(), HTTP_BODY_IS_NULL).string();
JSONObject json = new JSONObject(content);
return json.optString("status").equals("SM_OK");
} else {