forked from j62/ctbrec
1
0
Fork 0

Code cleanup

This commit is contained in:
0xboobface 2019-12-28 19:54:04 +01:00
parent 963b5ea89b
commit a9d87bfd99
3 changed files with 43 additions and 38 deletions

View File

@ -130,7 +130,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
throw new HttpException(response.code(), response.message());
}
} catch (Exception e) {
LOG.debug("Couldn't download HLS playlist (try {}) {} - {}", tries, e.getMessage(), segmentsURL);
LOG.debug("Couldn't download HLS playlist (try {}) {} - [{}]", tries, e.getMessage(), segmentsURL);
lastException = e;
}
waitSomeTime(100 * tries);

View File

@ -21,6 +21,7 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@ -52,13 +53,13 @@ public class HlsDownload extends AbstractHlsDownload {
private static final Logger LOG = LoggerFactory.getLogger(HlsDownload.class);
protected Path downloadDir;
protected transient Path downloadDir;
private int segmentCounter = 1;
private NumberFormat nf = new DecimalFormat("000000");
private Object downloadFinished = new Object();
private transient Object downloadFinished = new Object();
private ZonedDateTime splitRecStartTime;
private Config config;
private transient Config config;
public HlsDownload(HttpClient client) {
super(client);
@ -110,20 +111,7 @@ public class HlsDownload extends AbstractHlsDownload {
URL segmentUrl = new URL(segment);
String prefix = nf.format(segmentCounter++);
SegmentDownload segmentDownload = new SegmentDownload(playlist, segmentUrl, downloadDir, client, prefix);
try {
downloadThreadPool.submit(segmentDownload);
if (downloadQueue.remainingCapacity() < 10) {
LOG.debug("space left in queue {}", downloadQueue.remainingCapacity());
// if the queue is running full, we might be struggling with timeouts
// let's check, if the model is still online
if (!model.isOnline(true)) {
downloadQueue.clear();
internalStop();
}
}
} catch(RejectedExecutionException e) {
LOG.warn("Download queue is full ({}). Skipping segment {} {}", downloadQueue.size(), prefix, segmentUrl);
}
enqueueDownload(segmentDownload, prefix, segmentUrl);
}
}
@ -190,6 +178,23 @@ public class HlsDownload extends AbstractHlsDownload {
}
}
private void enqueueDownload(SegmentDownload segmentDownload, String prefix, URL segmentUrl) throws IOException, ExecutionException, InterruptedException {
try {
downloadThreadPool.submit(segmentDownload);
if (downloadQueue.remainingCapacity() < 10) {
LOG.debug("space left in queue {}", downloadQueue.remainingCapacity());
// if the queue is running full, we might be struggling with timeouts
// let's check, if the model is still online
if (!model.isOnline(true)) {
downloadQueue.clear();
internalStop();
}
}
} catch(RejectedExecutionException e) {
LOG.warn("Download queue is full ({}). Skipping segment {} {}", downloadQueue.size(), prefix, segmentUrl);
}
}
@Override
public void postprocess(Recording recording) {
Thread.currentThread().setName("PP " + model.getName());

View File

@ -37,11 +37,12 @@ import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ChaturbateModel extends AbstractModel {
public class ChaturbateModel extends AbstractModel { // NOSONAR
private static final String PUBLIC = "public";
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateModel.class);
private int[] resolution = new int[2];
private StreamInfo streamInfo;
private transient StreamInfo streamInfo;
private long streamInfoTimestamp = 0;
private static Semaphore requestThrottle = new Semaphore(2, true);
private static long lastRequest = 0;
@ -67,7 +68,7 @@ public class ChaturbateModel extends AbstractModel {
StreamInfo info = getStreamInfo(true);
roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse("");
}
return Objects.equals("public", roomStatus);
return Objects.equals(PUBLIC, roomStatus);
}
@Override
@ -106,16 +107,17 @@ public class ChaturbateModel extends AbstractModel {
streamInfo = loadStreamInfo();
setOnlineStateByRoomStatus(streamInfo.room_status);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ExecutionException(e);
}
}
return onlineState;
}
private void setOnlineStateByRoomStatus(String room_status) {
if(room_status != null) {
switch(room_status) {
case "public":
private void setOnlineStateByRoomStatus(String roomStatus) {
if (roomStatus != null) {
switch (roomStatus) {
case PUBLIC:
onlineState = ONLINE;
break;
case "offline":
@ -133,7 +135,7 @@ public class ChaturbateModel extends AbstractModel {
onlineState = State.GROUP;
break;
default:
LOG.debug("Unknown show type {}", room_status);
LOG.debug("Unknown show type {}", roomStatus);
onlineState = State.UNKNOWN;
}
}
@ -145,7 +147,7 @@ public class ChaturbateModel extends AbstractModel {
RequestBody body = new FormBody.Builder()
.add("csrfmiddlewaretoken", ((ChaturbateHttpClient)getSite().getHttpClient()).getToken())
.add("tip_amount", Integer.toString(tokens.intValue()))
.add("tip_room_type", "public")
.add("tip_room_type", PUBLIC)
.build();
Request req = new Request.Builder()
.url("https://chaturbate.com/tipping/send_tip/"+getName()+"/")
@ -243,11 +245,11 @@ public class ChaturbateModel extends AbstractModel {
}
}
private StreamInfo getStreamInfo() throws IOException, ExecutionException, InterruptedException {
private StreamInfo getStreamInfo() throws IOException, InterruptedException {
return getStreamInfo(false);
}
private StreamInfo getStreamInfo(boolean failFast) throws IOException, ExecutionException, InterruptedException {
private StreamInfo getStreamInfo(boolean failFast) throws IOException, InterruptedException {
if(failFast) {
return streamInfo;
} else {
@ -255,7 +257,7 @@ public class ChaturbateModel extends AbstractModel {
}
}
private StreamInfo loadStreamInfo() throws HttpException, IOException, InterruptedException {
private StreamInfo loadStreamInfo() throws IOException, InterruptedException {
long now = System.currentTimeMillis();
long streamInfoAge = now - streamInfoTimestamp;
if(streamInfo != null && streamInfoAge < 5000) {
@ -294,10 +296,9 @@ public class ChaturbateModel extends AbstractModel {
}
}
private int[] getResolution() throws ExecutionException, IOException, ParseException, PlaylistException, InterruptedException {
private int[] getResolution() throws IOException, ParseException, PlaylistException, InterruptedException {
int[] res = new int[2];
StreamInfo streamInfo = getStreamInfo();
if(!streamInfo.url.startsWith("http")) {
if(!getStreamInfo().url.startsWith("http")) {
return res;
}
@ -320,7 +321,6 @@ public class ChaturbateModel extends AbstractModel {
} catch(EOFException e) {
// the cause might be, that the playlist url in streaminfo is outdated,
// so let's remove it from cache and retry in the next iteration
streamInfo = null;
ex = e;
}
}
@ -332,12 +332,11 @@ public class ChaturbateModel extends AbstractModel {
return res;
}
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException, ExecutionException, InterruptedException {
StreamInfo streamInfo = getStreamInfo();
return getMasterPlaylist(streamInfo);
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException, InterruptedException {
return getMasterPlaylist(getStreamInfo());
}
public MasterPlaylist getMasterPlaylist(StreamInfo streamInfo) throws IOException, ParseException, PlaylistException, InterruptedException {
public MasterPlaylist getMasterPlaylist(StreamInfo streamInfo) throws IOException, ParseException, PlaylistException {
LOG.trace("Loading master playlist {}", streamInfo.url);
Request req = new Request.Builder()
.url(streamInfo.url)
@ -345,6 +344,7 @@ public class ChaturbateModel extends AbstractModel {
.build();
try (Response response = getSite().getHttpClient().execute(req)) {
if (response.isSuccessful()) {
LOG.trace(response.body().string());
InputStream inputStream = response.body().byteStream();
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
Playlist playlist = parser.parse();