Implement cherry.tv search

This commit is contained in:
0xb00bface 2021-11-06 17:41:34 +01:00
parent 5a86cfa85e
commit c5c2ed12ad
1 changed files with 21 additions and 18 deletions

View File

@ -4,18 +4,16 @@ import ctbrec.Model;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.sites.AbstractSite;
import okhttp3.HttpUrl;
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.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -111,32 +109,37 @@ public class CherryTv extends AbstractSite {
@Override
public List<Model> search(String q) throws IOException, InterruptedException {
JSONObject variables = new JSONObject().put("slug", q).put("limit", 10);
JSONObject persistedQuery = new JSONObject().put("persistedQuery", new JSONObject().put("version", 1).put("sha256Hash", ""));
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(new HttpUrl.Builder()
.scheme("https")
.host("cherry.tv")
.addPathSegment("qraphql")
.addQueryParameter("operationName", "Search")
.addQueryParameter("variables", variables.toString())
.addQueryParameter("extensions", persistedQuery.toString())
.build()
)
.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()) {
LOG.debug("Response: {}", Objects.requireNonNull(response.body()).string());
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.setDescription(hit.getString("description"));
model.setPreview(hit.getString("imageUrl"));
result.add(model);
}
} else {
throw new HttpException(response.code(), response.message());
}
}
return Collections.emptyList();
return result;
}
@Override