forked from j62/ctbrec
1
0
Fork 0

Cache Chaturbate stream info for 2 seconds to reduce requests

This commit is contained in:
0xb00bface 2021-07-25 14:56:48 +02:00
parent ff0864bbf7
commit 27caa0098a
1 changed files with 12 additions and 10 deletions

View File

@ -8,6 +8,8 @@ import java.io.ByteArrayInputStream;
import java.io.EOFException; import java.io.EOFException;
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.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -45,6 +47,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateModel.class); private static final Logger LOG = LoggerFactory.getLogger(ChaturbateModel.class);
private int[] resolution = new int[2]; private int[] resolution = new int[2];
private transient StreamInfo streamInfo; private transient StreamInfo streamInfo;
private transient Instant lastStreamInfoRequest = Instant.EPOCH;
/** /**
* This constructor exists only for deserialization. Please don't call it directly * This constructor exists only for deserialization. Please don't call it directly
@ -59,7 +62,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
String roomStatus; String roomStatus;
if(ignoreCache) { if (ignoreCache) {
StreamInfo info = loadStreamInfo(); StreamInfo info = loadStreamInfo();
roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse(""); roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse("");
LOG.trace("Model {} room status: {}", getName(), info.room_status); LOG.trace("Model {} room status: {}", getName(), info.room_status);
@ -99,16 +102,11 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
@Override @Override
public State getOnlineState(boolean failFast) throws IOException, ExecutionException { public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) { if (failFast) {
setOnlineStateByRoomStatus(Optional.ofNullable(streamInfo).map(si -> si.room_status).orElse("Unknown")); setOnlineStateByRoomStatus(Optional.ofNullable(streamInfo).map(si -> si.room_status).orElse("Unknown"));
} else { } else {
try { streamInfo = loadStreamInfo();
streamInfo = loadStreamInfo(); setOnlineStateByRoomStatus(streamInfo.room_status);
setOnlineStateByRoomStatus(streamInfo.room_status);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ExecutionException(e);
}
} }
return onlineState; return onlineState;
} }
@ -259,7 +257,10 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
} }
} }
private StreamInfo loadStreamInfo() throws IOException, InterruptedException { private StreamInfo loadStreamInfo() throws IOException {
if (Duration.between(lastStreamInfoRequest, Instant.now()).getSeconds() < 2) {
return streamInfo;
}
RequestBody body = new FormBody.Builder() RequestBody body = new FormBody.Builder()
.add("room_slug", getName()) .add("room_slug", getName())
.add("bandwidth", "high") .add("bandwidth", "high")
@ -271,6 +272,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST) .header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.build(); .build();
try (Response response = getSite().getHttpClient().execute(req)) { try (Response response = getSite().getHttpClient().execute(req)) {
lastStreamInfoRequest = Instant.now();
if (response.isSuccessful()) { if (response.isSuccessful()) {
String content = response.body().string(); String content = response.body().string();
LOG.trace("Raw stream info: {}", content); LOG.trace("Raw stream info: {}", content);