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