forked from j62/ctbrec
Code cleanup
This commit is contained in:
parent
963b5ea89b
commit
a9d87bfd99
|
@ -130,7 +130,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
|
||||||
throw new HttpException(response.code(), response.message());
|
throw new HttpException(response.code(), response.message());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} 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;
|
lastException = e;
|
||||||
}
|
}
|
||||||
waitSomeTime(100 * tries);
|
waitSomeTime(100 * tries);
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.RejectedExecutionException;
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -52,13 +53,13 @@ public class HlsDownload extends AbstractHlsDownload {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(HlsDownload.class);
|
private static final Logger LOG = LoggerFactory.getLogger(HlsDownload.class);
|
||||||
|
|
||||||
protected Path downloadDir;
|
protected transient Path downloadDir;
|
||||||
|
|
||||||
private int segmentCounter = 1;
|
private int segmentCounter = 1;
|
||||||
private NumberFormat nf = new DecimalFormat("000000");
|
private NumberFormat nf = new DecimalFormat("000000");
|
||||||
private Object downloadFinished = new Object();
|
private transient Object downloadFinished = new Object();
|
||||||
private ZonedDateTime splitRecStartTime;
|
private ZonedDateTime splitRecStartTime;
|
||||||
private Config config;
|
private transient Config config;
|
||||||
|
|
||||||
public HlsDownload(HttpClient client) {
|
public HlsDownload(HttpClient client) {
|
||||||
super(client);
|
super(client);
|
||||||
|
@ -110,20 +111,7 @@ public class HlsDownload extends AbstractHlsDownload {
|
||||||
URL segmentUrl = new URL(segment);
|
URL segmentUrl = new URL(segment);
|
||||||
String prefix = nf.format(segmentCounter++);
|
String prefix = nf.format(segmentCounter++);
|
||||||
SegmentDownload segmentDownload = new SegmentDownload(playlist, segmentUrl, downloadDir, client, prefix);
|
SegmentDownload segmentDownload = new SegmentDownload(playlist, segmentUrl, downloadDir, client, prefix);
|
||||||
try {
|
enqueueDownload(segmentDownload, prefix, segmentUrl);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
@Override
|
||||||
public void postprocess(Recording recording) {
|
public void postprocess(Recording recording) {
|
||||||
Thread.currentThread().setName("PP " + model.getName());
|
Thread.currentThread().setName("PP " + model.getName());
|
||||||
|
|
|
@ -37,11 +37,12 @@ import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
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 static final Logger LOG = LoggerFactory.getLogger(ChaturbateModel.class);
|
||||||
private int[] resolution = new int[2];
|
private int[] resolution = new int[2];
|
||||||
private StreamInfo streamInfo;
|
private transient StreamInfo streamInfo;
|
||||||
private long streamInfoTimestamp = 0;
|
private long streamInfoTimestamp = 0;
|
||||||
private static Semaphore requestThrottle = new Semaphore(2, true);
|
private static Semaphore requestThrottle = new Semaphore(2, true);
|
||||||
private static long lastRequest = 0;
|
private static long lastRequest = 0;
|
||||||
|
@ -67,7 +68,7 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
StreamInfo info = getStreamInfo(true);
|
StreamInfo info = getStreamInfo(true);
|
||||||
roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse("");
|
roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse("");
|
||||||
}
|
}
|
||||||
return Objects.equals("public", roomStatus);
|
return Objects.equals(PUBLIC, roomStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -106,16 +107,17 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
streamInfo = loadStreamInfo();
|
streamInfo = loadStreamInfo();
|
||||||
setOnlineStateByRoomStatus(streamInfo.room_status);
|
setOnlineStateByRoomStatus(streamInfo.room_status);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
throw new ExecutionException(e);
|
throw new ExecutionException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return onlineState;
|
return onlineState;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setOnlineStateByRoomStatus(String room_status) {
|
private void setOnlineStateByRoomStatus(String roomStatus) {
|
||||||
if(room_status != null) {
|
if (roomStatus != null) {
|
||||||
switch(room_status) {
|
switch (roomStatus) {
|
||||||
case "public":
|
case PUBLIC:
|
||||||
onlineState = ONLINE;
|
onlineState = ONLINE;
|
||||||
break;
|
break;
|
||||||
case "offline":
|
case "offline":
|
||||||
|
@ -133,7 +135,7 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
onlineState = State.GROUP;
|
onlineState = State.GROUP;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG.debug("Unknown show type {}", room_status);
|
LOG.debug("Unknown show type {}", roomStatus);
|
||||||
onlineState = State.UNKNOWN;
|
onlineState = State.UNKNOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +147,7 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
RequestBody body = new FormBody.Builder()
|
RequestBody body = new FormBody.Builder()
|
||||||
.add("csrfmiddlewaretoken", ((ChaturbateHttpClient)getSite().getHttpClient()).getToken())
|
.add("csrfmiddlewaretoken", ((ChaturbateHttpClient)getSite().getHttpClient()).getToken())
|
||||||
.add("tip_amount", Integer.toString(tokens.intValue()))
|
.add("tip_amount", Integer.toString(tokens.intValue()))
|
||||||
.add("tip_room_type", "public")
|
.add("tip_room_type", PUBLIC)
|
||||||
.build();
|
.build();
|
||||||
Request req = new Request.Builder()
|
Request req = new Request.Builder()
|
||||||
.url("https://chaturbate.com/tipping/send_tip/"+getName()+"/")
|
.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);
|
return getStreamInfo(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StreamInfo getStreamInfo(boolean failFast) throws IOException, ExecutionException, InterruptedException {
|
private StreamInfo getStreamInfo(boolean failFast) throws IOException, InterruptedException {
|
||||||
if(failFast) {
|
if(failFast) {
|
||||||
return streamInfo;
|
return streamInfo;
|
||||||
} else {
|
} 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 now = System.currentTimeMillis();
|
||||||
long streamInfoAge = now - streamInfoTimestamp;
|
long streamInfoAge = now - streamInfoTimestamp;
|
||||||
if(streamInfo != null && streamInfoAge < 5000) {
|
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];
|
int[] res = new int[2];
|
||||||
StreamInfo streamInfo = getStreamInfo();
|
if(!getStreamInfo().url.startsWith("http")) {
|
||||||
if(!streamInfo.url.startsWith("http")) {
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,7 +321,6 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
} catch(EOFException e) {
|
} catch(EOFException e) {
|
||||||
// the cause might be, that the playlist url in streaminfo is outdated,
|
// 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
|
// so let's remove it from cache and retry in the next iteration
|
||||||
streamInfo = null;
|
|
||||||
ex = e;
|
ex = e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -332,12 +332,11 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException, ExecutionException, InterruptedException {
|
public MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException, InterruptedException {
|
||||||
StreamInfo streamInfo = getStreamInfo();
|
return getMasterPlaylist(getStreamInfo());
|
||||||
return getMasterPlaylist(streamInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
LOG.trace("Loading master playlist {}", streamInfo.url);
|
||||||
Request req = new Request.Builder()
|
Request req = new Request.Builder()
|
||||||
.url(streamInfo.url)
|
.url(streamInfo.url)
|
||||||
|
@ -345,6 +344,7 @@ public class ChaturbateModel extends AbstractModel {
|
||||||
.build();
|
.build();
|
||||||
try (Response response = getSite().getHttpClient().execute(req)) {
|
try (Response response = getSite().getHttpClient().execute(req)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
|
LOG.trace(response.body().string());
|
||||||
InputStream inputStream = response.body().byteStream();
|
InputStream inputStream = response.body().byteStream();
|
||||||
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
||||||
Playlist playlist = parser.parse();
|
Playlist playlist = parser.parse();
|
||||||
|
|
Loading…
Reference in New Issue