forked from j62/ctbrec
1
0
Fork 0
ctbrec/common/src/main/java/ctbrec/sites/secretfriends/SecretFriendsModel.java

118 lines
4.3 KiB
Java

package ctbrec.sites.secretfriends;
import com.iheartradio.m3u8.ParseException;
import com.iheartradio.m3u8.PlaylistException;
import ctbrec.AbstractModel;
import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpException;
import ctbrec.recorder.download.StreamSource;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.nodes.Element;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import static ctbrec.Model.State.ONLINE;
import static ctbrec.io.HttpConstants.*;
public class SecretFriendsModel extends AbstractModel {
private String status = null;
private int[] resolution = new int[]{0, 0};
@Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if (ignoreCache || status == null) {
String url = SecretFriends.BASE_URI + "/friend/bio/" + getName();
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)
.header(REFERER, getUrl())
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
Element wrapper = HtmlParser.getTag(body, "div[class~=model-wrapper]");
SecretFriendsModel parsedModel = SecretFriendsModelParser.parse((SecretFriends) getSite(), wrapper);
setName(parsedModel.getName());
setUrl(parsedModel.getUrl());
setPreview(parsedModel.getPreview());
setOnlineState(parsedModel.getOnlineState(true));
} else {
throw new HttpException(response.code(), response.message());
}
}
}
return onlineState == ONLINE;
}
@Override
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException, JAXBException {
String name = getName();
String url = getSite().getBaseUrl() + "/api/front/models/username/" + name + "/cam?triggerRequest=loadCam";
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)
.header(REFERER, getUrl())
.build();
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
return Collections.emptyList();
} else {
throw new HttpException(response.code(), response.message());
}
}
}
@Override
public void invalidateCacheEntries() {
status = null;
resolution = new int[]{0, 0};
}
@Override
public void receiveTip(Double tokens) throws IOException {
// not implemented
}
@Override
public int[] getStreamResolution(boolean failFast) throws ExecutionException {
if (!failFast) {
try {
List<StreamSource> sources = getStreamSources();
if (!sources.isEmpty()) {
StreamSource best = sources.get(sources.size() - 1);
resolution = new int[]{best.getWidth(), best.getHeight()};
}
} catch (IOException | ParseException | PlaylistException | JAXBException e) {
throw new ExecutionException(e);
}
}
return resolution;
}
@Override
public boolean follow() throws IOException {
return false;
}
@Override
public boolean unfollow() throws IOException {
return false;
}
}