184 lines
5.5 KiB
Java
184 lines
5.5 KiB
Java
package ctbrec.sites.cherrytv;
|
|
|
|
import ctbrec.Model;
|
|
import ctbrec.io.HttpClient;
|
|
import ctbrec.io.HttpException;
|
|
import ctbrec.sites.AbstractSite;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URLEncoder;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Objects;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import static ctbrec.Model.State.OFFLINE;
|
|
import static ctbrec.Model.State.ONLINE;
|
|
import static ctbrec.io.HttpConstants.*;
|
|
|
|
public class CherryTv extends AbstractSite {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(CherryTv.class);
|
|
|
|
public static final String BASE_URL = "https://cherry.tv";
|
|
|
|
private CherryTvHttpClient httpClient;
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "CherryTV";
|
|
}
|
|
|
|
@Override
|
|
public String getBaseUrl() {
|
|
return BASE_URL;
|
|
}
|
|
|
|
@Override
|
|
public String getAffiliateLink() {
|
|
return getBaseUrl();
|
|
}
|
|
|
|
@Override
|
|
public CherryTvModel createModel(String name) {
|
|
CherryTvModel model = new CherryTvModel();
|
|
model.setName(name);
|
|
model.setUrl(getBaseUrl() + '/' + name);
|
|
model.setDescription("");
|
|
model.setSite(this);
|
|
return model;
|
|
}
|
|
|
|
@Override
|
|
public Double getTokenBalance() throws IOException {
|
|
return 0d;
|
|
}
|
|
|
|
@Override
|
|
public String getBuyTokensLink() {
|
|
return getAffiliateLink();
|
|
}
|
|
|
|
@Override
|
|
public synchronized boolean login() throws IOException {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public HttpClient getHttpClient() {
|
|
if (httpClient == null) {
|
|
httpClient = new CherryTvHttpClient(getConfig());
|
|
}
|
|
return httpClient;
|
|
}
|
|
|
|
@Override
|
|
public void init() throws IOException {
|
|
// nothing to do
|
|
}
|
|
|
|
@Override
|
|
public void shutdown() {
|
|
if (httpClient != null) {
|
|
httpClient.shutdown();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean supportsTips() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean supportsFollow() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean supportsSearch() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean searchRequiresLogin() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public List<Model> search(String q) throws IOException, InterruptedException {
|
|
String url = "https://cherry.tv/graphql?operationName=findStreamersBySearch&variables="
|
|
+ "{\"limit\":6,\"slug\":\"" + URLEncoder.encode(q, "utf-8") + "\"}&extensions={\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"03d2f017fee32e1b6a1d3f816ce226c464a78d8dab28895c321980fbadffc1ef\"}}";
|
|
Request req = new Request.Builder()
|
|
.url(url)
|
|
.header(USER_AGENT, getConfig().getSettings().httpUserAgent)
|
|
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
|
|
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
|
.header(REFERER, getBaseUrl())
|
|
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
|
.build();
|
|
LOG.debug("Search URL: {}", req.url());
|
|
List<Model> result = new LinkedList<>();
|
|
try (Response response = getHttpClient().execute(req)) {
|
|
if (response.isSuccessful()) {
|
|
JSONObject json = new JSONObject(Objects.requireNonNull(response.body()).string());
|
|
LOG.debug(json.toString(2));
|
|
JSONObject data = json.getJSONObject("data");
|
|
JSONObject searchResult = data.getJSONObject("searchResult");
|
|
JSONArray streamers = searchResult.getJSONArray("streamers");
|
|
for (int i = 0; i < streamers.length(); i++) {
|
|
JSONObject hit = streamers.getJSONObject(i);
|
|
CherryTvModel model = createModel(hit.getString("username"));
|
|
model.setId(hit.getString("id"));
|
|
boolean online = hit.optString("showStatus").equalsIgnoreCase("Public")
|
|
&& hit.optString("broadcastStatus").equalsIgnoreCase("Live");
|
|
model.setOnline(online);
|
|
model.setOnlineState(online ? ONLINE : OFFLINE);
|
|
model.setDescription(hit.getString("description"));
|
|
model.setPreview(hit.getString("imageUrl"));
|
|
result.add(model);
|
|
}
|
|
} else {
|
|
throw new HttpException(response.code(), response.message());
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public boolean isSiteForModel(Model m) {
|
|
return m instanceof CherryTvModel;
|
|
}
|
|
|
|
@Override
|
|
public boolean credentialsAvailable() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public Model createModelFromUrl(String url) {
|
|
Matcher m = Pattern.compile("https?://.*?cherry\\.tv/([^/]*?)/?").matcher(url);
|
|
if (m.matches()) {
|
|
String modelName = m.group(1);
|
|
CherryTvModel model = createModel(modelName);
|
|
try {
|
|
model.isOnline(true);
|
|
} catch (InterruptedException e) {
|
|
Thread.currentThread().interrupt();
|
|
} catch (Exception e) {
|
|
LOG.warn("Couldn't determine model id. This could cause problems in the future", e);
|
|
}
|
|
return model;
|
|
} else {
|
|
return super.createModelFromUrl(url);
|
|
}
|
|
}
|
|
}
|