From 086e15578f9e91488380584c2d11e89dbea273b2 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Mon, 21 Dec 2020 20:05:22 +0100 Subject: [PATCH] Core cleanup --- .../java/ctbrec/sites/manyvids/MVLive.java | 123 ++++++++++-------- .../ctbrec/sites/manyvids/MVLiveModel.java | 9 +- 2 files changed, 74 insertions(+), 58 deletions(-) diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLive.java b/common/src/main/java/ctbrec/sites/manyvids/MVLive.java index 7b7b2a78..9e49863a 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLive.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLive.java @@ -13,6 +13,8 @@ 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; @@ -28,6 +30,8 @@ import okhttp3.Response; public class MVLive extends AbstractSite { + private static final Logger LOG = LoggerFactory.getLogger(MVLive.class); + public static final String APP_HOST = "app-v1.live.manyvids.com"; public static final String WS_URL = "wss://" + APP_HOST; public static final String WS_ORIGIN = "https://live.manyvids.com"; @@ -123,46 +127,51 @@ public class MVLive extends AbstractSite { .build(); try (Response response = getHttpClient().execute(request)) { if (response.isSuccessful()) { - List models = new ArrayList<>(); String content = response.body().string(); Elements cards = HtmlParser.getTags(content, "div[class*=-model]"); - for (Element card : cards) { - try { - String cardHtml = card.html(); - Element link = HtmlParser.getTag(cardHtml, "a"); - link.setBaseUri(getBaseUrl()); - String name = HtmlParser.getText(cardHtml, "h4 a"); - MVLiveModel model = createModel(name); - model.setUrl(link.absUrl("href")); - Element thumb = HtmlParser.getTag(cardHtml, "a img.b-lazy"); - thumb.setBaseUri(getBaseUrl()); - model.setPreview(thumb.absUrl("data-src")); - - Element status = HtmlParser.getTag(cardHtml, "h4[class~=profile-pic-name]"); - String cssClass = status.attr("class"); - if(cssClass.contains("live")) { - model.setOnlineState(Model.State.ONLINE); - } else if(cssClass.contains("private")) { - model.setOnlineState(Model.State.PRIVATE); - } else { - model.setOnlineState(Model.State.UNKNOWN); - } - models.add(model); - } catch(RuntimeException e) { - if(e.getMessage().contains("No element selected by")) { - // ignore - } else { - throw e; - } - } - } - return models; + return parseModelCards(cards); } else { throw new HttpException(response.code(), response.message()); } } } + private List parseModelCards(Elements cards) { + List models = new ArrayList<>(); + for (Element card : cards) { + try { + String cardHtml = card.html(); + Element link = HtmlParser.getTag(cardHtml, "a"); + link.setBaseUri(getBaseUrl()); + String name = HtmlParser.getText(cardHtml, "h4 a"); + MVLiveModel model = createModel(name); + model.setUrl(link.absUrl("href")); + Element thumb = HtmlParser.getTag(cardHtml, "a img.b-lazy"); + thumb.setBaseUri(getBaseUrl()); + model.setPreview(thumb.absUrl("data-src")); + + Element status = HtmlParser.getTag(cardHtml, "h4[class~=profile-pic-name]"); + String cssClass = status.attr("class"); + if(cssClass.contains("live")) { + model.setOnlineState(Model.State.ONLINE); + } else if(cssClass.contains("private")) { + model.setOnlineState(Model.State.PRIVATE); + } else { + LOG.debug("Unknown online state {}", cssClass); + model.setOnlineState(Model.State.UNKNOWN); + } + models.add(model); + } catch(RuntimeException e) { + if(e.getMessage().contains("No element selected by")) { + // ignore + } else { + throw e; + } + } + } + return models; + } + @Override public boolean supportsSearch() { return true; @@ -213,29 +222,7 @@ public class MVLive extends AbstractSite { try (Response response = getHttpClient().execute(request)) { if (response.isSuccessful()) { String responseBody = response.body().string(); - JSONObject json = new JSONObject(responseBody); - if(json.has("stars")) { - JSONArray stars = json.getJSONArray("stars"); - for (int i = 0; i < stars.length(); i++) { - JSONObject star = stars.getJSONObject(i); - String name = star.getString("label"); - MVLiveModel model = createModel(name); - long id = star.getLong("id"); - String url = "https://www.manyvids.com/MVLive/" + model.getDisplayName() + '/' + id + '/'; - model.setUrl(url); - model.setPreview(star.getString("img")); - if (star.optString("is_live").equals("1")) { - if (star.optString("is_private").equals("1")) { - model.setOnlineState(State.PRIVATE); - } else { - model.setOnlineState(State.ONLINE); - } - } else { - model.setOnlineState(State.OFFLINE); - } - result.add(model); - } - } + parseSearchResult(result, responseBody); } else { throw new HttpException(response.code(), response.message()); } @@ -243,6 +230,32 @@ public class MVLive extends AbstractSite { return result; } + private void parseSearchResult(List result, String responseBody) { + JSONObject json = new JSONObject(responseBody); + if(json.has("stars")) { + JSONArray stars = json.getJSONArray("stars"); + for (int i = 0; i < stars.length(); i++) { + JSONObject star = stars.getJSONObject(i); + String name = star.getString("label"); + MVLiveModel model = createModel(name); + long id = star.getLong("id"); + String url = "https://www.manyvids.com/MVLive/" + model.getDisplayName() + '/' + id + '/'; + model.setUrl(url); + model.setPreview(star.getString("img")); + if (star.optString("is_live").equals("1")) { + if (star.optString("is_private").equals("1")) { + model.setOnlineState(State.PRIVATE); + } else { + model.setOnlineState(State.ONLINE); + } + } else { + model.setOnlineState(State.OFFLINE); + } + result.add(model); + } + } + } + @Override public Model createModelFromUrl(String url) { Matcher m = Pattern.compile("https://live.manyvids.com/(?:stream/)?(.*?)(?:/.*?)?").matcher(url.trim()); diff --git a/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java b/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java index bbca090a..a84f0a0f 100644 --- a/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java +++ b/common/src/main/java/ctbrec/sites/manyvids/MVLiveModel.java @@ -31,10 +31,12 @@ import com.iheartradio.m3u8.data.PlaylistData; import ctbrec.AbstractModel; import ctbrec.Config; import ctbrec.Model; +import ctbrec.NotImplementedExcetion; import ctbrec.StringUtil; import ctbrec.io.HttpException; import ctbrec.recorder.download.Download; import ctbrec.recorder.download.StreamSource; +import ctbrec.sites.ModelOfflineException; import okhttp3.Request; import okhttp3.Response; @@ -42,8 +44,8 @@ public class MVLiveModel extends AbstractModel { private static final transient Logger LOG = LoggerFactory.getLogger(MVLiveModel.class); - private MVLiveHttpClient httpClient; - private MVLiveClient client; + private transient MVLiveHttpClient httpClient; + private transient MVLiveClient client; private String roomNumber; @Override @@ -138,7 +140,7 @@ public class MVLiveModel extends AbstractModel { roomNumber = json.getString("floorId"); } else { LOG.debug("Room number response: {}", json.toString(2)); - throw new RuntimeException(getName() + " is offline"); + throw new ModelOfflineException(this); } } return roomNumber; @@ -194,6 +196,7 @@ public class MVLiveModel extends AbstractModel { @Override public void receiveTip(Double tokens) throws IOException { + throw new NotImplementedExcetion("Sending tips is not implemeted for MVLive"); } @Override