From 2a6d0fb617d968270a5b2c0b78db08b72eb1eade Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sat, 20 Feb 2021 11:28:21 +0100 Subject: [PATCH] Don't print HTML parsing worning for offline Bongacams models --- .../src/main/java/ctbrec/io/HtmlParser.java | 6 +++-- .../ctbrec/sites/bonga/BongaCamsModel.java | 25 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/ctbrec/io/HtmlParser.java b/common/src/main/java/ctbrec/io/HtmlParser.java index 09da2598..714718d9 100644 --- a/common/src/main/java/ctbrec/io/HtmlParser.java +++ b/common/src/main/java/ctbrec/io/HtmlParser.java @@ -7,6 +7,8 @@ import org.jsoup.select.Elements; public class HtmlParser { + private HtmlParser() {} + /** * Returns the tag selected by the given selector or null * @@ -17,7 +19,7 @@ public class HtmlParser { */ public static Element getTag(String html, String cssSelector) { Elements selection = getTags(html, cssSelector); - if (selection.size() == 0) { + if (selection.isEmpty()) { throw new HtmlParserException("Bad selector. No element selected by " + cssSelector); } Element tag = selection.first(); @@ -39,7 +41,7 @@ public class HtmlParser { public static String getText(String html, String cssSelector) { Document doc = Jsoup.parse(html); Elements selection = doc.select(cssSelector); - if (selection.size() == 0) { + if (selection.isEmpty()) { throw new HtmlParserException("Bad selector. No element selected by " + cssSelector); } Element elem = selection.first(); diff --git a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java index d2b83ea6..91a14fec 100644 --- a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java +++ b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java @@ -56,6 +56,7 @@ public class BongaCamsModel extends AbstractModel { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { if (ignoreCache) { String url = "https://en.bongacams.com/" + URLEncoder.encode(getName(), StandardCharsets.UTF_8.name()); + LOG.debug(url); Request req = new Request.Builder().url(url) .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .header(ACCEPT, "*") @@ -64,10 +65,10 @@ public class BongaCamsModel extends AbstractModel { .build(); try (Response resp = site.getHttpClient().execute(req)) { String body = resp.body().string(); - String chatType = HtmlParser.getText(body, "p.chatType"); + String chatType = parseChatType(body); onlineState = mapState(chatType); if (onlineState == ONLINE) { - if(isStreamAvailable()) { + if (isStreamAvailable()) { online = true; } else { online = false; @@ -76,14 +77,23 @@ public class BongaCamsModel extends AbstractModel { } else { online = false; } - } catch (HtmlParserException e) { - LOG.warn("Online check didn't work", e); - online = false; } } return online; } + private String parseChatType(String body) { + String chatType = ""; + if (body.contains("chatType")) { + try { + chatType = HtmlParser.getText(body, ".chatType"); + } catch (HtmlParserException e) { + LOG.warn("Online check didn't work", e); + } + } + return chatType; + } + private State mapState(String chatType) { if (chatType.matches(".*? is in a public chat")) { return ONLINE; @@ -151,7 +161,10 @@ public class BongaCamsModel extends AbstractModel { } else { try { isOnline(true); - } catch (IOException | ExecutionException | InterruptedException e) { + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + onlineState = OFFLINE; + } catch (IOException | ExecutionException e) { onlineState = OFFLINE; } return onlineState;