diff --git a/common/src/main/java/ctbrec/sites/cam4/Cam4Model.java b/common/src/main/java/ctbrec/sites/cam4/Cam4Model.java index 299ea1df..4287b596 100644 --- a/common/src/main/java/ctbrec/sites/cam4/Cam4Model.java +++ b/common/src/main/java/ctbrec/sites/cam4/Cam4Model.java @@ -25,6 +25,7 @@ import com.iheartradio.m3u8.PlaylistParser; import com.iheartradio.m3u8.data.MasterPlaylist; import com.iheartradio.m3u8.data.Playlist; import com.iheartradio.m3u8.data.PlaylistData; +import com.iheartradio.m3u8.data.StreamInfo; import ctbrec.AbstractModel; import ctbrec.Config; @@ -38,31 +39,31 @@ import okhttp3.Response; public class Cam4Model extends AbstractModel { - private static final transient Logger LOG = LoggerFactory.getLogger(Cam4Model.class); + private static final Logger LOG = LoggerFactory.getLogger(Cam4Model.class); private String playlistUrl; private int[] resolution = null; private boolean privateRoom = false; @Override public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { - if(ignoreCache || onlineState == UNKNOWN) { + if (ignoreCache || onlineState == UNKNOWN) { try { loadModelDetails(); } catch (ModelDetailsEmptyException e) { return false; } } - return onlineState == ONLINE && !privateRoom; + return onlineState == ONLINE && !privateRoom && playlistUrl != null && !playlistUrl.isEmpty(); } private void loadModelDetails() throws IOException, ModelDetailsEmptyException { String url = site.getBaseUrl() + "/getBroadcasting?usernames=" + getName(); LOG.trace("Loading model details {}", url); Request req = new Request.Builder().url(url).build(); - try(Response response = site.getHttpClient().execute(req)) { - if(response.isSuccessful()) { + try (Response response = site.getHttpClient().execute(req)) { + if (response.isSuccessful()) { JSONArray json = new JSONArray(response.body().string()); - if(json.length() == 0) { + if (json.length() == 0) { onlineState = OFFLINE; throw new ModelDetailsEmptyException("Model details are empty"); } @@ -71,13 +72,13 @@ public class Cam4Model extends AbstractModel { setOnlineStateByShowType(showType); playlistUrl = details.getString("hlsPreviewUrl"); privateRoom = details.getBoolean("privateRoom"); - if(privateRoom) { + if (privateRoom) { onlineState = PRIVATE; } - if(details.has("resolution")) { + if (details.has("resolution")) { String res = details.getString("resolution"); String[] tokens = res.split(":"); - resolution = new int[] {Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])}; + resolution = new int[] { Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]) }; } } else { throw new HttpException(response.code(), response.message()); @@ -116,7 +117,7 @@ public class Cam4Model extends AbstractModel { try { loadModelDetails(); } catch (ModelDetailsEmptyException e) { - LOG.warn("Couldn't load model details", e.getMessage()); + LOG.warn("Couldn't load model details {}", e.getMessage()); } } return onlineState; @@ -124,9 +125,12 @@ public class Cam4Model extends AbstractModel { } private String getPlaylistUrl() throws IOException { - if(playlistUrl == null) { + if(playlistUrl == null || playlistUrl.trim().isEmpty()) { try { loadModelDetails(); + if (playlistUrl == null) { + throw new IOException("Couldn't determine playlist url"); + } } catch (ModelDetailsEmptyException e) { throw new IOException(e); } @@ -142,7 +146,7 @@ public class Cam4Model extends AbstractModel { if (playlist.hasStreamInfo()) { StreamSource src = new StreamSource(); src.bandwidth = playlist.getStreamInfo().getBandwidth(); - src.height = Optional.ofNullable(playlist.getStreamInfo()).map(si -> si.getResolution()).map(res -> res.height).orElse(0); + src.height = Optional.ofNullable(playlist.getStreamInfo()).map(StreamInfo::getResolution).map(res -> res.height).orElse(0); String masterUrl = getPlaylistUrl(); String baseUrl = masterUrl.substring(0, masterUrl.lastIndexOf('/') + 1); String segmentUri = baseUrl + playlist.getUri(); @@ -155,7 +159,7 @@ public class Cam4Model extends AbstractModel { } private MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException { - LOG.trace("Loading master playlist {}", getPlaylistUrl()); + LOG.debug("Loading master playlist [{}]", getPlaylistUrl()); Request req = new Request.Builder().url(getPlaylistUrl()).build(); try (Response response = site.getHttpClient().execute(req)) { @@ -226,14 +230,14 @@ public class Cam4Model extends AbstractModel { // we have to use a client without any cam4 cookies here, otherwise // this request is redirected to the login page. no idea why - try(Response response = site.getRecorder().getHttpClient().execute(req)) { + try (Response response = site.getRecorder().getHttpClient().execute(req)) { String broadCasterId = null; - if(response.isSuccessful()) { + if (response.isSuccessful()) { String content = response.body().string(); try { Element tag = HtmlParser.getTag(content, "input[name=\"broadcasterId\"]"); broadCasterId = tag.attr("value"); - } catch(Exception e) { + } catch (Exception e) { LOG.debug(content); throw new IOException(e); } @@ -250,12 +254,12 @@ public class Cam4Model extends AbstractModel { .post(body) .addHeader("X-Requested-With", "XMLHttpRequest") .build(); - Response resp = site.getHttpClient().execute(req); - if(resp.isSuccessful()) { - return Objects.equals(resp.body().string(), "Ok"); - } else { - resp.close(); - return false; + try (Response resp = site.getHttpClient().execute(req)) { + if (resp.isSuccessful()) { + return Objects.equals(resp.body().string(), "Ok"); + } else { + return false; + } } } else { return false;