Improve Chaturbate search

This commit is contained in:
0xb00bface 2021-06-04 11:37:16 +02:00
parent 939758403e
commit 0b25c68bdb
2 changed files with 26 additions and 18 deletions

View File

@ -2,6 +2,7 @@
========================
* Added Amateur.TV
* Added XloveCam
* Improved Chaturbate search
* Fixed tipping function
4.3.1

View File

@ -10,8 +10,12 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.StringUtil;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient;
import ctbrec.sites.AbstractSite;
@ -119,34 +123,37 @@ public class Chaturbate extends AbstractSite {
@Override
public List<Model> search(String q) throws IOException, InterruptedException {
String url = baseUrl + "?keywords=" + URLEncoder.encode(q, "utf-8");
String url = baseUrl + "/ax/search/?keywords=" + URLEncoder.encode(q, "utf-8");
List<Model> result = new ArrayList<>();
// search online models
Request req = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(ACCEPT, "*/*")
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header(REFERER, getBaseUrl())
.build();
try(Response resp = getHttpClient().execute(req)) {
if(resp.isSuccessful()) {
result.addAll(ChaturbateModelParser.parseModels(this, resp.body().string()));
}
}
// since chaturbate does not return offline models, we at least try, if the profile page
// exists for the search string
url = baseUrl + '/' + q;
req = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try(Response resp = getHttpClient().execute(req)) {
if(resp.isSuccessful()) {
Model model = createModel(q);
result.add(model);
try (Response resp = getHttpClient().execute(req)) {
if (resp.isSuccessful()) {
JSONObject json = new JSONObject(resp.body().string());
JSONArray offline = json.getJSONArray("offline");
JSONArray online = json.getJSONArray("online");
for (int i = 0; i < online.length(); i++) {
result.add(createModel(online.getString(i)));
}
for (int i = 0; i < offline.length(); i++) {
result.add(createModel(offline.getString(i)));
}
}
}
result.sort((m1, m2) -> {
int m1match = StringUtil.percentageOfEquality(q, m1.getName());
int m2match = StringUtil.percentageOfEquality(q, m2.getName());
return m2match - m1match;
});
return result;
}