forked from j62/ctbrec
1
0
Fork 0

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