forked from j62/ctbrec
Fix thumbnails in Camsoda search results
This commit is contained in:
parent
f7fc33afd6
commit
1b80458353
|
@ -1,6 +1,15 @@
|
||||||
package ctbrec.sites.camsoda;
|
package ctbrec.sites.camsoda;
|
||||||
|
|
||||||
import static ctbrec.io.HttpConstants.*;
|
import ctbrec.Model;
|
||||||
|
import ctbrec.io.HttpClient;
|
||||||
|
import ctbrec.io.HttpException;
|
||||||
|
import ctbrec.sites.AbstractSite;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
@ -10,17 +19,8 @@ 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 static ctbrec.io.HttpConstants.USER_AGENT;
|
||||||
import org.json.JSONObject;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import ctbrec.Model;
|
|
||||||
import ctbrec.io.HttpClient;
|
|
||||||
import ctbrec.io.HttpException;
|
|
||||||
import ctbrec.sites.AbstractSite;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
public class Camsoda extends AbstractSite {
|
public class Camsoda extends AbstractSite {
|
||||||
|
|
||||||
|
@ -123,25 +123,30 @@ public class Camsoda extends AbstractSite {
|
||||||
|
|
||||||
@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 + "/api/v1/browse/autocomplete?s=" + URLEncoder.encode(q, UTF_8);
|
||||||
Request req = new Request.Builder()
|
Request req = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.addHeader(USER_AGENT, getConfig().getSettings().httpUserAgent)
|
.addHeader(USER_AGENT, getConfig().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());
|
JSONObject json = new JSONObject(response.body().string());
|
||||||
if(json.optBoolean("status")) {
|
if (json.optBoolean("status")) {
|
||||||
List<Model> models = new ArrayList<>();
|
List<Model> models = new ArrayList<>();
|
||||||
JSONArray results = json.getJSONArray("results");
|
JSONArray results = json.getJSONArray("results");
|
||||||
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);
|
||||||
CamsodaModel model = (CamsodaModel) createModel(result.getString("username"));
|
CamsodaModel model = (CamsodaModel) createModel(result.getString("username"));
|
||||||
String thumb = result.getString("thumb");
|
String thumb = result.getString("thumb");
|
||||||
if(thumb != null) {
|
if (thumb != null) {
|
||||||
model.setPreview("https:" + thumb);
|
if (thumb.startsWith("//")) {
|
||||||
|
thumb = "https:" + thumb;
|
||||||
|
} else if (thumb.startsWith("/")) {
|
||||||
|
thumb = BASE_URI + thumb;
|
||||||
|
}
|
||||||
|
model.setPreview(thumb);
|
||||||
}
|
}
|
||||||
if(result.has("display_name")) {
|
if (result.has("display_name")) {
|
||||||
model.setDisplayName(result.getString("display_name"));
|
model.setDisplayName(result.getString("display_name"));
|
||||||
}
|
}
|
||||||
models.add(model);
|
models.add(model);
|
||||||
|
@ -171,7 +176,7 @@ public class Camsoda extends AbstractSite {
|
||||||
@Override
|
@Override
|
||||||
public Model createModelFromUrl(String url) {
|
public Model createModelFromUrl(String url) {
|
||||||
Matcher m = Pattern.compile("https?://(?:www\\.)?camsoda.com/([^/]*?)/?").matcher(url);
|
Matcher m = Pattern.compile("https?://(?:www\\.)?camsoda.com/([^/]*?)/?").matcher(url);
|
||||||
if(m.matches()) {
|
if (m.matches()) {
|
||||||
String modelName = m.group(1);
|
String modelName = m.group(1);
|
||||||
return createModel(modelName);
|
return createModel(modelName);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue