312 lines
12 KiB
Java
312 lines
12 KiB
Java
package ctbrec.sites.cam4;
|
|
|
|
import static ctbrec.Model.State.*;
|
|
import static ctbrec.io.HttpConstants.*;
|
|
import static java.util.regex.Pattern.*;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.json.JSONObject;
|
|
import org.jsoup.nodes.Element;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.iheartradio.m3u8.Encoding;
|
|
import com.iheartradio.m3u8.Format;
|
|
import com.iheartradio.m3u8.ParseException;
|
|
import com.iheartradio.m3u8.ParsingMode;
|
|
import com.iheartradio.m3u8.PlaylistException;
|
|
import com.iheartradio.m3u8.PlaylistParser;
|
|
import com.iheartradio.m3u8.data.MasterPlaylist;
|
|
import com.iheartradio.m3u8.data.Playlist;
|
|
import com.iheartradio.m3u8.data.PlaylistData;
|
|
import com.iheartradio.m3u8.data.StreamInfo;
|
|
|
|
import ctbrec.AbstractModel;
|
|
import ctbrec.Config;
|
|
import ctbrec.io.HtmlParser;
|
|
import ctbrec.io.HttpException;
|
|
import ctbrec.recorder.download.StreamSource;
|
|
import okhttp3.FormBody;
|
|
import okhttp3.Request;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.Response;
|
|
|
|
public class Cam4Model extends AbstractModel {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Cam4Model.class);
|
|
private transient Instant playlistRequestTimestamp = Instant.EPOCH;
|
|
private String playlistUrl;
|
|
private int[] resolution = null;
|
|
private boolean privateRoom = false;
|
|
|
|
@Override
|
|
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
|
|
if (ignoreCache || onlineState == UNKNOWN) {
|
|
try {
|
|
loadModelDetails();
|
|
getPlaylistUrl();
|
|
} catch (Exception e) {
|
|
onlineState = OFFLINE;
|
|
}
|
|
}
|
|
return onlineState == ONLINE && !privateRoom && playlistUrl != null && !playlistUrl.isEmpty();
|
|
}
|
|
|
|
private void loadModelDetails() throws IOException {
|
|
JSONObject roomState = new Cam4WsClient(Config.getInstance(), (Cam4)getSite(), this).getRoomState();
|
|
if(LOG.isTraceEnabled()) LOG.trace(roomState.toString(2));
|
|
String state = roomState.optString("newShowsState");
|
|
setOnlineStateByShowType(state);
|
|
privateRoom = roomState.optBoolean("privateRoom");
|
|
setDescription(roomState.optString("status"));
|
|
}
|
|
|
|
public void setOnlineStateByShowType(String showType) {
|
|
switch(showType) {
|
|
case "NORMAL":
|
|
case "ACCEPTING":
|
|
case "GROUP_SHOW_SELLING_TICKETS":
|
|
case "GS_SELLING_TICKETS":
|
|
case "GS_SELLING_TICKETS_UNSUCCESSFUL":
|
|
onlineState = ONLINE;
|
|
break;
|
|
case "PRIVATE_SHOW":
|
|
case "INSIDE_PS":
|
|
onlineState = PRIVATE;
|
|
break;
|
|
case "INSIDE_GS":
|
|
case "GROUP_SHOW":
|
|
onlineState = GROUP;
|
|
break;
|
|
case "PAUSED":
|
|
onlineState = AWAY;
|
|
break;
|
|
case "OFFLINE":
|
|
onlineState = OFFLINE;
|
|
break;
|
|
default:
|
|
LOG.debug("############################## Unknown show type [{}]", showType);
|
|
onlineState = UNKNOWN;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
|
|
if(failFast) {
|
|
return onlineState;
|
|
} else {
|
|
if(onlineState == UNKNOWN) {
|
|
try {
|
|
loadModelDetails();
|
|
} catch (Exception e) {
|
|
LOG.warn("Couldn't load model details {}", e.getMessage());
|
|
}
|
|
}
|
|
return onlineState;
|
|
}
|
|
}
|
|
|
|
private String getPlaylistUrl() throws IOException {
|
|
if (playlistUrl == null || playlistUrl.trim().isEmpty() || playlistIsOutdated()) {
|
|
String page = loadModelPage();
|
|
Matcher m = Pattern.compile("hlsUrl\\s*:\\s*'(.*?)'", DOTALL | MULTILINE).matcher(page);
|
|
if (m.find()) {
|
|
playlistUrl = m.group(1);
|
|
} else {
|
|
m = Pattern.compile("\"videoPlayUrl\"\\s*:\\s*\"(.*?)\"", DOTALL | MULTILINE).matcher(page);
|
|
if (m.find()) {
|
|
generatePlaylistUrlFromStreamName(m.group(1));
|
|
}
|
|
}
|
|
if (playlistUrl == null) {
|
|
throw new IOException("Couldn't determine playlist url");
|
|
}
|
|
playlistRequestTimestamp = Instant.now();
|
|
}
|
|
return playlistUrl;
|
|
}
|
|
|
|
private boolean playlistIsOutdated() {
|
|
return Duration.between(playlistRequestTimestamp, Instant.now()).getSeconds() > TimeUnit.MINUTES.toSeconds(2);
|
|
}
|
|
|
|
private void generatePlaylistUrlFromStreamName(String streamName) {
|
|
Matcher m = Pattern.compile(".*?-(\\d{3,})-.*").matcher(streamName);
|
|
if (m.find()) {
|
|
String number = m.group(1);
|
|
playlistUrl = "https://cam4-hls.xcdnpro.com/" + number + "/cam4-origin-live/ngrp:" + streamName + "_all/playlist.m3u8";
|
|
}
|
|
}
|
|
|
|
private String loadModelPage() throws IOException {
|
|
Request req = new Request.Builder().url(getUrl()).build();
|
|
try (Response response = site.getHttpClient().execute(req)) {
|
|
if (response.isSuccessful()) {
|
|
return response.body().string();
|
|
} else {
|
|
throw new HttpException(response.code(), response.message());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
|
|
MasterPlaylist masterPlaylist = getMasterPlaylist();
|
|
List<StreamSource> sources = new ArrayList<>();
|
|
for (PlaylistData playlist : masterPlaylist.getPlaylists()) {
|
|
if (playlist.hasStreamInfo()) {
|
|
StreamSource src = new StreamSource();
|
|
src.bandwidth = playlist.getStreamInfo().getBandwidth();
|
|
src.height = Optional.ofNullable(playlist.getStreamInfo()).map(StreamInfo::getResolution).map(res -> res.height).orElse(0);
|
|
String masterUrl = getPlaylistUrl();
|
|
String baseUrl = masterUrl.substring(0, masterUrl.lastIndexOf('/') + 1);
|
|
String segmentUri = baseUrl + playlist.getUri();
|
|
src.mediaPlaylistUrl = segmentUri;
|
|
LOG.trace("Media playlist {}", src.mediaPlaylistUrl);
|
|
sources.add(src);
|
|
}
|
|
}
|
|
return sources;
|
|
}
|
|
|
|
private MasterPlaylist getMasterPlaylist() throws IOException, ParseException, PlaylistException {
|
|
LOG.trace("Loading master playlist [{}]", getPlaylistUrl());
|
|
Request req = new Request.Builder().url(getPlaylistUrl()).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();
|
|
return master;
|
|
} else {
|
|
throw new HttpException(response.code(), "Couldn't download HLS playlist");
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void invalidateCacheEntries() {
|
|
resolution = null;
|
|
playlistUrl = null;
|
|
}
|
|
|
|
@Override
|
|
public void receiveTip(Double tokens) throws IOException {
|
|
throw new RuntimeException("Not implemented for Cam4, yet");
|
|
}
|
|
|
|
@Override
|
|
public int[] getStreamResolution(boolean failFast) throws ExecutionException {
|
|
if(resolution == null) {
|
|
if(failFast) {
|
|
return new int[2];
|
|
}
|
|
try {
|
|
if(!isOnline()) {
|
|
return new int[2];
|
|
}
|
|
List<StreamSource> sources = getStreamSources();
|
|
Collections.sort(sources);
|
|
StreamSource best = sources.get(sources.size()-1);
|
|
resolution = new int[] {best.width, best.height};
|
|
} catch (InterruptedException e) {
|
|
Thread.currentThread().interrupt();
|
|
LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage());
|
|
resolution = new int[2];
|
|
} catch (ExecutionException | IOException | ParseException | PlaylistException e) {
|
|
LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage());
|
|
resolution = new int[2];
|
|
}
|
|
return resolution;
|
|
} else {
|
|
return resolution;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean follow() throws IOException {
|
|
String url = site.getBaseUrl() + "/profiles/addFriendFavorite?action=addFavorite&object=" + getName() + "&_=" + System.currentTimeMillis();
|
|
Request req = new Request.Builder()
|
|
.url(url)
|
|
.addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
|
.build();
|
|
try (Response response = site.getHttpClient().execute(req)) {
|
|
return response.isSuccessful();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean unfollow() throws IOException {
|
|
// get model user id
|
|
String url = site.getBaseUrl() + '/' + getName();
|
|
Request req = new Request.Builder()
|
|
.url(url)
|
|
.addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
|
.build();
|
|
|
|
// we have to use a client without any cam4 cookies here, otherwise
|
|
// this request is redirected to the login page. no idea why
|
|
try (Response response = site.getRecorder().getHttpClient().execute(req)) {
|
|
if (response.isSuccessful()) {
|
|
String content = response.body().string();
|
|
String broadCasterId = null;
|
|
try {
|
|
Element tag = HtmlParser.getTag(content, "input[name=\"broadcasterId\"]");
|
|
broadCasterId = tag.attr("value");
|
|
} catch (Exception e) {
|
|
LOG.debug(content);
|
|
throw new IOException(e);
|
|
}
|
|
|
|
// send unfollow request
|
|
String username = Config.getInstance().getSettings().cam4Username;
|
|
url = site.getBaseUrl() + '/' + username + "/edit/friends_favorites";
|
|
RequestBody body = new FormBody.Builder()
|
|
.add("deleteFavorites", broadCasterId)
|
|
.add("simpleresult", "true")
|
|
.build();
|
|
req = new Request.Builder()
|
|
.url(url)
|
|
.post(body)
|
|
.addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
|
.build();
|
|
try (Response resp = site.getHttpClient().execute(req)) {
|
|
if (resp.isSuccessful()) {
|
|
return Objects.equals(resp.body().string(), "Ok");
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setPlaylistUrl(String playlistUrl) {
|
|
this.playlistUrl = playlistUrl;
|
|
}
|
|
|
|
public class ModelDetailsEmptyException extends Exception {
|
|
public ModelDetailsEmptyException(String msg) {
|
|
super(msg);
|
|
}
|
|
}
|
|
}
|