forked from j62/ctbrec
1
0
Fork 0

Fix CamSoda followed tab

This commit is contained in:
0xb00bface 2020-07-16 21:28:47 +02:00
parent 4b2e17d0b1
commit e202c946ac
3 changed files with 42 additions and 95 deletions

View File

@ -1,6 +1,9 @@
package ctbrec.ui.sites.camsoda;
import java.util.function.Predicate;
import ctbrec.sites.camsoda.Camsoda;
import ctbrec.sites.camsoda.CamsodaModel;
import ctbrec.ui.tabs.FollowedTab;
import ctbrec.ui.tabs.ThumbOverviewTab;
import javafx.concurrent.WorkerStateEvent;
@ -18,9 +21,10 @@ public class CamsodaFollowedTab extends ThumbOverviewTab implements FollowedTab
boolean showOnline = true;
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...");
grid.getChildren().add(status);
((CamsodaUpdateService)updateService).setFilter(createFilter(this));
}
@Override
@ -40,9 +44,9 @@ public class CamsodaFollowedTab extends ThumbOverviewTab implements FollowedTab
HBox.setMargin(online, new Insets(5, 5, 5, 40));
HBox.setMargin(offline, new Insets(5, 5, 5, 5));
online.setSelected(true);
group.selectedToggleProperty().addListener((e) -> {
group.selectedToggleProperty().addListener(e -> {
showOnline = online.isSelected();
queue.clear();
((CamsodaFollowedUpdateService)updateService).showOnline(online.isSelected());
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;
}
};
}
}

View File

@ -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;
}
}

View File

@ -1,5 +1,7 @@
package ctbrec.ui.sites.camsoda;
import static ctbrec.Model.State.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
@ -60,53 +62,55 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
protected List<CamsodaModel> loadOnlineModels() throws IOException {
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();
} else {
String url = CamsodaUpdateService.this.url;
LOG.debug("Fetching page {}", url);
if(loginRequired) {
SiteUiFactory.getUi(camsoda).login();
}
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()) {
JSONObject json = new JSONObject(response.body().string());
if(json.has("status") && json.getBoolean("status")) {
String body = response.body().string();
JSONObject json = new JSONObject(body);
if (json.optBoolean("status")) {
JSONArray template = json.getJSONArray("template");
JSONArray results = json.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
try {
if(result.has("tpl")) {
CamsodaModel model;
if (result.has("tpl")) {
JSONArray tpl = result.getJSONArray("tpl");
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.setSortOrder(tpl.getFloat(getTemplateIndex(template, "sort_value")));
String preview = "https:" + tpl.getString(getTemplateIndex(template, "thumb"));
model.setPreview(preview);
String displayName = tpl.getString(getTemplateIndex(template, "display_name"));
model.setDisplayName(displayName.replaceAll("[^a-zA-Z0-9]", ""));
if(model.getDisplayName().isBlank()) {
if (model.getDisplayName().isBlank()) {
model.setDisplayName(name);
}
model.setNew(result.optBoolean("new"));
model.setOnlineState(tpl.getString(getTemplateIndex(template, "stream_name")).isEmpty() ? OFFLINE : ONLINE);
models.add(model);
} else {
String name = result.getString("username");
CamsodaModel model = (CamsodaModel) camsoda.createModel(name);
model = (CamsodaModel) camsoda.createModel(name);
model.setSortOrder(result.getFloat("sort_value"));
if(result.has("status")) {
if (result.has("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]", ""));
if(model.getDisplayName().isBlank()) {
if (model.getDisplayName().isBlank()) {
model.setDisplayName(name);
}
}
if(result.has("thumb")) {
if (result.has("thumb")) {
String previewUrl = "https:" + result.getString("thumb");
model.setPreview(previewUrl);
}
@ -138,4 +142,8 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
}
throw new NoSuchElementException(string + " not found in template: " + template.toString());
}
public void setFilter(Predicate<CamsodaModel> filter) {
this.filter = filter;
}
}