Fix MVLive overview tabs

This commit is contained in:
0xb00bface 2023-04-09 12:41:50 +02:00
parent c8d9e53cc2
commit ac2c8ded4d
2 changed files with 58 additions and 14 deletions

View File

@ -23,16 +23,10 @@ public class MVLiveTabProvider extends AbstractTabProvider {
}
private Tab createTab(String title) {
var updateService = new MVLiveUpdateService((MVLive) site);
var updateService = new MVLiveUpdateService((MVLive) site, "https://creator-api.vidchat.manyvids.com/creator/live");
var tab = new ThumbOverviewTab(title, updateService, site);
tab.setRecorder(site.getRecorder());
tab.setImageAspectRatio(16.0 / 9.0);
tab.setImageAspectRatio(1);
return tab;
}
@Override
public Tab getFollowedTab() {
return null;
}
}

View File

@ -1,28 +1,78 @@
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;
public MVLiveUpdateService(MVLive mvlive) {
this.mvlive = mvlive;
}
private final String url;
@Override
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
return new Task<>() {
@Override
public List<Model> call() throws IOException {
return mvlive.getModels();
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;
}
}