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