Fix Cam4 search
This commit is contained in:
parent
3d4bd6f331
commit
ed3a370d18
|
@ -1,15 +1,7 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Settings.ProxyType;
|
||||
import okhttp3.*;
|
||||
import okhttp3.OkHttpClient.Builder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -21,11 +13,40 @@ import java.security.NoSuchAlgorithmException;
|
|||
import java.security.SecureRandom;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import javax.net.ssl.KeyManager;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Settings.ProxyType;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.OkHttpClient.Builder;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.Route;
|
||||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
|
||||
public abstract class HttpClient {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);
|
||||
|
@ -268,10 +289,22 @@ public abstract class HttpClient {
|
|||
}
|
||||
|
||||
public static String bodyToJsonObject(Response response) {
|
||||
return Optional.ofNullable(response.body()).map(Object::toString).orElse("{}");
|
||||
return Optional.ofNullable(response.body()).map(b -> {
|
||||
try {
|
||||
return b.string();
|
||||
} catch (IOException e) {
|
||||
return "{}";
|
||||
}
|
||||
}).orElse("{}");
|
||||
}
|
||||
|
||||
public static String bodyToJsonArray(Response response) {
|
||||
return Optional.ofNullable(response.body()).map(Object::toString).orElse("[]");
|
||||
return Optional.ofNullable(response.body()).map(b -> {
|
||||
try {
|
||||
return b.string();
|
||||
} catch (IOException e) {
|
||||
return "[]";
|
||||
}
|
||||
}).orElse("[]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,18 @@
|
|||
package ctbrec.sites.cam4;
|
||||
|
||||
import static ctbrec.io.HttpClient.*;
|
||||
import static ctbrec.io.HttpConstants.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
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;
|
||||
|
@ -8,18 +21,6 @@ import ctbrec.io.HttpException;
|
|||
import ctbrec.sites.AbstractSite;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static ctbrec.io.HttpClient.bodyToJsonObject;
|
||||
import static ctbrec.io.HttpConstants.USER_AGENT;
|
||||
|
||||
public class Cam4 extends AbstractSite {
|
||||
|
||||
|
@ -121,27 +122,27 @@ public class Cam4 extends AbstractSite {
|
|||
|
||||
private void search(String q, boolean offline, List<Model> models) throws IOException {
|
||||
String url = BASE_URI + "/usernameSearch?username=" + URLEncoder.encode(q, "utf-8");
|
||||
if(offline) {
|
||||
if (offline) {
|
||||
url += "&offline=true";
|
||||
}
|
||||
Request req = new Request.Builder()
|
||||
.url(url)
|
||||
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.build();
|
||||
try(Response response = getHttpClient().execute(req)) {
|
||||
if(response.isSuccessful()) {
|
||||
String body = bodyToJsonObject(response);
|
||||
try (Response response = getHttpClient().execute(req)) {
|
||||
if (response.isSuccessful()) {
|
||||
String body = bodyToJsonArray(response);
|
||||
JSONArray results = new JSONArray(body);
|
||||
for (int i = 0; i < results.length(); i++) {
|
||||
JSONObject result = results.getJSONObject(i);
|
||||
Model model = createModel(result.getString("username"));
|
||||
String thumb = null;
|
||||
if(result.has("thumbnailId")) {
|
||||
if (result.has("thumbnailId")) {
|
||||
thumb = "https://snapshots.xcdnpro.com/thumbnails/" + model.getName() + "?s=" + result.getString("thumbnailId");
|
||||
} else {
|
||||
thumb = result.getString("profileImageLink");
|
||||
}
|
||||
if(StringUtil.isNotBlank(thumb)) {
|
||||
if (StringUtil.isNotBlank(thumb)) {
|
||||
model.setPreview(thumb);
|
||||
}
|
||||
models.add(model);
|
||||
|
|
Loading…
Reference in New Issue