Implement follow/unfollow and login with cookies
This commit is contained in:
parent
75ab95e1ea
commit
4d7409f443
|
@ -47,7 +47,7 @@ public class StreamateFollowedService extends PaginatedScheduledService {
|
|||
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
||||
httpClient.login();
|
||||
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;
|
||||
LOG.debug("Fetching page {}", _url);
|
||||
Request request = new Request.Builder()
|
||||
|
@ -68,7 +68,7 @@ public class StreamateFollowedService extends PaginatedScheduledService {
|
|||
JSONObject p = performers.getJSONObject(i);
|
||||
String nickname = p.getString("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());
|
||||
boolean online = p.optString("LiveStatus").equals("live");
|
||||
model.setOnline(online);
|
||||
|
|
|
@ -61,7 +61,7 @@ public class StreamateUpdateService extends PaginatedScheduledService {
|
|||
JSONObject p = performers.getJSONObject(i);
|
||||
String nickname = p.getString("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.setOnline(p.optBoolean("online"));
|
||||
// TODO figure out, what all the states mean
|
||||
|
|
|
@ -125,7 +125,7 @@ public class Streamate extends AbstractSite {
|
|||
|
||||
@Override
|
||||
public boolean supportsFollow() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -151,7 +151,6 @@ public class Streamate extends AbstractSite {
|
|||
if (response.isSuccessful()) {
|
||||
String body = response.body().string();
|
||||
JSONObject json = new JSONObject(body);
|
||||
LOG.debug(json.toString(2));
|
||||
if (json.optString("status").equals("SM_OK")) {
|
||||
List<Model> models = new ArrayList<>();
|
||||
JSONObject results = json.getJSONObject("results");
|
||||
|
@ -159,7 +158,7 @@ public class Streamate extends AbstractSite {
|
|||
for (int i = 0; i < nickname.length(); i++) {
|
||||
JSONObject result = nickname.getJSONObject(i);
|
||||
StreamateModel model = (StreamateModel) createModel(result.getString("nickname"));
|
||||
model.setId(result.getString("performerId"));
|
||||
model.setId(Long.parseLong(result.getString("performerId")));
|
||||
String thumb = result.getString("thumbnail");
|
||||
if (thumb != null) {
|
||||
model.setPreview(thumb);
|
||||
|
|
|
@ -2,6 +2,7 @@ package ctbrec.sites.streamate;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -20,7 +21,7 @@ public class StreamateHttpClient extends HttpClient {
|
|||
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(StreamateHttpClient.class);
|
||||
|
||||
private String userId = "";
|
||||
private Long userId;
|
||||
private String saKey = "";
|
||||
|
||||
public StreamateHttpClient() {
|
||||
|
@ -33,6 +34,14 @@ public class StreamateHttpClient extends HttpClient {
|
|||
.value("1")
|
||||
.build();
|
||||
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
|
||||
|
@ -65,14 +74,12 @@ public class StreamateHttpClient extends HttpClient {
|
|||
.build();
|
||||
try (Response response = client.newCall(login).execute()) {
|
||||
String content = response.body().string();
|
||||
//LOG.debug(content);
|
||||
if(response.isSuccessful()) {
|
||||
JSONObject json = new JSONObject(content);
|
||||
LOG.debug(json.toString());
|
||||
loggedIn = json.has("sakey");
|
||||
saKey = json.optString("sakey");
|
||||
JSONObject account = json.getJSONObject("account");
|
||||
userId = Long.toString(account.getLong("userid"));
|
||||
userId = account.getLong("userid");
|
||||
} else {
|
||||
throw new IOException("Login failed: " + response.code() + " " + response.message());
|
||||
}
|
||||
|
@ -83,53 +90,40 @@ public class StreamateHttpClient extends HttpClient {
|
|||
}
|
||||
|
||||
/**
|
||||
* Check, if the login worked
|
||||
* @throws IOException
|
||||
* Check, if the login worked by loading the favorites
|
||||
*/
|
||||
public boolean checkLoginSuccess() throws IOException {
|
||||
//https://www.streamate.com/api/search/v1/favorites?host=streamate.com&domain=streamate.com&page_number=1&results_per_page=48&sakey=62857cfd1908cd28
|
||||
public boolean checkLoginSuccess() {
|
||||
String url = Streamate.BASE_URL + "/api/search/v1/favorites?host=streamate.com&domain=streamate.com";
|
||||
url = url + "&page_number=1&results_per_page=48&sakey=" + saKey + "&userid=" + userId;
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader("Accept", "application/json, */*")
|
||||
.addHeader("Accept-Language", "en")
|
||||
.addHeader("Referer", Streamate.BASE_URL)
|
||||
.build();
|
||||
try(Response response = execute(request)) {
|
||||
if (response.isSuccessful()) {
|
||||
String content = response.body().string();
|
||||
JSONObject json = new JSONObject(content);
|
||||
if(json.optString("status").equals("SM_OK")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
// String modelName = getAnyModelName();
|
||||
// // we request the roomData of a random model, because it contains
|
||||
// // user data, if the user is logged in, which we can use to verify, that the login worked
|
||||
// String url = Streamate.BASE_URL + "/tools/amf.php";
|
||||
// RequestBody body = new FormBody.Builder()
|
||||
// .add("method", "getRoomData")
|
||||
// .add("args[]", modelName)
|
||||
// .add("args[]", "false")
|
||||
// //.add("method", "ping") // TODO alternative request, but
|
||||
// //.add("args[]", <userId>) // where to get the userId
|
||||
// .build();
|
||||
// Request request = new Request.Builder()
|
||||
// .url(url)
|
||||
// .addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||
// .addHeader("Accept", "application/json, text/javascript, */*")
|
||||
// .addHeader("Accept-Language", "en")
|
||||
// .addHeader("Referer", Streamate.BASE_URL)
|
||||
// .addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
// .post(body)
|
||||
// .build();
|
||||
// try(Response response = execute(request)) {
|
||||
// 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());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch(Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getSaKey() {
|
||||
return saKey;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,12 +15,16 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import com.iheartradio.m3u8.ParseException;
|
||||
import com.iheartradio.m3u8.PlaylistException;
|
||||
import com.squareup.moshi.JsonReader;
|
||||
import com.squareup.moshi.JsonWriter;
|
||||
|
||||
import ctbrec.AbstractModel;
|
||||
import ctbrec.Config;
|
||||
import ctbrec.io.HttpException;
|
||||
import ctbrec.recorder.download.StreamSource;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class StreamateModel extends AbstractModel {
|
||||
|
@ -30,7 +34,7 @@ public class StreamateModel extends AbstractModel {
|
|||
private boolean online = false;
|
||||
private List<StreamSource> streamSources = new ArrayList<>();
|
||||
private int[] resolution;
|
||||
private String id;
|
||||
private Long id;
|
||||
|
||||
@Override
|
||||
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
|
||||
|
@ -180,19 +184,64 @@ public class StreamateModel extends AbstractModel {
|
|||
|
||||
@Override
|
||||
public boolean follow() throws IOException {
|
||||
return false;
|
||||
return follow(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
public void setId(Long 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue