Add new tabs to Chaturbate

This commit is contained in:
0xb00bface 2023-04-10 12:27:37 +02:00
parent db8ed3396a
commit 4bec3f0fc8
3 changed files with 87 additions and 3 deletions

View File

@ -0,0 +1,70 @@
package ctbrec.ui.sites.chaturbate;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.StringUtil;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpException;
import ctbrec.sites.chaturbate.Chaturbate;
import ctbrec.sites.chaturbate.ChaturbateModel;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import static ctbrec.io.HttpConstants.*;
@Slf4j
@RequiredArgsConstructor
public class ChaturbateApiUpdateService extends PaginatedScheduledService {
private final String url;
private final Chaturbate chaturbate;
@Override
protected Task<List<Model>> createTask() {
return new Task<>() {
@Override
protected List<Model> call() throws Exception {
var request = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
.build();
try (var response = chaturbate.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
String body = response.body().string();
log.trace(body);
var models = new ArrayList<Model>();
JSONObject json = new JSONObject(body);
JSONArray rooms = json.optJSONArray("rooms");
for (int i = 0; rooms != null && i < rooms.length(); i++) {
JSONObject room = rooms.getJSONObject(i);
String name = room.getString("room");
ChaturbateModel model = (ChaturbateModel) chaturbate.createModel(name);
model.setDescription(toText(room.optString("subject")) + " #gender:" + room.optString("gender"));
if (StringUtil.isNotBlank(room.optString("display_age"))) {
model.setDescription(model.getDescription() + " #age:" + room.optString("display_age"));
}
models.add(model);
}
return models;
} else {
throw new HttpException(response.code(), response.message());
}
}
}
};
}
protected String toText(String html) {
return HtmlParser.getText(html, "*");
}
}

View File

@ -14,9 +14,9 @@ import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
public class ChaturbateFollowedTab extends ThumbOverviewTab implements FollowedTab {
private Label status;
private String onlineUrl;
private String offlineUrl;
private final Label status;
private final String onlineUrl;
private final String offlineUrl;
public ChaturbateFollowedTab(String title, String url, Chaturbate chaturbate) {
super(title, new ChaturbateUpdateService(url, true, chaturbate), chaturbate);

View File

@ -2,6 +2,7 @@ package ctbrec.ui.sites.chaturbate;
import ctbrec.sites.chaturbate.Chaturbate;
import ctbrec.ui.sites.AbstractTabProvider;
import ctbrec.ui.tabs.PaginatedScheduledService;
import ctbrec.ui.tabs.ThumbOverviewTab;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
@ -31,9 +32,13 @@ public class ChaturbateTabProvider extends AbstractTabProvider {
followedTab.setRecorder(recorder);
followedTab.setImageAspectRatio(9.0 / 16.0);
tabs.add(followedTab);
tabs.add(createApiTab("Top Rated", site.getBaseUrl() + "/api/ts/discover/carousels/top-rated/"));
tabs.add(createApiTab("Trending", site.getBaseUrl() + "/api/ts/discover/carousels/trending/"));
tabs.add(createApiTab("Recommended", site.getBaseUrl() + "/api/ts/discover/carousels/recommended/"));
return tabs;
}
@Override
public Tab getFollowedTab() {
return followedTab;
@ -41,9 +46,18 @@ public class ChaturbateTabProvider extends AbstractTabProvider {
private Tab createTab(String title, String url) {
var updateService = new ChaturbateUpdateService(url, false, (Chaturbate) site);
return createTab(title, updateService);
}
private Tab createTab(String title, PaginatedScheduledService updateService) {
var tab = new ThumbOverviewTab(title, updateService, site);
tab.setRecorder(recorder);
tab.setImageAspectRatio(9.0 / 16.0);
return tab;
}
private Tab createApiTab(String title, String apiUrl) {
var updateService = new ChaturbateApiUpdateService(apiUrl, (Chaturbate) site);
return createTab(title, updateService);
}
}