forked from j62/ctbrec
1
0
Fork 0

Improve Bongacams online state detection

This commit is contained in:
0xb00bface 2020-10-04 19:31:34 +02:00
parent e250628c96
commit 0b384cf85e
4 changed files with 44 additions and 38 deletions

View File

@ -8,7 +8,7 @@
<parent> <parent>
<groupId>ctbrec</groupId> <groupId>ctbrec</groupId>
<artifactId>master</artifactId> <artifactId>master</artifactId>
<version>3.10.0</version> <version>3.10.1</version>
<relativePath>../master</relativePath> <relativePath>../master</relativePath>
</parent> </parent>

View File

@ -18,7 +18,7 @@ public class HtmlParser {
public static Element getTag(String html, String cssSelector) { public static Element getTag(String html, String cssSelector) {
Elements selection = getTags(html, cssSelector); Elements selection = getTags(html, cssSelector);
if (selection.size() == 0) { 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(); Element tag = selection.first();
return tag; return tag;
@ -40,7 +40,7 @@ public class HtmlParser {
Document doc = Jsoup.parse(html); Document doc = Jsoup.parse(html);
Elements selection = doc.select(cssSelector); Elements selection = doc.select(cssSelector);
if (selection.size() == 0) { 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(); Element elem = selection.first();
return elem.text(); return elem.text();

View File

@ -0,0 +1,9 @@
package ctbrec.io;
public class HtmlParserException extends RuntimeException {
public HtmlParserException(String msg) {
super(msg);
}
}

View File

@ -13,7 +13,6 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -33,6 +32,7 @@ import com.iheartradio.m3u8.data.StreamInfo;
import ctbrec.AbstractModel; import ctbrec.AbstractModel;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.io.HtmlParser; import ctbrec.io.HtmlParser;
import ctbrec.io.HtmlParserException;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.recorder.download.StreamSource; import ctbrec.recorder.download.StreamSource;
import okhttp3.FormBody; import okhttp3.FormBody;
@ -55,49 +55,44 @@ public class BongaCamsModel extends AbstractModel {
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if (ignoreCache) { if (ignoreCache) {
JSONObject roomData = getRoomData(); String url = "https://en.bongacams.com/" + URLEncoder.encode(getDisplayName(), StandardCharsets.UTF_8.name());
//LOG.debug(roomData.toString(2)); Request req = new Request.Builder().url(url)
if (!roomData.has("performerData")) { .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
return false; .header(ACCEPT, "*")
}
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)
.header(ACCEPT_LANGUAGE, "en") .header(ACCEPT_LANGUAGE, "en")
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header(REFERER, getSite().getBaseUrl()) .header(REFERER, getSite().getBaseUrl())
.build(); .build();
try (Response resp = site.getHttpClient().execute(req)) { try (Response resp = site.getHttpClient().execute(req)) {
String body = resp.body().string(); String body = resp.body().string();
JSONObject json = new JSONObject(body); String chatType = HtmlParser.getText(body, "p.chatType");
if (json.optString(STATUS).equals(SUCCESS)) { onlineState = mapState(chatType);
JSONArray models = json.getJSONArray("models"); if (onlineState == ONLINE) {
for (int i = 0; i < models.length(); i++) { if(isStreamAvailable()) {
JSONObject model = models.getJSONObject(i); online = true;
//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;
}
}
} else { } else {
online = false; online = false;
onlineState = AWAY;
}
}
} catch (HtmlParserException e) {
online = false;
} }
} }
}
return online; 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() { private boolean isStreamAvailable() {
try { try {
String url = getStreamUrl(); String url = getStreamUrl();
@ -151,8 +146,10 @@ public class BongaCamsModel extends AbstractModel {
if (failFast) { if (failFast) {
return onlineState; return onlineState;
} else { } else {
if(onlineState == UNKNOWN) { try {
return online ? ONLINE : OFFLINE; isOnline(true);
} catch (IOException | ExecutionException | InterruptedException e) {
onlineState = OFFLINE;
} }
return onlineState; return onlineState;
} }