forked from j62/ctbrec
Fix CamSoda followed tab
This commit is contained in:
parent
4b2e17d0b1
commit
e202c946ac
|
@ -1,6 +1,9 @@
|
||||||
package ctbrec.ui.sites.camsoda;
|
package ctbrec.ui.sites.camsoda;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import ctbrec.sites.camsoda.Camsoda;
|
import ctbrec.sites.camsoda.Camsoda;
|
||||||
|
import ctbrec.sites.camsoda.CamsodaModel;
|
||||||
import ctbrec.ui.tabs.FollowedTab;
|
import ctbrec.ui.tabs.FollowedTab;
|
||||||
import ctbrec.ui.tabs.ThumbOverviewTab;
|
import ctbrec.ui.tabs.ThumbOverviewTab;
|
||||||
import javafx.concurrent.WorkerStateEvent;
|
import javafx.concurrent.WorkerStateEvent;
|
||||||
|
@ -18,9 +21,10 @@ public class CamsodaFollowedTab extends ThumbOverviewTab implements FollowedTab
|
||||||
boolean showOnline = true;
|
boolean showOnline = true;
|
||||||
|
|
||||||
public CamsodaFollowedTab(String title, Camsoda camsoda) {
|
public CamsodaFollowedTab(String title, Camsoda camsoda) {
|
||||||
super(title, new CamsodaFollowedUpdateService(camsoda), camsoda);
|
super(title, new CamsodaUpdateService(camsoda.getBaseUrl() + "/api/v1/browse/following", true, camsoda, m -> true), camsoda);
|
||||||
status = new Label("Logging in...");
|
status = new Label("Logging in...");
|
||||||
grid.getChildren().add(status);
|
grid.getChildren().add(status);
|
||||||
|
((CamsodaUpdateService)updateService).setFilter(createFilter(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -40,9 +44,9 @@ public class CamsodaFollowedTab extends ThumbOverviewTab implements FollowedTab
|
||||||
HBox.setMargin(online, new Insets(5, 5, 5, 40));
|
HBox.setMargin(online, new Insets(5, 5, 5, 40));
|
||||||
HBox.setMargin(offline, new Insets(5, 5, 5, 5));
|
HBox.setMargin(offline, new Insets(5, 5, 5, 5));
|
||||||
online.setSelected(true);
|
online.setSelected(true);
|
||||||
group.selectedToggleProperty().addListener((e) -> {
|
group.selectedToggleProperty().addListener(e -> {
|
||||||
|
showOnline = online.isSelected();
|
||||||
queue.clear();
|
queue.clear();
|
||||||
((CamsodaFollowedUpdateService)updateService).showOnline(online.isSelected());
|
|
||||||
updateService.restart();
|
updateService.restart();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -78,4 +82,18 @@ public class CamsodaFollowedTab extends ThumbOverviewTab implements FollowedTab
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Predicate<CamsodaModel> createFilter(CamsodaFollowedTab tab) {
|
||||||
|
return m -> {
|
||||||
|
try {
|
||||||
|
return m.isOnline() == tab.showOnline;
|
||||||
|
} catch(InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,79 +0,0 @@
|
||||||
package ctbrec.ui.sites.camsoda;
|
|
||||||
|
|
||||||
import static ctbrec.Model.State.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import ctbrec.Model;
|
|
||||||
import ctbrec.io.HttpException;
|
|
||||||
import ctbrec.sites.camsoda.Camsoda;
|
|
||||||
import ctbrec.sites.camsoda.CamsodaModel;
|
|
||||||
import ctbrec.ui.SiteUiFactory;
|
|
||||||
import ctbrec.ui.tabs.PaginatedScheduledService;
|
|
||||||
import javafx.concurrent.Task;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
public class CamsodaFollowedUpdateService extends PaginatedScheduledService {
|
|
||||||
private Camsoda camsoda;
|
|
||||||
private boolean showOnline = true;
|
|
||||||
|
|
||||||
public CamsodaFollowedUpdateService(Camsoda camsoda) {
|
|
||||||
this.camsoda = camsoda;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Task<List<Model>> createTask() {
|
|
||||||
return new Task<List<Model>>() {
|
|
||||||
@Override
|
|
||||||
public List<Model> call() throws IOException {
|
|
||||||
List<Model> models = new ArrayList<>();
|
|
||||||
String url = camsoda.getBaseUrl() + "/api/v1/user/current";
|
|
||||||
SiteUiFactory.getUi(camsoda).login();
|
|
||||||
Request request = new Request.Builder().url(url).build();
|
|
||||||
try(Response response = camsoda.getHttpClient().execute(request)) {
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
JSONObject json = new JSONObject(response.body().string());
|
|
||||||
if(json.has("status") && json.getBoolean("status")) {
|
|
||||||
JSONObject user = json.getJSONObject("user");
|
|
||||||
JSONArray following = user.getJSONArray("following");
|
|
||||||
for (int i = 0; i < following.length(); i++) {
|
|
||||||
JSONObject m = following.getJSONObject(i);
|
|
||||||
CamsodaModel model = (CamsodaModel) camsoda.createModel(m.getString("followname"));
|
|
||||||
boolean online = m.getInt("online") == 1;
|
|
||||||
model.setOnlineState(online ? ONLINE : OFFLINE);
|
|
||||||
model.setPreview("https://md.camsoda.com/thumbs/" + model.getName() + ".jpg");
|
|
||||||
models.add(model);
|
|
||||||
}
|
|
||||||
return models.stream()
|
|
||||||
.filter((m) -> {
|
|
||||||
try {
|
|
||||||
return m.isOnline() == showOnline;
|
|
||||||
} catch (IOException | ExecutionException | InterruptedException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
} else {
|
|
||||||
response.close();
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new HttpException(response.code(), response.message());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void showOnline(boolean online) {
|
|
||||||
this.showOnline = online;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,7 @@
|
||||||
package ctbrec.ui.sites.camsoda;
|
package ctbrec.ui.sites.camsoda;
|
||||||
|
|
||||||
|
import static ctbrec.Model.State.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -60,53 +62,55 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
|
||||||
|
|
||||||
protected List<CamsodaModel> loadOnlineModels() throws IOException {
|
protected List<CamsodaModel> loadOnlineModels() throws IOException {
|
||||||
List<CamsodaModel> models = new ArrayList<>();
|
List<CamsodaModel> models = new ArrayList<>();
|
||||||
if(loginRequired && StringUtil.isBlank(ctbrec.Config.getInstance().getSettings().username)) {
|
if(loginRequired && StringUtil.isBlank(ctbrec.Config.getInstance().getSettings().camsodaUsername)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
} else {
|
} else {
|
||||||
String url = CamsodaUpdateService.this.url;
|
|
||||||
LOG.debug("Fetching page {}", url);
|
LOG.debug("Fetching page {}", url);
|
||||||
if(loginRequired) {
|
if(loginRequired) {
|
||||||
SiteUiFactory.getUi(camsoda).login();
|
SiteUiFactory.getUi(camsoda).login();
|
||||||
}
|
}
|
||||||
Request request = new Request.Builder().url(url).build();
|
Request request = new Request.Builder().url(url).build();
|
||||||
try(Response response = camsoda.getHttpClient().execute(request)) {
|
try (Response response = camsoda.getHttpClient().execute(request)) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
JSONObject json = new JSONObject(response.body().string());
|
String body = response.body().string();
|
||||||
if(json.has("status") && json.getBoolean("status")) {
|
JSONObject json = new JSONObject(body);
|
||||||
|
if (json.optBoolean("status")) {
|
||||||
JSONArray template = json.getJSONArray("template");
|
JSONArray template = json.getJSONArray("template");
|
||||||
JSONArray results = json.getJSONArray("results");
|
JSONArray results = json.getJSONArray("results");
|
||||||
for (int i = 0; i < results.length(); i++) {
|
for (int i = 0; i < results.length(); i++) {
|
||||||
JSONObject result = results.getJSONObject(i);
|
JSONObject result = results.getJSONObject(i);
|
||||||
try {
|
try {
|
||||||
if(result.has("tpl")) {
|
CamsodaModel model;
|
||||||
|
if (result.has("tpl")) {
|
||||||
JSONArray tpl = result.getJSONArray("tpl");
|
JSONArray tpl = result.getJSONArray("tpl");
|
||||||
String name = tpl.getString(getTemplateIndex(template, "username"));
|
String name = tpl.getString(getTemplateIndex(template, "username"));
|
||||||
CamsodaModel model = (CamsodaModel) camsoda.createModel(name);
|
model = (CamsodaModel) camsoda.createModel(name);
|
||||||
model.setDescription(tpl.getString(getTemplateIndex(template, "subject_html")));
|
model.setDescription(tpl.getString(getTemplateIndex(template, "subject_html")));
|
||||||
model.setSortOrder(tpl.getFloat(getTemplateIndex(template, "sort_value")));
|
model.setSortOrder(tpl.getFloat(getTemplateIndex(template, "sort_value")));
|
||||||
String preview = "https:" + tpl.getString(getTemplateIndex(template, "thumb"));
|
String preview = "https:" + tpl.getString(getTemplateIndex(template, "thumb"));
|
||||||
model.setPreview(preview);
|
model.setPreview(preview);
|
||||||
String displayName = tpl.getString(getTemplateIndex(template, "display_name"));
|
String displayName = tpl.getString(getTemplateIndex(template, "display_name"));
|
||||||
model.setDisplayName(displayName.replaceAll("[^a-zA-Z0-9]", ""));
|
model.setDisplayName(displayName.replaceAll("[^a-zA-Z0-9]", ""));
|
||||||
if(model.getDisplayName().isBlank()) {
|
if (model.getDisplayName().isBlank()) {
|
||||||
model.setDisplayName(name);
|
model.setDisplayName(name);
|
||||||
}
|
}
|
||||||
model.setNew(result.optBoolean("new"));
|
model.setNew(result.optBoolean("new"));
|
||||||
|
model.setOnlineState(tpl.getString(getTemplateIndex(template, "stream_name")).isEmpty() ? OFFLINE : ONLINE);
|
||||||
models.add(model);
|
models.add(model);
|
||||||
} else {
|
} else {
|
||||||
String name = result.getString("username");
|
String name = result.getString("username");
|
||||||
CamsodaModel model = (CamsodaModel) camsoda.createModel(name);
|
model = (CamsodaModel) camsoda.createModel(name);
|
||||||
model.setSortOrder(result.getFloat("sort_value"));
|
model.setSortOrder(result.getFloat("sort_value"));
|
||||||
if(result.has("status")) {
|
if (result.has("status")) {
|
||||||
model.setOnlineStateByStatus(result.getString("status"));
|
model.setOnlineStateByStatus(result.getString("status"));
|
||||||
}
|
}
|
||||||
if(result.has("display_name")) {
|
if (result.has("display_name")) {
|
||||||
model.setDisplayName(result.getString("display_name").replaceAll("[^a-zA-Z0-9]", ""));
|
model.setDisplayName(result.getString("display_name").replaceAll("[^a-zA-Z0-9]", ""));
|
||||||
if(model.getDisplayName().isBlank()) {
|
if (model.getDisplayName().isBlank()) {
|
||||||
model.setDisplayName(name);
|
model.setDisplayName(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(result.has("thumb")) {
|
if (result.has("thumb")) {
|
||||||
String previewUrl = "https:" + result.getString("thumb");
|
String previewUrl = "https:" + result.getString("thumb");
|
||||||
model.setPreview(previewUrl);
|
model.setPreview(previewUrl);
|
||||||
}
|
}
|
||||||
|
@ -138,4 +142,8 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
|
||||||
}
|
}
|
||||||
throw new NoSuchElementException(string + " not found in template: " + template.toString());
|
throw new NoSuchElementException(string + " not found in template: " + template.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setFilter(Predicate<CamsodaModel> filter) {
|
||||||
|
this.filter = filter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue