Improve playlist loading for Cam4Model

Also improve isOnline to take into account, if a playlist url is
available or not
This commit is contained in:
0xboobface 2019-12-26 23:44:26 +01:00
parent 55760a1b7d
commit 33c7c6606d
1 changed files with 26 additions and 22 deletions

View File

@ -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,7 +39,7 @@ 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;
@ -52,7 +53,7 @@ public class Cam4Model extends AbstractModel {
return false;
}
}
return onlineState == ONLINE && !privateRoom;
return onlineState == ONLINE && !privateRoom && playlistUrl != null && !playlistUrl.isEmpty();
}
private void loadModelDetails() throws IOException, ModelDetailsEmptyException {
@ -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)) {
@ -250,13 +254,13 @@ public class Cam4Model extends AbstractModel {
.post(body)
.addHeader("X-Requested-With", "XMLHttpRequest")
.build();
Response resp = site.getHttpClient().execute(req);
try (Response resp = site.getHttpClient().execute(req)) {
if (resp.isSuccessful()) {
return Objects.equals(resp.body().string(), "Ok");
} else {
resp.close();
return false;
}
}
} else {
return false;
}