forked from j62/ctbrec
Fix CamSoda downloads
Some models now have a different stream URL. ctbrec has to distiguish between the old and the new URLs
This commit is contained in:
parent
33c7c6606d
commit
f3a13a6f06
|
@ -66,15 +66,11 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
|
|||
if(result.has("tpl")) {
|
||||
JSONArray tpl = result.getJSONArray("tpl");
|
||||
String name = tpl.getString(getTemplateIndex(template, "username"));
|
||||
// int connections = tpl.getInt(2);
|
||||
String streamName = tpl.getString(getTemplateIndex(template, "stream_name"));
|
||||
CamsodaModel model = (CamsodaModel) camsoda.createModel(name);
|
||||
model.setDescription(tpl.getString(getTemplateIndex(template, "subject_html")));
|
||||
model.setSortOrder(tpl.getFloat(getTemplateIndex(template, "sort_value")));
|
||||
String preview = "https:" + tpl.getString(getTemplateIndex(template, "thumb"));
|
||||
model.setPreview(preview);
|
||||
JSONArray edgeServers = tpl.getJSONArray(getTemplateIndex(template, "edge_servers"));
|
||||
model.setStreamUrl("https://" + edgeServers.getString(0) + "/cam/mp4:" + streamName + "_h264_aac_480p/playlist.m3u8");
|
||||
String displayName = tpl.getString(getTemplateIndex(template, "display_name"));
|
||||
model.setDisplayName(displayName.replaceAll("[^a-zA-Z0-9]", ""));
|
||||
if(model.getDisplayName().isBlank()) {
|
||||
|
@ -84,7 +80,6 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
|
|||
} else {
|
||||
String name = result.getString("username");
|
||||
CamsodaModel model = (CamsodaModel) camsoda.createModel(name);
|
||||
String streamName = result.getString("stream_name");
|
||||
model.setSortOrder(result.getFloat("sort_value"));
|
||||
models.add(model);
|
||||
if(result.has("status")) {
|
||||
|
@ -98,11 +93,6 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
|
|||
}
|
||||
}
|
||||
|
||||
if(result.has("edge_servers")) {
|
||||
JSONArray edgeServers = result.getJSONArray("edge_servers");
|
||||
model.setStreamUrl("https://" + edgeServers.getString(0) + "/cam/mp4:" + streamName + "_h264_aac_480p/playlist.m3u8");
|
||||
}
|
||||
|
||||
if(result.has("thumb")) {
|
||||
String previewUrl = "https:" + result.getString("thumb");
|
||||
model.setPreview(previewUrl);
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package ctbrec.sites.camsoda;
|
||||
|
||||
import static ctbrec.Model.State.*;
|
||||
import static ctbrec.io.HttpConstants.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -35,41 +39,128 @@ import okhttp3.Response;
|
|||
|
||||
public class CamsodaModel extends AbstractModel {
|
||||
|
||||
private static final String STREAM_NAME = "stream_name";
|
||||
private static final String EDGE_SERVERS = "edge_servers";
|
||||
private static final String STATUS = "status";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CamsodaModel.class);
|
||||
private String streamUrl;
|
||||
private List<StreamSource> streamSources = null;
|
||||
private float sortOrder = 0;
|
||||
private Random random = new Random();
|
||||
int[] resolution = new int[2];
|
||||
boolean oldStreamUrl = true;
|
||||
|
||||
public String getStreamUrl() throws IOException {
|
||||
if (streamUrl == null) {
|
||||
// load model
|
||||
if(oldStreamUrl) {
|
||||
loadModel();
|
||||
} else {
|
||||
getNewStreamUrl();
|
||||
}
|
||||
}
|
||||
return streamUrl;
|
||||
}
|
||||
|
||||
private void loadModel() throws IOException {
|
||||
String modelUrl = site.getBaseUrl() + "/api/v1/user/" + getName();
|
||||
Request req = new Request.Builder().url(modelUrl).build();
|
||||
Response response = site.getHttpClient().execute(req);
|
||||
try {
|
||||
JSONObject result = new JSONObject(response.body().string());
|
||||
if(result.getBoolean("status")) {
|
||||
JSONObject chat = result.getJSONObject("user").getJSONObject("chat");
|
||||
String status = chat.getString("status");
|
||||
setOnlineStateByStatus(status);
|
||||
if(chat.has("edge_servers")) {
|
||||
String edgeServer = chat.getJSONArray("edge_servers").getString(0);
|
||||
String streamName = chat.getString("stream_name");
|
||||
streamUrl = "https://" + edgeServer + "/cam/mp4:" + streamName + "_h264_aac_480p/playlist.m3u8";
|
||||
public String getNewStreamUrl() throws IOException {
|
||||
String guestUsername = "guest_" + 10_000 + random.nextInt(50_000);
|
||||
String url = site.getBaseUrl() + "/api/v1/video/vtoken/" + getName() + "?username=" + guestUsername;
|
||||
Request req = new Request.Builder()
|
||||
.url(url)
|
||||
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(req)) {
|
||||
if (response.isSuccessful()) {
|
||||
JSONObject jsonResponse = new JSONObject(response.body().string());
|
||||
if (jsonResponse.optInt(STATUS) == 1) {
|
||||
String edgeServer = jsonResponse.getJSONArray(EDGE_SERVERS).getString(0);
|
||||
String streamName = jsonResponse.getString(STREAM_NAME);
|
||||
String token = jsonResponse.getString("token");
|
||||
streamUrl = "https://" + edgeServer + '/' + streamName + "_h264_aac_480p/index.m3u8?token=" + token;
|
||||
} else {
|
||||
throw new JSONException("Response does not contain a token");
|
||||
}
|
||||
} else {
|
||||
throw new HttpException(response.code(), response.message());
|
||||
}
|
||||
}
|
||||
return streamUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
|
||||
String playlistUrl = getStreamUrl();
|
||||
if (playlistUrl == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
LOG.debug("Loading playlist {}", playlistUrl);
|
||||
Request req = new Request.Builder()
|
||||
.url(playlistUrl)
|
||||
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(req)) {
|
||||
if (response.isSuccessful()) {
|
||||
InputStream inputStream = response.body().byteStream();
|
||||
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
||||
Playlist playlist = parser.parse();
|
||||
MasterPlaylist master = playlist.getMasterPlaylist();
|
||||
PlaylistData playlistData = master.getPlaylists().get(0);
|
||||
StreamSource streamsource = new StreamSource();
|
||||
if (oldStreamUrl) {
|
||||
streamsource.mediaPlaylistUrl = streamUrl.replace("playlist.m3u8", playlistData.getUri());
|
||||
} else {
|
||||
int cutOffAt = playlistUrl.indexOf("index.m3u8");
|
||||
String segmentPlaylistUrl = playlistUrl.substring(0, cutOffAt) + playlistData.getUri();
|
||||
streamsource.mediaPlaylistUrl = segmentPlaylistUrl;
|
||||
}
|
||||
if (playlistData.hasStreamInfo()) {
|
||||
StreamInfo info = playlistData.getStreamInfo();
|
||||
streamsource.bandwidth = info.getBandwidth();
|
||||
streamsource.width = info.hasResolution() ? info.getResolution().width : 0;
|
||||
streamsource.height = info.hasResolution() ? info.getResolution().height : 0;
|
||||
} else {
|
||||
streamsource.bandwidth = 0;
|
||||
streamsource.width = 0;
|
||||
streamsource.height = 0;
|
||||
}
|
||||
streamSources = Collections.singletonList(streamsource);
|
||||
} else {
|
||||
LOG.trace("Response: {}", response.body().string());
|
||||
throw new HttpException(response.code(), response.message());
|
||||
}
|
||||
}
|
||||
return streamSources;
|
||||
}
|
||||
|
||||
private void loadModel() throws IOException {
|
||||
String modelUrl = site.getBaseUrl() + "/api/v1/user/" + getName();
|
||||
Request req = new Request.Builder()
|
||||
.url(modelUrl)
|
||||
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(req)) {
|
||||
if (response.isSuccessful()) {
|
||||
JSONObject result = new JSONObject(response.body().string());
|
||||
if (result.getBoolean(STATUS)) {
|
||||
JSONObject chat = result.getJSONObject("user").getJSONObject("chat");
|
||||
String status = chat.getString(STATUS);
|
||||
oldStreamUrl = !chat.getString(STREAM_NAME).contains("/");
|
||||
if (oldStreamUrl && chat.has(EDGE_SERVERS)) {
|
||||
String edgeServer = chat.getJSONArray(EDGE_SERVERS).getString(0);
|
||||
String streamName = chat.getString(STREAM_NAME);
|
||||
streamUrl = "https://" + edgeServer + "/cam/mp4:" + streamName + "_h264_aac_480p/playlist.m3u8";
|
||||
}
|
||||
setOnlineStateByStatus(status);
|
||||
} else {
|
||||
throw new IOException("Result was not ok");
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
} else throw new HttpException(response.code(), response.message());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,39 +207,6 @@ public class CamsodaModel extends AbstractModel {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
|
||||
String streamUrl = getStreamUrl();
|
||||
if(streamUrl == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Request req = new Request.Builder().url(streamUrl).build();
|
||||
Response response = site.getHttpClient().execute(req);
|
||||
try {
|
||||
InputStream inputStream = response.body().byteStream();
|
||||
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
|
||||
Playlist playlist = parser.parse();
|
||||
MasterPlaylist master = playlist.getMasterPlaylist();
|
||||
PlaylistData playlistData = master.getPlaylists().get(0);
|
||||
StreamSource streamsource = new StreamSource();
|
||||
streamsource.mediaPlaylistUrl = streamUrl.replace("playlist.m3u8", playlistData.getUri());
|
||||
if(playlistData.hasStreamInfo()) {
|
||||
StreamInfo info = playlistData.getStreamInfo();
|
||||
streamsource.bandwidth = info.getBandwidth();
|
||||
streamsource.width = info.hasResolution() ? info.getResolution().width : 0;
|
||||
streamsource.height = info.hasResolution() ? info.getResolution().height : 0;
|
||||
} else {
|
||||
streamsource.bandwidth = 0;
|
||||
streamsource.width = 0;
|
||||
streamsource.height = 0;
|
||||
}
|
||||
streamSources = Collections.singletonList(streamsource);
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
return streamSources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateCacheEntries() {
|
||||
streamSources = null;
|
||||
|
@ -158,9 +216,6 @@ public class CamsodaModel extends AbstractModel {
|
|||
public int[] getStreamResolution(boolean failFast) throws ExecutionException {
|
||||
if (failFast) {
|
||||
return resolution;
|
||||
} else {
|
||||
if(failFast) {
|
||||
return new int[] {0,0};
|
||||
} else {
|
||||
try {
|
||||
List<StreamSource> streamSources = getStreamSources();
|
||||
|
@ -176,7 +231,6 @@ public class CamsodaModel extends AbstractModel {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveTip(Double tokens) throws IOException {
|
||||
|
@ -191,11 +245,11 @@ public class CamsodaModel extends AbstractModel {
|
|||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.addHeader("Referer", Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader("Accept", "application/json, text/plain, */*")
|
||||
.addHeader("Accept-Language", "en")
|
||||
.addHeader("X-CSRF-Token", csrfToken)
|
||||
.addHeader(REFERER, Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||
.addHeader(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.addHeader(X_CSRF_TOKEN, csrfToken)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(request)) {
|
||||
if (!response.isSuccessful()) {
|
||||
|
@ -213,11 +267,11 @@ public class CamsodaModel extends AbstractModel {
|
|||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(RequestBody.create(null, ""))
|
||||
.addHeader("Referer", Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader("Accept", "application/json, text/plain, */*")
|
||||
.addHeader("Accept-Language", "en")
|
||||
.addHeader("X-CSRF-Token", csrfToken)
|
||||
.addHeader(REFERER, Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||
.addHeader(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.addHeader(X_CSRF_TOKEN, csrfToken)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(request)) {
|
||||
if (response.isSuccessful()) {
|
||||
|
@ -236,11 +290,11 @@ public class CamsodaModel extends AbstractModel {
|
|||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(RequestBody.create(null, ""))
|
||||
.addHeader("Referer", Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader("Accept", "application/json, text/plain, */*")
|
||||
.addHeader("Accept-Language", "en")
|
||||
.addHeader("X-CSRF-Token", csrfToken)
|
||||
.addHeader(REFERER, Camsoda.BASE_URI + '/' + getName())
|
||||
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.addHeader(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
||||
.addHeader(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
||||
.addHeader(X_CSRF_TOKEN, csrfToken)
|
||||
.build();
|
||||
try (Response response = site.getHttpClient().execute(request)) {
|
||||
if (response.isSuccessful()) {
|
||||
|
|
Loading…
Reference in New Issue