forked from j62/ctbrec
1
0
Fork 0

Add own download class to manage the websocket connection

This commit is contained in:
0xboobface 2018-12-18 13:18:49 +01:00
parent 02c65bfdd1
commit 2f09b1d517
5 changed files with 51 additions and 14 deletions

View File

@ -29,6 +29,7 @@ import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.sites.fc2live.Fc2Live;
import okhttp3.Request;
import okhttp3.Response;
@ -47,9 +48,17 @@ public abstract class AbstractHlsDownload implements Download {
this.client = client;
}
SegmentPlaylist getNextSegments(String segments) throws IOException, ParseException, PlaylistException {
protected SegmentPlaylist getNextSegments(String segments) throws IOException, ParseException, PlaylistException {
URL segmentsUrl = new URL(segments);
Request request = new Request.Builder().url(segmentsUrl).addHeader("connection", "keep-alive").build();
Request request = new Request.Builder()
.url(segmentsUrl)
.header("Accept", "*/*")
.header("Accept-Language", "en-US,en;q=0.5")
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.header("Origin", Fc2Live.BASE_URL)
.header("Referer", Fc2Live.BASE_URL)
.header("Connection", "keep-alive")
.build();
try(Response response = client.execute(request)) {
if(response.isSuccessful()) {
// String body = response.body().string();
@ -69,11 +78,11 @@ public abstract class AbstractHlsDownload implements Download {
if(!uri.startsWith("http")) {
String _url = segmentsUrl.toString();
_url = _url.substring(0, _url.lastIndexOf('/') + 1);
String segmentUri = _url + uri;
lsp.totalDuration += trackData.getTrackInfo().duration;
lsp.lastSegDuration = trackData.getTrackInfo().duration;
lsp.segments.add(segmentUri);
uri = _url + uri;
}
lsp.totalDuration += trackData.getTrackInfo().duration;
lsp.lastSegDuration = trackData.getTrackInfo().duration;
lsp.segments.add(uri);
}
return lsp;
}
@ -85,7 +94,7 @@ public abstract class AbstractHlsDownload implements Download {
}
String getSegmentPlaylistUrl(Model model) throws IOException, ExecutionException, ParseException, PlaylistException {
protected String getSegmentPlaylistUrl(Model model) throws IOException, ExecutionException, ParseException, PlaylistException {
LOG.debug("{} stream idx: {}", model.getName(), model.getStreamUrlIndex());
List<StreamSource> streamSources = model.getStreamSources();
Collections.sort(streamSources);

View File

@ -23,7 +23,7 @@ public class Fc2Live extends AbstractSite {
@Override
public String getAffiliateLink() {
return BASE_URL;
return BASE_URL + "/?afid=98987181";
}
@Override
@ -41,7 +41,7 @@ public class Fc2Live extends AbstractSite {
@Override
public String getBuyTokensLink() {
return BASE_URL;
return getAffiliateLink();
}
@Override

View File

@ -0,0 +1,28 @@
package ctbrec.sites.fc2live;
import java.io.IOException;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpClient;
import ctbrec.recorder.download.MergedHlsDownload;
public class Fc2MergedHlsDownload extends MergedHlsDownload {
private Fc2WebSocketClient ws;
public Fc2MergedHlsDownload(HttpClient client) {
super(client);
}
@Override
public void start(Model model, Config config) throws IOException {
super.start(model, config);
}
@Override
public void stop() {
super.stop();
}
}

View File

@ -83,7 +83,7 @@ public class Fc2Model extends AbstractModel {
public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) {
return onlineState;
} else if(Objects.equals(onlineState, "n/a")){
} else if(Objects.equals(onlineState, State.UNKNOWN)){
loadModelInfo();
}
return onlineState;

View File

@ -42,7 +42,9 @@ public class Fc2WebSocketClient {
JSONArray playlists = args.getJSONArray("playlists_high_latency");
JSONObject playlist = playlists.getJSONObject(0);
playlistUrl = playlist.getString("url");
webSocket.close(1000, "");
synchronized (monitor) {
monitor.notify();
}
}
}
@ -53,9 +55,7 @@ public class Fc2WebSocketClient {
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
synchronized (monitor) {
monitor.notify();
}
LOG.debug("ws closed {} - {}", code, reason);
}
@Override