forked from j62/ctbrec
Fix Streamate by removing the use of the XSRF
Apparently the XSRF is not needed anymore
This commit is contained in:
parent
5f517682b9
commit
68c68a126a
|
@ -1,23 +1,5 @@
|
||||||
package ctbrec.ui.sites.streamate;
|
package ctbrec.ui.sites.streamate;
|
||||||
|
|
||||||
import static ctbrec.Model.State.*;
|
|
||||||
import static ctbrec.io.HttpConstants.*;
|
|
||||||
import static ctbrec.sites.streamate.Streamate.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
|
@ -27,15 +9,26 @@ import ctbrec.sites.streamate.StreamateModel;
|
||||||
import ctbrec.ui.tabs.PaginatedScheduledService;
|
import ctbrec.ui.tabs.PaginatedScheduledService;
|
||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static ctbrec.Model.State.OFFLINE;
|
||||||
|
import static ctbrec.Model.State.ONLINE;
|
||||||
|
import static ctbrec.io.HttpConstants.*;
|
||||||
|
import static ctbrec.sites.streamate.Streamate.NAIAD_URL;
|
||||||
|
|
||||||
public class StreamateFollowedService extends PaginatedScheduledService {
|
public class StreamateFollowedService extends PaginatedScheduledService {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(StreamateFollowedService.class);
|
private static final Logger LOG = LoggerFactory.getLogger(StreamateFollowedService.class);
|
||||||
|
|
||||||
private static final int MODELS_PER_PAGE = 48;
|
private static final int MODELS_PER_PAGE = 48;
|
||||||
private Streamate streamate;
|
private final Streamate streamate;
|
||||||
private StreamateHttpClient httpClient;
|
private final StreamateHttpClient httpClient;
|
||||||
private String url;
|
private final String url;
|
||||||
private boolean showOnline = true;
|
private boolean showOnline = true;
|
||||||
|
|
||||||
public StreamateFollowedService(Streamate streamate) {
|
public StreamateFollowedService(Streamate streamate) {
|
||||||
|
@ -46,13 +39,14 @@ public class StreamateFollowedService extends PaginatedScheduledService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Task<List<Model>> createTask() {
|
protected Task<List<Model>> createTask() {
|
||||||
return new Task<List<Model>>() {
|
return new Task<>() {
|
||||||
@Override
|
@Override
|
||||||
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
public List<Model> call() throws IOException {
|
||||||
httpClient.login();
|
httpClient.login();
|
||||||
String saKey = httpClient.getSaKey();
|
String saKey = httpClient.getSaKey();
|
||||||
String pageUrl = url + "&from=" + ((page - 1) * MODELS_PER_PAGE) + "&size=" + MODELS_PER_PAGE;
|
String pageUrl = url + "&from=" + ((page - 1) * MODELS_PER_PAGE) + "&size=" + MODELS_PER_PAGE;
|
||||||
LOG.debug("Fetching page {}", pageUrl);
|
LOG.debug("Fetching page {}", pageUrl);
|
||||||
|
var smtid = UUID.randomUUID() + "G0211569057409";
|
||||||
var request = new Request.Builder()
|
var request = new Request.Builder()
|
||||||
.url(pageUrl)
|
.url(pageUrl)
|
||||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||||
|
@ -62,14 +56,13 @@ public class StreamateFollowedService extends PaginatedScheduledService {
|
||||||
.header(REFERER, streamate.getBaseUrl() + "/view/favorites")
|
.header(REFERER, streamate.getBaseUrl() + "/view/favorites")
|
||||||
.header("sakey", saKey)
|
.header("sakey", saKey)
|
||||||
.header("platform", "SCP")
|
.header("platform", "SCP")
|
||||||
.header("smtid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smtid", smtid)
|
||||||
.header("smeid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smeid", smtid)
|
||||||
.header("smvid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smvid", smtid)
|
||||||
.header("X-XSRF-TOKEN", httpClient.getXsrfToken())
|
|
||||||
.build();
|
.build();
|
||||||
try(var response = streamate.getHttpClient().execute(request)) {
|
try (var response = streamate.getHttpClient().execute(request)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
return parseModels(response.body().string());
|
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(response.code(), response.message());
|
throw new HttpException(response.code(), response.message());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,5 @@
|
||||||
package ctbrec.ui.sites.streamate;
|
package ctbrec.ui.sites.streamate;
|
||||||
|
|
||||||
import static ctbrec.Model.State.*;
|
|
||||||
import static ctbrec.io.HttpConstants.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
import ctbrec.sites.streamate.Streamate;
|
import ctbrec.sites.streamate.Streamate;
|
||||||
|
@ -23,15 +7,27 @@ import ctbrec.sites.streamate.StreamateHttpClient;
|
||||||
import ctbrec.sites.streamate.StreamateModel;
|
import ctbrec.sites.streamate.StreamateModel;
|
||||||
import ctbrec.ui.tabs.PaginatedScheduledService;
|
import ctbrec.ui.tabs.PaginatedScheduledService;
|
||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static ctbrec.Model.State.*;
|
||||||
|
import static ctbrec.io.HttpConstants.ORIGIN;
|
||||||
|
|
||||||
public class StreamateUpdateService extends PaginatedScheduledService {
|
public class StreamateUpdateService extends PaginatedScheduledService {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(StreamateUpdateService.class);
|
private static final Logger LOG = LoggerFactory.getLogger(StreamateUpdateService.class);
|
||||||
|
|
||||||
private static final int MODELS_PER_PAGE = 48;
|
private static final int MODELS_PER_PAGE = 48;
|
||||||
private Streamate streamate;
|
private final Streamate streamate;
|
||||||
private StreamateHttpClient httpClient;
|
private final StreamateHttpClient httpClient;
|
||||||
private String url;
|
private final String url;
|
||||||
|
|
||||||
public StreamateUpdateService(Streamate streamate, String url) {
|
public StreamateUpdateService(Streamate streamate, String url) {
|
||||||
this.streamate = streamate;
|
this.streamate = streamate;
|
||||||
|
@ -41,25 +37,26 @@ public class StreamateUpdateService extends PaginatedScheduledService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Task<List<Model>> createTask() {
|
protected Task<List<Model>> createTask() {
|
||||||
return new Task<List<Model>>() {
|
return new Task<>() {
|
||||||
@Override
|
@Override
|
||||||
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
public List<Model> call() throws IOException {
|
||||||
int from = (page - 1) * MODELS_PER_PAGE;
|
int from = (page - 1) * MODELS_PER_PAGE;
|
||||||
String saKey = httpClient.getSaKey();
|
String saKey = httpClient.getSaKey();
|
||||||
String pageUrl = url + "&from=" + from + "&size=" + MODELS_PER_PAGE;
|
String pageUrl = url + "&from=" + from + "&size=" + MODELS_PER_PAGE;
|
||||||
LOG.debug("Fetching page {}", pageUrl);
|
LOG.debug("Fetching page {}", pageUrl);
|
||||||
|
var smtid = UUID.randomUUID() + "G0211569057409";
|
||||||
var request = httpClient.newRequestBuilder()
|
var request = httpClient.newRequestBuilder()
|
||||||
.url(pageUrl)
|
.url(pageUrl)
|
||||||
.header(ORIGIN, streamate.getBaseUrl())
|
.header(ORIGIN, streamate.getBaseUrl())
|
||||||
.header("sakey", saKey)
|
.header("sakey", saKey)
|
||||||
.header("platform", "SCP")
|
.header("platform", "SCP")
|
||||||
.header("smtid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smtid", smtid)
|
||||||
.header("smeid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smeid", smtid)
|
||||||
.header("smvid", UUID.randomUUID().toString() + "G0211569057409")
|
.header("smvid", smtid)
|
||||||
.build();
|
.build();
|
||||||
try (var response = httpClient.execute(request)) {
|
try (var response = httpClient.execute(request)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
return parseModels(response.body().string());
|
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(response.code(), response.message());
|
throw new HttpException(response.code(), response.message());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,19 @@
|
||||||
package ctbrec.sites.streamate;
|
package ctbrec.sites.streamate;
|
||||||
|
|
||||||
import static ctbrec.io.HttpConstants.*;
|
import ctbrec.Config;
|
||||||
|
import ctbrec.io.HttpClient;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.UUID;
|
import java.util.Objects;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import static ctbrec.io.HttpConstants.*;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import ctbrec.Config;
|
|
||||||
import ctbrec.io.HttpClient;
|
|
||||||
import ctbrec.io.HttpException;
|
|
||||||
import okhttp3.Cookie;
|
|
||||||
import okhttp3.HttpUrl;
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
public class StreamateHttpClient extends HttpClient {
|
public class StreamateHttpClient extends HttpClient {
|
||||||
|
|
||||||
|
@ -34,7 +25,6 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
private Long userId;
|
private Long userId;
|
||||||
private String saKey = "";
|
private String saKey = "";
|
||||||
private String userNickname = "";
|
private String userNickname = "";
|
||||||
private String xsrfToken = null;
|
|
||||||
|
|
||||||
public StreamateHttpClient(Config config) {
|
public StreamateHttpClient(Config config) {
|
||||||
super("streamate", config);
|
super("streamate", config);
|
||||||
|
@ -54,31 +44,6 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
} catch (NoSuchElementException e) {
|
} catch (NoSuchElementException e) {
|
||||||
// ignore
|
// 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().toString()+"; Xld_rct=1;") //
|
|
||||||
.header(REFERER, Streamate.BASE_URL)
|
|
||||||
.build();
|
|
||||||
try (Response resp = execute(req)) {
|
|
||||||
if (resp.code() == 200) {
|
|
||||||
LOG.info("Initial request was fine, Extracting XSRF token");
|
|
||||||
Matcher m = Pattern.compile("\"xsrfToken\":\"(.*?)\"").matcher(resp.body().string());
|
|
||||||
if (m.find()) {
|
|
||||||
xsrfToken = m.group(1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new HttpException(resp.code(), resp.message());
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.error("Initial request failed", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -113,7 +78,7 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
.post(body)
|
.post(body)
|
||||||
.build();
|
.build();
|
||||||
try (Response response = client.newCall(login).execute()) {
|
try (Response response = client.newCall(login).execute()) {
|
||||||
String content = response.body().string();
|
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
|
||||||
if(response.isSuccessful()) {
|
if(response.isSuccessful()) {
|
||||||
JSONObject json = new JSONObject(content);
|
JSONObject json = new JSONObject(content);
|
||||||
loggedIn = json.has(SAKEY_KEY);
|
loggedIn = json.has(SAKEY_KEY);
|
||||||
|
@ -135,8 +100,7 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||||
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||||
.header(REFERER, Streamate.BASE_URL)
|
.header(REFERER, Streamate.BASE_URL)
|
||||||
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST); // @formatter:on
|
||||||
.header("X-XSRF-TOKEN", getXsrfToken()); // @formatter:on
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,7 +112,7 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
Request request = newRequestBuilder().url(url).build();
|
Request request = newRequestBuilder().url(url).build();
|
||||||
try (Response response = execute(request)) {
|
try (Response response = execute(request)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
String content = response.body().string();
|
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
|
||||||
JSONObject json = new JSONObject(content);
|
JSONObject json = new JSONObject(content);
|
||||||
return json.optString("status").equals("SM_OK");
|
return json.optString("status").equals("SM_OK");
|
||||||
} else {
|
} else {
|
||||||
|
@ -173,8 +137,4 @@ public class StreamateHttpClient extends HttpClient {
|
||||||
public String getUserNickname() {
|
public String getUserNickname() {
|
||||||
return userNickname;
|
return userNickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getXsrfToken() {
|
|
||||||
return xsrfToken;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue