From 4d7409f443fc7e4541c6391d49b7758bd7326e77 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 14 Dec 2018 20:25:57 +0100 Subject: [PATCH] Implement follow/unfollow and login with cookies --- .../streamate/StreamateFollowedService.java | 4 +- .../streamate/StreamateUpdateService.java | 2 +- .../ctbrec/sites/streamate/Streamate.java | 5 +- .../sites/streamate/StreamateHttpClient.java | 82 +++++++++---------- .../sites/streamate/StreamateModel.java | 61 ++++++++++++-- 5 files changed, 98 insertions(+), 56 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/sites/streamate/StreamateFollowedService.java b/client/src/main/java/ctbrec/ui/sites/streamate/StreamateFollowedService.java index 6aab7d5b..2c78e1d7 100644 --- a/client/src/main/java/ctbrec/ui/sites/streamate/StreamateFollowedService.java +++ b/client/src/main/java/ctbrec/ui/sites/streamate/StreamateFollowedService.java @@ -47,7 +47,7 @@ public class StreamateFollowedService extends PaginatedScheduledService { public List 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); diff --git a/client/src/main/java/ctbrec/ui/sites/streamate/StreamateUpdateService.java b/client/src/main/java/ctbrec/ui/sites/streamate/StreamateUpdateService.java index 37ea045c..083a2008 100644 --- a/client/src/main/java/ctbrec/ui/sites/streamate/StreamateUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/streamate/StreamateUpdateService.java @@ -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 diff --git a/common/src/main/java/ctbrec/sites/streamate/Streamate.java b/common/src/main/java/ctbrec/sites/streamate/Streamate.java index 390767f0..a86eb91b 100644 --- a/common/src/main/java/ctbrec/sites/streamate/Streamate.java +++ b/common/src/main/java/ctbrec/sites/streamate/Streamate.java @@ -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 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); diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java b/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java index d1242dac..b901b385 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateHttpClient.java @@ -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 - 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[]", ) // 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()); - // } - // } + 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; + } + } else { + return false; + } + } catch(Exception e) { + return false; + } } public String getSaKey() { return saKey; } - public String getUserId() { + public Long getUserId() { return userId; } } diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java index 66e58376..4ea1b54b 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java @@ -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 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); + } +} \ No newline at end of file