Don't print HTML parsing worning for offline Bongacams models

This commit is contained in:
0xb00bface 2021-02-20 11:28:21 +01:00
parent 4721519671
commit 2a6d0fb617
2 changed files with 23 additions and 8 deletions

View File

@ -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();

View File

@ -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;