Some tweaks to BongaCamsModel

This commit is contained in:
0xb00bface 2023-12-30 22:12:21 +01:00
parent 531b7b698f
commit 9b45896961
1 changed files with 30 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import com.iheartradio.m3u8.*;
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;
import ctbrec.io.HtmlParser;
@ -21,6 +20,8 @@ import org.jsoup.nodes.Element;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
@ -44,6 +45,9 @@ public class BongaCamsModel extends AbstractModel {
private final transient List<StreamSource> streamSources = new ArrayList<>();
private int[] resolution;
private transient JSONObject modelInfo;
private transient Instant lastInfoRequest = Instant.EPOCH;
@Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if (ignoreCache) {
@ -139,6 +143,10 @@ public class BongaCamsModel extends AbstractModel {
}
private JSONObject getRoomData() throws IOException {
if (Objects.nonNull(modelInfo) && Duration.between(lastInfoRequest, Instant.now()).getSeconds() < 5) {
return modelInfo;
}
lastInfoRequest = Instant.now();
String url = getSite().getBaseUrl() + "/tools/amf.php";
RequestBody body = new FormBody.Builder()
.add("method", "getRoomData")
@ -154,6 +162,7 @@ public class BongaCamsModel extends AbstractModel {
try (Response response = site.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
JSONObject json = new JSONObject(Objects.requireNonNull(response.body(), HTTP_RESPONSE_BODY_IS_NULL).string());
modelInfo = json;
return json;
} else {
throw new IOException(response.code() + " " + response.message());
@ -196,20 +205,19 @@ public class BongaCamsModel extends AbstractModel {
private void extractStreamSources(String streamUrl, MasterPlaylist master) {
streamSources.clear();
for (PlaylistData playlistData : master.getPlaylists()) {
StreamSource streamsource = new StreamSource();
streamsource.setMediaPlaylistUrl(streamUrl.replace("playlist.m3u8", playlistData.getUri()));
if (playlistData.hasStreamInfo()) {
StreamInfo info = playlistData.getStreamInfo();
streamsource.setBandwidth(info.getBandwidth());
streamsource.setWidth(info.hasResolution() ? info.getResolution().width : 0);
streamsource.setHeight(info.hasResolution() ? info.getResolution().height : 0);
for (PlaylistData playlist : master.getPlaylists()) {
StreamSource src = new StreamSource();
src.setMediaPlaylistUrl(streamUrl.replace("playlist.m3u8", playlist.getUri()));
if (playlist.hasStreamInfo()) {
src.setBandwidth(playlist.getStreamInfo().getBandwidth());
src.setHeight(Optional.ofNullable(playlist.getStreamInfo().getResolution()).map(res -> res.height).orElse(0));
src.setWidth(Optional.ofNullable(playlist.getStreamInfo().getResolution()).map(res -> res.width).orElse(0));
} else {
streamsource.setBandwidth(0);
streamsource.setWidth(0);
streamsource.setHeight(0);
src.setBandwidth(0);
src.setWidth(0);
src.setHeight(0);
}
streamSources.add(streamsource);
streamSources.add(src);
}
}
@ -227,6 +235,8 @@ public class BongaCamsModel extends AbstractModel {
@Override
public void invalidateCacheEntries() {
resolution = null;
modelInfo = null;
lastInfoRequest = Instant.EPOCH;
}
@Override
@ -272,7 +282,7 @@ public class BongaCamsModel extends AbstractModel {
}
List<StreamSource> sources = getStreamSources();
Collections.sort(sources);
StreamSource best = sources.get(sources.size() - 1);
StreamSource best = sources.getLast();
resolution = new int[]{best.getWidth(), best.getHeight()};
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
@ -396,6 +406,12 @@ public class BongaCamsModel extends AbstractModel {
}
}
@Override
public Instant getLastSeen() {
Instant lastSeen = super.getLastSeen();
return (lastSeen.equals(Instant.EPOCH)) ? getAddedTimestamp() : lastSeen;
}
private Request.Builder newRequestBuilder() {
return new Request.Builder()
.url(getUrl())