diff --git a/client/pom.xml b/client/pom.xml index 5f7f04d4..e56329e5 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -8,7 +8,7 @@ ctbrec master - 3.10.0 + 3.10.1 ../master diff --git a/common/src/main/java/ctbrec/io/HtmlParser.java b/common/src/main/java/ctbrec/io/HtmlParser.java index 121d494c..09da2598 100644 --- a/common/src/main/java/ctbrec/io/HtmlParser.java +++ b/common/src/main/java/ctbrec/io/HtmlParser.java @@ -18,7 +18,7 @@ public class HtmlParser { public static Element getTag(String html, String cssSelector) { Elements selection = getTags(html, cssSelector); if (selection.size() == 0) { - throw new RuntimeException("Bad selector. No element selected by " + cssSelector); + throw new HtmlParserException("Bad selector. No element selected by " + cssSelector); } Element tag = selection.first(); return tag; @@ -40,7 +40,7 @@ public class HtmlParser { Document doc = Jsoup.parse(html); Elements selection = doc.select(cssSelector); if (selection.size() == 0) { - throw new RuntimeException("Bad selector. No element selected by " + cssSelector); + throw new HtmlParserException("Bad selector. No element selected by " + cssSelector); } Element elem = selection.first(); return elem.text(); diff --git a/common/src/main/java/ctbrec/io/HtmlParserException.java b/common/src/main/java/ctbrec/io/HtmlParserException.java new file mode 100644 index 00000000..5afb0b7d --- /dev/null +++ b/common/src/main/java/ctbrec/io/HtmlParserException.java @@ -0,0 +1,9 @@ +package ctbrec.io; + +public class HtmlParserException extends RuntimeException { + + public HtmlParserException(String msg) { + super(msg); + } + +} diff --git a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java index 38ea5b85..6268abb3 100644 --- a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java +++ b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java @@ -13,7 +13,6 @@ import java.util.List; import java.util.Locale; import java.util.concurrent.ExecutionException; -import org.json.JSONArray; import org.json.JSONObject; import org.jsoup.nodes.Element; import org.slf4j.Logger; @@ -33,6 +32,7 @@ import com.iheartradio.m3u8.data.StreamInfo; import ctbrec.AbstractModel; import ctbrec.Config; import ctbrec.io.HtmlParser; +import ctbrec.io.HtmlParserException; import ctbrec.io.HttpException; import ctbrec.recorder.download.StreamSource; import okhttp3.FormBody; @@ -55,49 +55,44 @@ public class BongaCamsModel extends AbstractModel { @Override public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { if (ignoreCache) { - JSONObject roomData = getRoomData(); - //LOG.debug(roomData.toString(2)); - if (!roomData.has("performerData")) { - return false; - } - JSONObject performerData = roomData.getJSONObject("performerData"); - setDisplayName(performerData.optString("displayName")); - String url = BongaCams.baseUrl + "/tools/listing_v3.php?livetab=&online_only=true&offset=0&model_search%5Bdisplay_name%5D%5Btext%5D=" - + URLEncoder.encode(getDisplayName(), StandardCharsets.UTF_8.name()) + "&_online_filter=0"; - //LOG.debug("Online Check: {}", url); - Request req = new Request.Builder() - .url(url) - .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgentMobile) - .header(ACCEPT, MIMETYPE_APPLICATION_JSON) + String url = "https://en.bongacams.com/" + URLEncoder.encode(getDisplayName(), StandardCharsets.UTF_8.name()); + Request req = new Request.Builder().url(url) + .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) + .header(ACCEPT, "*") .header(ACCEPT_LANGUAGE, "en") - .header(X_REQUESTED_WITH, XML_HTTP_REQUEST) .header(REFERER, getSite().getBaseUrl()) .build(); try (Response resp = site.getHttpClient().execute(req)) { String body = resp.body().string(); - JSONObject json = new JSONObject(body); - if (json.optString(STATUS).equals(SUCCESS)) { - JSONArray models = json.getJSONArray("models"); - for (int i = 0; i < models.length(); i++) { - JSONObject model = models.getJSONObject(i); - //LOG.debug(model.toString(2)); - setDescription(model.optString("topic")); - String username = model.optString("username"); - if (username.equalsIgnoreCase(getName())) { - String room = model.optString("room"); - mapOnlineState(room); - online = online && isStreamAvailable(); - break; - } + String chatType = HtmlParser.getText(body, "p.chatType"); + onlineState = mapState(chatType); + if (onlineState == ONLINE) { + if(isStreamAvailable()) { + online = true; + } else { + online = false; + onlineState = AWAY; } - } else { - online = false; } + } catch (HtmlParserException e) { + online = false; } } return online; } + private State mapState(String chatType) { + if (chatType.matches(".*? is in a public chat")) { + return ONLINE; + } else if (chatType.matches(".*? is in a group chat")) { + return GROUP; + } else if (chatType.matches(".*? is in a private chat")) { + return PRIVATE; + } else { + return OFFLINE; + } + } + private boolean isStreamAvailable() { try { String url = getStreamUrl(); @@ -148,11 +143,13 @@ public class BongaCamsModel extends AbstractModel { @Override public State getOnlineState(boolean failFast) throws IOException, ExecutionException { - if(failFast) { + if (failFast) { return onlineState; } else { - if(onlineState == UNKNOWN) { - return online ? ONLINE : OFFLINE; + try { + isOnline(true); + } catch (IOException | ExecutionException | InterruptedException e) { + onlineState = OFFLINE; } return onlineState; }