forked from j62/ctbrec
1
0
Fork 0

Implement follow/unfollow and login with cookies

This commit is contained in:
0xboobface 2018-12-14 20:25:57 +01:00
parent 75ab95e1ea
commit 4d7409f443
5 changed files with 98 additions and 56 deletions

View File

@ -47,7 +47,7 @@ public class StreamateFollowedService extends PaginatedScheduledService {
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException { public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
httpClient.login(); httpClient.login();
String saKey = httpClient.getSaKey(); String saKey = httpClient.getSaKey();
String userId = httpClient.getUserId(); Long userId = httpClient.getUserId();
String _url = url + "&page_number=" + page + "&results_per_page=" + MODELS_PER_PAGE + "&sakey=" + saKey + "&userid=" + userId; String _url = url + "&page_number=" + page + "&results_per_page=" + MODELS_PER_PAGE + "&sakey=" + saKey + "&userid=" + userId;
LOG.debug("Fetching page {}", _url); LOG.debug("Fetching page {}", _url);
Request request = new Request.Builder() Request request = new Request.Builder()
@ -68,7 +68,7 @@ public class StreamateFollowedService extends PaginatedScheduledService {
JSONObject p = performers.getJSONObject(i); JSONObject p = performers.getJSONObject(i);
String nickname = p.getString("Nickname"); String nickname = p.getString("Nickname");
StreamateModel model = (StreamateModel) streamate.createModel(nickname); StreamateModel model = (StreamateModel) streamate.createModel(nickname);
model.setId(Long.toString(p.getLong("PerformerId"))); model.setId(p.getLong("PerformerId"));
model.setPreview("https://m1.nsimg.net/biopic/320x240/" + model.getId()); model.setPreview("https://m1.nsimg.net/biopic/320x240/" + model.getId());
boolean online = p.optString("LiveStatus").equals("live"); boolean online = p.optString("LiveStatus").equals("live");
model.setOnline(online); model.setOnline(online);

View File

@ -61,7 +61,7 @@ public class StreamateUpdateService extends PaginatedScheduledService {
JSONObject p = performers.getJSONObject(i); JSONObject p = performers.getJSONObject(i);
String nickname = p.getString("nickname"); String nickname = p.getString("nickname");
StreamateModel model = (StreamateModel) streamate.createModel(nickname); StreamateModel model = (StreamateModel) streamate.createModel(nickname);
model.setId(Long.toString(p.getLong("id"))); model.setId(p.getLong("id"));
model.setPreview(p.getString("thumbnail")); model.setPreview(p.getString("thumbnail"));
model.setOnline(p.optBoolean("online")); model.setOnline(p.optBoolean("online"));
// TODO figure out, what all the states mean // TODO figure out, what all the states mean

View File

@ -125,7 +125,7 @@ public class Streamate extends AbstractSite {
@Override @Override
public boolean supportsFollow() { public boolean supportsFollow() {
return false; return true;
} }
@Override @Override
@ -151,7 +151,6 @@ public class Streamate extends AbstractSite {
if (response.isSuccessful()) { if (response.isSuccessful()) {
String body = response.body().string(); String body = response.body().string();
JSONObject json = new JSONObject(body); JSONObject json = new JSONObject(body);
LOG.debug(json.toString(2));
if (json.optString("status").equals("SM_OK")) { if (json.optString("status").equals("SM_OK")) {
List<Model> models = new ArrayList<>(); List<Model> models = new ArrayList<>();
JSONObject results = json.getJSONObject("results"); JSONObject results = json.getJSONObject("results");
@ -159,7 +158,7 @@ public class Streamate extends AbstractSite {
for (int i = 0; i < nickname.length(); i++) { for (int i = 0; i < nickname.length(); i++) {
JSONObject result = nickname.getJSONObject(i); JSONObject result = nickname.getJSONObject(i);
StreamateModel model = (StreamateModel) createModel(result.getString("nickname")); StreamateModel model = (StreamateModel) createModel(result.getString("nickname"));
model.setId(result.getString("performerId")); model.setId(Long.parseLong(result.getString("performerId")));
String thumb = result.getString("thumbnail"); String thumb = result.getString("thumbnail");
if (thumb != null) { if (thumb != null) {
model.setPreview(thumb); model.setPreview(thumb);

View File

@ -2,6 +2,7 @@ package ctbrec.sites.streamate;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.NoSuchElementException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -20,7 +21,7 @@ public class StreamateHttpClient extends HttpClient {
private static final transient Logger LOG = LoggerFactory.getLogger(StreamateHttpClient.class); private static final transient Logger LOG = LoggerFactory.getLogger(StreamateHttpClient.class);
private String userId = ""; private Long userId;
private String saKey = ""; private String saKey = "";
public StreamateHttpClient() { public StreamateHttpClient() {
@ -33,6 +34,14 @@ public class StreamateHttpClient extends HttpClient {
.value("1") .value("1")
.build(); .build();
getCookieJar().saveFromResponse(HttpUrl.parse(Streamate.BASE_URL), Collections.singletonList(searchCookie)); getCookieJar().saveFromResponse(HttpUrl.parse(Streamate.BASE_URL), Collections.singletonList(searchCookie));
// try to load sakey from cookie
try {
Cookie cookie = getCookieJar().getCookie(HttpUrl.parse("https://www.streamate.com"), "sakey");
saKey = cookie.value();
} catch (NoSuchElementException e) {
// ignore
}
} }
@Override @Override
@ -65,14 +74,12 @@ public class StreamateHttpClient extends HttpClient {
.build(); .build();
try (Response response = client.newCall(login).execute()) { try (Response response = client.newCall(login).execute()) {
String content = response.body().string(); String content = response.body().string();
//LOG.debug(content);
if(response.isSuccessful()) { if(response.isSuccessful()) {
JSONObject json = new JSONObject(content); JSONObject json = new JSONObject(content);
LOG.debug(json.toString());
loggedIn = json.has("sakey"); loggedIn = json.has("sakey");
saKey = json.optString("sakey"); saKey = json.optString("sakey");
JSONObject account = json.getJSONObject("account"); JSONObject account = json.getJSONObject("account");
userId = Long.toString(account.getLong("userid")); userId = account.getLong("userid");
} else { } else {
throw new IOException("Login failed: " + response.code() + " " + response.message()); throw new IOException("Login failed: " + response.code() + " " + response.message());
} }
@ -83,53 +90,40 @@ public class StreamateHttpClient extends HttpClient {
} }
/** /**
* Check, if the login worked * Check, if the login worked by loading the favorites
* @throws IOException
*/ */
public boolean checkLoginSuccess() throws IOException { public boolean checkLoginSuccess() {
//https://www.streamate.com/api/search/v1/favorites?host=streamate.com&domain=streamate.com&page_number=1&results_per_page=48&sakey=62857cfd1908cd28 String url = Streamate.BASE_URL + "/api/search/v1/favorites?host=streamate.com&domain=streamate.com";
return false; url = url + "&page_number=1&results_per_page=48&sakey=" + saKey + "&userid=" + userId;
// String modelName = getAnyModelName(); Request request = new Request.Builder()
// // we request the roomData of a random model, because it contains .url(url)
// // user data, if the user is logged in, which we can use to verify, that the login worked .addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
// String url = Streamate.BASE_URL + "/tools/amf.php"; .addHeader("Accept", "application/json, */*")
// RequestBody body = new FormBody.Builder() .addHeader("Accept-Language", "en")
// .add("method", "getRoomData") .addHeader("Referer", Streamate.BASE_URL)
// .add("args[]", modelName) .build();
// .add("args[]", "false") try(Response response = execute(request)) {
// //.add("method", "ping") // TODO alternative request, but if (response.isSuccessful()) {
// //.add("args[]", <userId>) // where to get the userId String content = response.body().string();
// .build(); JSONObject json = new JSONObject(content);
// Request request = new Request.Builder() if(json.optString("status").equals("SM_OK")) {
// .url(url) return true;
// .addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent) } else {
// .addHeader("Accept", "application/json, text/javascript, */*") return false;
// .addHeader("Accept-Language", "en") }
// .addHeader("Referer", Streamate.BASE_URL) } else {
// .addHeader("X-Requested-With", "XMLHttpRequest") return false;
// .post(body) }
// .build(); } catch(Exception e) {
// try(Response response = execute(request)) { return false;
// if(response.isSuccessful()) { }
// JSONObject json = new JSONObject(response.body().string());
// if(json.optString("status").equals("success")) {
// JSONObject userData = json.getJSONObject("userData");
// userId = userData.optInt("userId");
// return userId > 0;
// } else {
// throw new IOException("Request was not successful: " + json.toString(2));
// }
// } else {
// throw new HttpException(response.code(), response.message());
// }
// }
} }
public String getSaKey() { public String getSaKey() {
return saKey; return saKey;
} }
public String getUserId() { public Long getUserId() {
return userId; return userId;
} }
} }

View File

@ -15,12 +15,16 @@ import org.slf4j.LoggerFactory;
import com.iheartradio.m3u8.ParseException; import com.iheartradio.m3u8.ParseException;
import com.iheartradio.m3u8.PlaylistException; import com.iheartradio.m3u8.PlaylistException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import ctbrec.AbstractModel; import ctbrec.AbstractModel;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.recorder.download.StreamSource; import ctbrec.recorder.download.StreamSource;
import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
public class StreamateModel extends AbstractModel { public class StreamateModel extends AbstractModel {
@ -30,7 +34,7 @@ public class StreamateModel extends AbstractModel {
private boolean online = false; private boolean online = false;
private List<StreamSource> streamSources = new ArrayList<>(); private List<StreamSource> streamSources = new ArrayList<>();
private int[] resolution; private int[] resolution;
private String id; private Long id;
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
@ -180,19 +184,64 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public boolean follow() throws IOException { public boolean follow() throws IOException {
return false; return follow(true);
} }
@Override @Override
public boolean unfollow() throws IOException { public boolean unfollow() throws IOException {
return false; return follow(false);
} }
public String getId() { private boolean follow(boolean follow) throws IOException {
StreamateHttpClient client = (StreamateHttpClient) getSite().getHttpClient();
client.login();
String saKey = client.getSaKey();
Long userId = client.getUserId();
JSONObject requestParams = new JSONObject();
requestParams.put("sakey", saKey);
requestParams.put("userid", userId);
requestParams.put("pid", id);
requestParams.put("domain", "streamate.com");
requestParams.put("fav", follow);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestParams.toString());
String url = site.getBaseUrl() + "/ajax/fav-notify.php?userid="+userId+"&sakey="+saKey+"&pid="+id+"&fav="+follow+"&domain=streamate.com";
Request request = new Request.Builder()
.url(url)
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.addHeader("Accept", "application/json, */*")
.addHeader("Accept-Language", "en")
.addHeader("Referer", getSite().getBaseUrl())
.post(body)
.build();
try(Response response = getSite().getHttpClient().execute(request)) {
String content = response.body().string();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(content);
return json.optBoolean("success");
} else {
throw new HttpException(response.code(), response.message());
}
}
}
public Long getId() {
return id; return id;
} }
public void setId(String id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
}
@Override
public void readSiteSpecificData(JsonReader reader) throws IOException {
reader.nextName();
id = reader.nextLong();
}
@Override
public void writeSiteSpecificData(JsonWriter writer) throws IOException {
writer.name("id").value(id);
}
}