forked from j62/ctbrec
Fix Cam4 search
This commit is contained in:
parent
3d4bd6f331
commit
ed3a370d18
|
@ -1,15 +1,7 @@
|
||||||
package ctbrec.io;
|
package ctbrec.io;
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import static java.nio.charset.StandardCharsets.*;
|
||||||
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 javax.net.ssl.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -21,11 +13,40 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
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.Map.Entry;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
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 {
|
public abstract class HttpClient {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);
|
private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);
|
||||||
|
@ -268,10 +289,22 @@ public abstract class HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String bodyToJsonObject(Response response) {
|
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) {
|
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;
|
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.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.StringUtil;
|
import ctbrec.StringUtil;
|
||||||
|
@ -8,18 +21,6 @@ import ctbrec.io.HttpException;
|
||||||
import ctbrec.sites.AbstractSite;
|
import ctbrec.sites.AbstractSite;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
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 {
|
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 {
|
private void search(String q, boolean offline, List<Model> models) throws IOException {
|
||||||
String url = BASE_URI + "/usernameSearch?username=" + URLEncoder.encode(q, "utf-8");
|
String url = BASE_URI + "/usernameSearch?username=" + URLEncoder.encode(q, "utf-8");
|
||||||
if(offline) {
|
if (offline) {
|
||||||
url += "&offline=true";
|
url += "&offline=true";
|
||||||
}
|
}
|
||||||
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()) {
|
||||||
String body = bodyToJsonObject(response);
|
String body = bodyToJsonArray(response);
|
||||||
JSONArray results = new JSONArray(body);
|
JSONArray results = new JSONArray(body);
|
||||||
for (int i = 0; i < results.length(); i++) {
|
for (int i = 0; i < results.length(); i++) {
|
||||||
JSONObject result = results.getJSONObject(i);
|
JSONObject result = results.getJSONObject(i);
|
||||||
Model model = createModel(result.getString("username"));
|
Model model = createModel(result.getString("username"));
|
||||||
String thumb = null;
|
String thumb = null;
|
||||||
if(result.has("thumbnailId")) {
|
if (result.has("thumbnailId")) {
|
||||||
thumb = "https://snapshots.xcdnpro.com/thumbnails/" + model.getName() + "?s=" + result.getString("thumbnailId");
|
thumb = "https://snapshots.xcdnpro.com/thumbnails/" + model.getName() + "?s=" + result.getString("thumbnailId");
|
||||||
} else {
|
} else {
|
||||||
thumb = result.getString("profileImageLink");
|
thumb = result.getString("profileImageLink");
|
||||||
}
|
}
|
||||||
if(StringUtil.isNotBlank(thumb)) {
|
if (StringUtil.isNotBlank(thumb)) {
|
||||||
model.setPreview(thumb);
|
model.setPreview(thumb);
|
||||||
}
|
}
|
||||||
models.add(model);
|
models.add(model);
|
||||||
|
|
Loading…
Reference in New Issue