forked from j62/ctbrec
1
0
Fork 0

Implement search for Flirt4Free

This commit is contained in:
0xboobface 2019-04-15 17:42:20 +02:00
parent 94460d1e94
commit 1700eeecf5
1 changed files with 18 additions and 26 deletions

View File

@ -3,22 +3,21 @@ package ctbrec.sites.flirt4free;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import ctbrec.sites.AbstractSite;
import ctbrec.sites.camsoda.CamsodaModel;
import okhttp3.Request;
import okhttp3.Response;
@ -119,39 +118,32 @@ public class Flirt4Free extends AbstractSite {
@Override
public boolean supportsSearch() {
return false;
return true;
}
@Override
public List<Model> search(String q) throws IOException, InterruptedException {
String url = BASE_URI + "/api/v1/browse/autocomplete?s=" + URLEncoder.encode(q, "utf-8");
String url = BASE_URI + "/search/?query=" + URLEncoder.encode(q, "utf-8");
Request req = new Request.Builder()
.url(url)
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.build();
try(Response response = getHttpClient().execute(req)) {
if(response.isSuccessful()) {
JSONObject json = new JSONObject(response.body().string());
if(json.optBoolean("status")) {
List<Model> models = new ArrayList<>();
JSONArray results = json.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
CamsodaModel model = (CamsodaModel) createModel(result.getString("username"));
String thumb = result.getString("thumb");
if(thumb != null) {
model.setPreview("https:" + thumb);
}
if(result.has("display_name")) {
model.setDisplayName(result.getString("display_name"));
}
models.add(model);
}
return models;
} else {
LOG.warn("Search result: " + json.toString(2));
return Collections.emptyList();
List<Model> result = new ArrayList<>();
String body = response.body().string();
Elements modelLinks = HtmlParser.getTags(body, "form#advanced_search h4 a.common-link");
for (Element link : modelLinks) {
String cellHtml = link.parent().parent().parent().html();
link.setBaseUri(BASE_URI);
String bioPage = link.absUrl("href");
Model model = createModelFromUrl(bioPage);
Element img = HtmlParser.getTag(cellHtml, "img[alt]");
model.setPreview(img.attr("src"));
model.setDisplayName(img.attr("alt"));
result.add(model);
}
return result;
} else {
throw new HttpException(response.code(), response.message());
}