From b18a32ede3b73a8fe9be97c261b75be3cf6cab68 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sun, 16 Feb 2020 13:28:33 +0100 Subject: [PATCH] Fix LiveJasmin followed tab --- .../sites/jasmin/LiveJasminFollowedTab.java | 28 ----------- .../LiveJasminFollowedUpdateService.java | 48 +++++++------------ .../main/java/ctbrec/NotLoggedInExcetion.java | 10 +++- 3 files changed, 25 insertions(+), 61 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedTab.java b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedTab.java index 8d59dc30..bd6217c4 100644 --- a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedTab.java +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedTab.java @@ -2,13 +2,9 @@ package ctbrec.ui.sites.jasmin; import ctbrec.sites.jasmin.LiveJasmin; import ctbrec.ui.tabs.FollowedTab; -import javafx.geometry.Insets; import javafx.scene.Scene; -import javafx.scene.control.RadioButton; -import javafx.scene.control.ToggleGroup; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; -import javafx.scene.layout.HBox; public class LiveJasminFollowedTab extends LiveJasminTab implements FollowedTab { @@ -16,30 +12,6 @@ public class LiveJasminFollowedTab extends LiveJasminTab implements FollowedTab super("Followed", new LiveJasminFollowedUpdateService(liveJasmin), liveJasmin); } - @Override - protected void createGui() { - super.createGui(); - addOnlineOfflineSelector(); - } - - private void addOnlineOfflineSelector() { - ToggleGroup group = new ToggleGroup(); - RadioButton online = new RadioButton("online"); - online.setToggleGroup(group); - RadioButton offline = new RadioButton("offline"); - offline.setToggleGroup(group); - pagination.getChildren().add(online); - pagination.getChildren().add(offline); - HBox.setMargin(online, new Insets(5,5,5,40)); - HBox.setMargin(offline, new Insets(5,5,5,5)); - online.setSelected(true); - group.selectedToggleProperty().addListener((e) -> { - ((LiveJasminFollowedUpdateService)updateService).setShowOnline(online.isSelected()); - queue.clear(); - updateService.restart(); - }); - } - @Override public void setScene(Scene scene) { scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> { diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedUpdateService.java b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedUpdateService.java index ab2e1c3d..c183a9fc 100644 --- a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminFollowedUpdateService.java @@ -1,5 +1,6 @@ package ctbrec.ui.sites.jasmin; +import static ctbrec.io.HtmlParser.*; import static ctbrec.io.HttpConstants.*; import java.io.IOException; @@ -7,13 +8,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; -import org.json.JSONArray; -import org.json.JSONObject; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ctbrec.Config; import ctbrec.Model; +import ctbrec.NotLoggedInExcetion; import ctbrec.io.HttpException; import ctbrec.sites.jasmin.LiveJasmin; import ctbrec.sites.jasmin.LiveJasminModel; @@ -32,8 +34,8 @@ public class LiveJasminFollowedUpdateService extends PaginatedScheduledService { public LiveJasminFollowedUpdateService(LiveJasmin liveJasmin) { this.liveJasmin = liveJasmin; - long ts = System.currentTimeMillis(); - this.url = liveJasmin.getBaseUrl() + "/en/free/favourite/get-favourite-list?_dc=" + ts; + this.url = liveJasmin.getBaseUrl() + "/en/member/favorite"; + //this.url = liveJasmin.getBaseUrl() + "/en/free/favourite/get-favourite-list?_dc=" + ts; } @Override @@ -42,51 +44,33 @@ public class LiveJasminFollowedUpdateService extends PaginatedScheduledService { @Override public List call() throws IOException { if(!liveJasmin.credentialsAvailable()) { - throw new RuntimeException("Credentials missing"); + throw new NotLoggedInExcetion("Credentials missing"); } boolean loggedIn = SiteUiFactory.getUi(liveJasmin).login(); if(!loggedIn) { throw new RuntimeException("Couldn't login to livejasmin"); } - //String _url = url + ((page-1) * 36); // TODO find out how to switch pages //LOG.debug("Fetching page {}", url); Request request = new Request.Builder() .url(url) .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .header(ACCEPT, "*/*") .header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage()) - .header(REFERER, liveJasmin.getBaseUrl() + "/en/free/favorite") - .header(X_REQUESTED_WITH, XML_HTTP_REQUEST) + .header(REFERER, liveJasmin.getBaseUrl()) .build(); try (Response response = liveJasmin.getHttpClient().execute(request)) { if (response.isSuccessful()) { String body = response.body().string(); List models = new ArrayList<>(); - JSONObject json = new JSONObject(body); - //LOG.debug(json.toString(2)); - if(json.has("success")) { - JSONObject data = json.getJSONObject("data"); - JSONArray performers = data.getJSONArray("performers"); - for (int i = 0; i < performers.length(); i++) { - JSONObject m = performers.getJSONObject(i); - String name = m.optString("pid"); - if(name.isEmpty()) { - continue; - } - LiveJasminModel model = (LiveJasminModel) liveJasmin.createModel(name); - model.setId(m.getString("id")); - model.setPreview(m.getString("profilePictureUrl")); - Model.State onlineState = LiveJasminModel.mapStatus(m.getInt("status")); - boolean online = onlineState == Model.State.ONLINE; - model.setOnlineState(onlineState); - if(online == showOnline) { - models.add(model); - } - } - } else { - LOG.error("Request failed:\n{}", body); - throw new IOException("Response was not successful"); + Elements modelCells = getTags(body, "article[class~=perf_container]"); + for (Element modelCell : modelCells) { + String cellHtml = modelCell.html(); + String name = getText(cellHtml, "span[class~=performer_name_simple]").trim(); + LiveJasminModel model = (LiveJasminModel) liveJasmin.createModel(name); + model.setPreview(getTag(cellHtml, "img[class~=performer-image]").attr("data-jpg-src")); + model.setId(getTag(cellHtml, "span[class~=remove][class~=favorite]").attr("data-model-id")); + models.add(model); } return models; } else { diff --git a/common/src/main/java/ctbrec/NotLoggedInExcetion.java b/common/src/main/java/ctbrec/NotLoggedInExcetion.java index f3f293b0..2542bb45 100644 --- a/common/src/main/java/ctbrec/NotLoggedInExcetion.java +++ b/common/src/main/java/ctbrec/NotLoggedInExcetion.java @@ -1,5 +1,13 @@ package ctbrec; -public class NotLoggedInExcetion extends Exception { +public class NotLoggedInExcetion extends RuntimeException { + + public NotLoggedInExcetion() { + super(); + } + + public NotLoggedInExcetion(String mesg) { + super(mesg); + } }