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