From ac2c8ded4db983ecf36cf90fcc1e90860b938444 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sun, 9 Apr 2023 12:41:50 +0200 Subject: [PATCH] Fix MVLive overview tabs --- .../ui/sites/manyvids/MVLiveTabProvider.java | 10 +-- .../sites/manyvids/MVLiveUpdateService.java | 62 +++++++++++++++++-- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveTabProvider.java b/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveTabProvider.java index a847e544..07033719 100644 --- a/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveTabProvider.java +++ b/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveTabProvider.java @@ -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; - } - } diff --git a/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveUpdateService.java b/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveUpdateService.java index b31299b0..ff0be2fc 100644 --- a/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/manyvids/MVLiveUpdateService.java @@ -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> createTask() { - return new Task>() { + return new Task<>() { @Override public List call() throws IOException { - return mvlive.getModels(); + List models = loadModels(url); + return models; } }; } + + protected List loadModels(String url) throws IOException { + List 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; + } }