From 1350dce14fd3a4b3e1e1bef0179f95f01374adc6 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sat, 3 Oct 2020 16:14:24 +0200 Subject: [PATCH] Fix BongaCams online check Fix Bongacams unfollow --- .../sites/bonga/BongaCamsUpdateService.java | 37 ++++--------------- .../main/java/ctbrec/ui/tabs/ThumbCell.java | 6 ++- .../ctbrec/sites/bonga/BongaCamsModel.java | 37 +++++++++++++------ 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/sites/bonga/BongaCamsUpdateService.java b/client/src/main/java/ctbrec/ui/sites/bonga/BongaCamsUpdateService.java index 72abc4ef..ff611ec3 100644 --- a/client/src/main/java/ctbrec/ui/sites/bonga/BongaCamsUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/bonga/BongaCamsUpdateService.java @@ -1,6 +1,5 @@ package ctbrec.ui.sites.bonga; -import static ctbrec.Model.State.*; import static ctbrec.io.HttpConstants.*; import java.io.IOException; @@ -54,45 +53,23 @@ public class BongaCamsUpdateService extends PaginatedScheduledService { String content = response.body().string(); List models = new ArrayList<>(); JSONObject json = new JSONObject(content); - if(json.optString("status").equals("success")) { + //LOG.debug(json.toString(2)); + if (json.optString("status").equals("success")) { JSONArray jsonModels = json.getJSONArray("models"); for (int i = 0; i < jsonModels.length(); i++) { JSONObject m = jsonModels.getJSONObject(i); String name = m.optString("username"); - if(name.isEmpty()) { + if (name.isEmpty()) { continue; } BongaCamsModel model = (BongaCamsModel) bongaCams.createModel(name); - boolean away = m.optBoolean("is_away"); - boolean online = m.optBoolean("online"); - model.setOnline(online); - - if(online) { - model.setOnlineState(ONLINE); - if(away) { - model.setOnlineState(AWAY); - } else { - switch(m.optString("room")) { - case "private": - case "fullprivate": - model.setOnlineState(PRIVATE); - break; - case "group": - case "public": - model.setOnlineState(ONLINE); - break; - default: - LOG.debug(m.optString("room")); - model.setOnlineState(ONLINE); - } - } - } else { - model.setOnlineState(OFFLINE); - } + model.mapOnlineState(m.optString("room")); + model.setOnline(m.optInt("viewers") > 0); model.setPreview("https:" + m.getString("thumb_image")); - if(m.has("display_name")) { + if (m.has("display_name")) { model.setDisplayName(m.getString("display_name")); } + model.setDescription(m.optString("topic")); models.add(model); } } diff --git a/client/src/main/java/ctbrec/ui/tabs/ThumbCell.java b/client/src/main/java/ctbrec/ui/tabs/ThumbCell.java index af438e20..e50d6838 100644 --- a/client/src/main/java/ctbrec/ui/tabs/ThumbCell.java +++ b/client/src/main/java/ctbrec/ui/tabs/ThumbCell.java @@ -285,8 +285,6 @@ public class ThumbCell extends StackPane { try { resolution = resolutionCache.get(model); resolutionBackgroundColor = resolutionOnlineColor; - LOG.trace("Model resolution {} {}x{}", model.getName(), resolution[0], resolution[1]); - LOG.trace("Resolution queue size: {}", ThumbOverviewTab.queue.size()); final int w = resolution[1]; String width = w != Integer.MAX_VALUE ? Integer.toString(w) : "HD"; tagText = width; @@ -297,6 +295,10 @@ public class ThumbCell extends StackPane { resolutionCache.invalidate(model); } else { resolutionBackgroundColor = resolutionOfflineColor; + if (state == ONLINE) { + // state can't be ONLINE while the model is offline + tagText = OFFLINE.name(); + } } } else { State state = model.getOnlineState(true); diff --git a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java index f2676af5..9be4b119 100644 --- a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java +++ b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java @@ -32,7 +32,6 @@ import com.iheartradio.m3u8.data.StreamInfo; import ctbrec.AbstractModel; import ctbrec.Config; -import ctbrec.Model; import ctbrec.io.HtmlParser; import ctbrec.io.HttpException; import ctbrec.recorder.download.StreamSource; @@ -57,6 +56,7 @@ public class BongaCamsModel extends AbstractModel { 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; } @@ -64,7 +64,7 @@ public class BongaCamsModel extends AbstractModel { 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.trace("Online Check: {}", url); + //LOG.debug("Online Check: {}", url); Request req = new Request.Builder() .url(url) .header(USER_AGENT, Config.getInstance().getSettings().httpUserAgentMobile) @@ -75,22 +75,18 @@ public class BongaCamsModel extends AbstractModel { .build(); try (Response resp = site.getHttpClient().execute(req)) { String body = resp.body().string(); - LOG.trace(body); 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())) { - boolean away = model.optBoolean("is_away"); String room = model.optString("room"); - online = !away - && model.optBoolean("online") - && room.equalsIgnoreCase("public") - && isStreamAvailable(); - onlineState = Model.State.ONLINE; + mapOnlineState(room); + online = online && isStreamAvailable(); break; } } @@ -197,7 +193,7 @@ public class BongaCamsModel extends AbstractModel { private String getStreamUrl() throws IOException { JSONObject roomData = getRoomData(); - if(roomData.optString(STATUS).equals(SUCCESS)) { + if (roomData.optString(STATUS).equals(SUCCESS)) { JSONObject localData = roomData.getJSONObject("localData"); String server = localData.getString("videoServerUrl"); return "https:" + server + "/hls/stream_" + getName() + "/playlist.m3u8"; @@ -261,8 +257,10 @@ public class BongaCamsModel extends AbstractModel { } catch (InterruptedException e) { Thread.currentThread().interrupt(); LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); + resolution = new int[2]; } catch (ExecutionException | IOException | ParseException | PlaylistException e) { LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); + resolution = new int[2]; } return resolution; } else { @@ -335,7 +333,7 @@ public class BongaCamsModel extends AbstractModel { throw new IOException("Not logged in"); } - String url = getSite().getBaseUrl() + "/unfollow/" + getName() + '/' + getUserId(); + String url = getSite().getBaseUrl() + "/unfollow/" + getName(); LOG.debug("Calling {}", url); RequestBody body = new FormBody.Builder() .add("_csrf_token", getCsrfToken()) @@ -377,4 +375,21 @@ public class BongaCamsModel extends AbstractModel { public void setUserId(int userId) { this.userId = userId; } + + public void mapOnlineState(String roomState) { + switch (roomState) { + case "private": + case "fullprivate": + setOnlineState(PRIVATE); + break; + case "group": + case "public": + setOnlineState(ONLINE); + setOnline(true); + break; + default: + LOG.debug(roomState); + setOnlineState(OFFLINE); + } + } }