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>
<groupId>ctbrec</groupId>
<artifactId>master</artifactId>
<version>3.10.0</version>
<version>3.10.1</version>
<relativePath>../master</relativePath>
</parent>

View File

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

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.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;
}
}
} 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();
@ -151,8 +146,10 @@ public class BongaCamsModel extends AbstractModel {
if (failFast) {
return onlineState;
} else {
if(onlineState == UNKNOWN) {
return online ? ONLINE : OFFLINE;
try {
isOnline(true);
} catch (IOException | ExecutionException | InterruptedException e) {
onlineState = OFFLINE;
}
return onlineState;
}