79 lines
2.7 KiB
Java
79 lines
2.7 KiB
Java
package ctbrec.ui.sites.manyvids;
|
|
|
|
import ctbrec.Model;
|
|
import ctbrec.io.HttpException;
|
|
import ctbrec.sites.manyvids.MVLive;
|
|
import ctbrec.sites.manyvids.MVLiveModel;
|
|
import ctbrec.ui.tabs.PaginatedScheduledService;
|
|
import javafx.concurrent.Task;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
import static ctbrec.io.HttpConstants.*;
|
|
|
|
@Slf4j
|
|
@RequiredArgsConstructor
|
|
public class MVLiveUpdateService extends PaginatedScheduledService {
|
|
|
|
private final MVLive mvlive;
|
|
private final String url;
|
|
|
|
@Override
|
|
protected Task<List<Model>> createTask() {
|
|
return new Task<>() {
|
|
@Override
|
|
public List<Model> call() throws IOException {
|
|
List<Model> models = loadModels(url);
|
|
return models;
|
|
}
|
|
};
|
|
}
|
|
|
|
protected List<Model> loadModels(String url) throws IOException {
|
|
List<Model> models = new ArrayList<>();
|
|
log.debug("Loading live models from {}", url);
|
|
Request req = new Request.Builder()
|
|
.url(url)
|
|
.header(ACCEPT, "*/*")
|
|
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
|
|
.header(ORIGIN, MVLive.BASE_URL)
|
|
.header(REFERER, MVLive.BASE_URL)
|
|
.header(AUTHORIZATION, "GUEST")
|
|
.build();
|
|
try (Response response = mvlive.getHttpClient().execute(req)) {
|
|
String body = response.body().string();
|
|
log.trace("response body: {}", body);
|
|
if (response.isSuccessful()) {
|
|
JSONObject json = new JSONObject(body);
|
|
if (!json.has("live_creators")) {
|
|
log.debug("Unexpected response:\n{}", json.toString(2));
|
|
return Collections.emptyList();
|
|
}
|
|
JSONArray creators = json.getJSONArray("live_creators");
|
|
for (int i = 0; i < creators.length(); i++) {
|
|
JSONObject creator = creators.getJSONObject(i);
|
|
MVLiveModel model = mvlive.createModel(creator.getString("url_handle"));
|
|
model.updateStateFromJson(creator);
|
|
models.add(model);
|
|
}
|
|
if (!json.optString("next_token").isBlank()) {
|
|
models.addAll(loadModels(url + "?next_token=" + json.optString("next_token")));
|
|
}
|
|
} else {
|
|
throw new HttpException(response.code(), body);
|
|
}
|
|
}
|
|
return models;
|
|
}
|
|
}
|