forked from j62/ctbrec
1
0
Fork 0

Add enum to Model for the online states

This commit is contained in:
0xboobface 2018-12-05 15:51:21 +01:00
parent 65e6c5b76e
commit 42177b4399
14 changed files with 186 additions and 74 deletions

View File

@ -110,7 +110,7 @@ public class JavaFxModel implements Model {
} }
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
return delegate.getOnlineState(failFast); return delegate.getOnlineState(failFast);
} }

View File

@ -192,10 +192,6 @@ public class ThumbCell extends StackPane {
setThumbWidth(Config.getInstance().getSettings().thumbWidth); setThumbWidth(Config.getInstance().getSettings().thumbWidth);
setRecording(recording); setRecording(recording);
if(Config.getInstance().getSettings().determineResolution) {
determineResolution();
}
update(); update();
} }
@ -221,11 +217,13 @@ public class ThumbCell extends StackPane {
int[] resolution = resolutionCache.getIfPresent(model); int[] resolution = resolutionCache.getIfPresent(model);
if(resolution != null) { if(resolution != null) {
ThumbOverviewTab.threadPool.submit(() -> {
try { try {
updateResolutionTag(resolution); updateResolutionTag(resolution);
} catch(Exception e) { } catch(Exception e) {
LOG.warn("Couldn't update resolution tag for model {}", model.getName(), e); LOG.warn("Couldn't update resolution tag for model {}", model.getName(), e);
} }
});
} else { } else {
ThumbOverviewTab.threadPool.submit(() -> { ThumbOverviewTab.threadPool.submit(() -> {
try { try {
@ -263,14 +261,14 @@ public class ThumbCell extends StackPane {
private void updateResolutionTag(int[] resolution) throws IOException, ExecutionException, InterruptedException { private void updateResolutionTag(int[] resolution) throws IOException, ExecutionException, InterruptedException {
String _res = "n/a"; String _res = "n/a";
Paint resolutionBackgroundColor = resolutionOnlineColor; Paint resolutionBackgroundColor = resolutionOnlineColor;
String state = model.getOnlineState(false); String state = model.getOnlineState(false).toString();
if (model.isOnline()) { if (model.isOnline()) {
LOG.trace("Model resolution {} {}x{}", model.getName(), resolution[0], resolution[1]); LOG.trace("Model resolution {} {}x{}", model.getName(), resolution[0], resolution[1]);
LOG.trace("Resolution queue size: {}", ThumbOverviewTab.queue.size()); LOG.trace("Resolution queue size: {}", ThumbOverviewTab.queue.size());
final int w = resolution[1]; final int w = resolution[1];
_res = w > 0 ? w != Integer.MAX_VALUE ? Integer.toString(w) : "HD" : state; _res = w > 0 ? w != Integer.MAX_VALUE ? Integer.toString(w) : "HD" : state;
} else { } else {
_res = model.getOnlineState(false); _res = model.getOnlineState(false).toString();
resolutionBackgroundColor = resolutionOfflineColor; resolutionBackgroundColor = resolutionOfflineColor;
} }
final String resText = _res; final String resText = _res;

View File

@ -340,7 +340,6 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
} }
List<Model> models = updateService.getValue(); List<Model> models = updateService.getValue();
updateGrid(models); updateGrid(models);
} }
protected void updateGrid(List<? extends Model> models) { protected void updateGrid(List<? extends Model> models) {

View File

@ -1,5 +1,7 @@
package ctbrec.ui.sites.bonga; package ctbrec.ui.sites.bonga;
import static ctbrec.Model.STATUS.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -58,16 +60,30 @@ public class BongaCamsUpdateService extends PaginatedScheduledService {
BongaCamsModel model = (BongaCamsModel) bongaCams.createModel(name); BongaCamsModel model = (BongaCamsModel) bongaCams.createModel(name);
model.setUserId(m.getInt("user_id")); model.setUserId(m.getInt("user_id"));
boolean away = m.optBoolean("is_away"); boolean away = m.optBoolean("is_away");
boolean online = m.optBoolean("online") && !away; boolean online = m.optBoolean("online");
model.setOnline(online); model.setOnline(online);
if(online) { if(online) {
model.setOnlineState(ONLINE);
if(away) { if(away) {
model.setOnlineState("away"); model.setOnlineState(AWAY);
} else { } else {
model.setOnlineState(m.getString("room")); switch(m.optString("room")) {
case "private":
case "fullprivate":
model.setOnlineState(PRIVATE);
break;
case "group":
case "public":
model.setOnlineState(ONLINE);
break;
default:
LOG.debug(m.optString("room"));
model.setOnlineState(ONLINE);
}
} }
} else { } else {
model.setOnlineState("offline"); model.setOnlineState(OFFLINE);
} }
model.setPreview("https:" + m.getString("thumb_image")); model.setPreview("https:" + m.getString("thumb_image"));
if(m.has("display_name")) { if(m.has("display_name")) {

View File

@ -68,7 +68,7 @@ public class Cam4FollowedUpdateService extends PaginatedScheduledService {
String modelName = path.substring(1); String modelName = path.substring(1);
Cam4Model model = (Cam4Model) site.createModel(modelName); Cam4Model model = (Cam4Model) site.createModel(modelName);
model.setPreview("https://snapshots.xcdnpro.com/thumbnails/"+model.getName()+"?s=" + System.currentTimeMillis()); model.setPreview("https://snapshots.xcdnpro.com/thumbnails/"+model.getName()+"?s=" + System.currentTimeMillis());
model.setOnlineState(parseOnlineState(cellHtml)); model.setOnlineStateByShowType(parseOnlineState(cellHtml));
models.add(model); models.add(model);
} }
return models.stream() return models.stream()

View File

@ -1,5 +1,7 @@
package ctbrec.ui.sites.camsoda; package ctbrec.ui.sites.camsoda;
import static ctbrec.Model.STATUS.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -47,7 +49,7 @@ public class CamsodaFollowedUpdateService extends PaginatedScheduledService {
JSONObject m = following.getJSONObject(i); JSONObject m = following.getJSONObject(i);
CamsodaModel model = (CamsodaModel) camsoda.createModel(m.getString("followname")); CamsodaModel model = (CamsodaModel) camsoda.createModel(m.getString("followname"));
boolean online = m.getInt("online") == 1; boolean online = m.getInt("online") == 1;
model.setOnlineState(online ? "online" : "offline"); model.setOnlineState(online ? ONLINE : OFFLINE);
model.setPreview("https://md.camsoda.com/thumbs/" + model.getName() + ".jpg"); model.setPreview("https://md.camsoda.com/thumbs/" + model.getName() + ".jpg");
models.add(model); models.add(model);
} }

View File

@ -89,7 +89,7 @@ public class CamsodaUpdateService extends PaginatedScheduledService {
model.setSortOrder(result.getFloat("sort_value")); model.setSortOrder(result.getFloat("sort_value"));
models.add(model); models.add(model);
if(result.has("status")) { if(result.has("status")) {
model.setOnlineState(result.getString("status")); model.setOnlineStateByStatus(result.getString("status"));
} }
if(result.has("display_name")) { if(result.has("display_name")) {

View File

@ -21,6 +21,7 @@ public abstract class AbstractModel implements Model {
private int streamUrlIndex = -1; private int streamUrlIndex = -1;
private boolean suspended = false; private boolean suspended = false;
protected Site site; protected Site site;
protected STATUS onlineState = STATUS.UNKNOWN;
@Override @Override
public boolean isOnline() throws IOException, ExecutionException, InterruptedException { public boolean isOnline() throws IOException, ExecutionException, InterruptedException {
@ -121,6 +122,15 @@ public abstract class AbstractModel implements Model {
this.suspended = suspended; this.suspended = suspended;
} }
@Override
public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
return onlineState;
}
public void setOnlineState(STATUS status) {
this.onlineState = status;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;

View File

@ -65,7 +65,7 @@ public interface Model {
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException; public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException;
public String getOnlineState(boolean failFast) throws IOException, ExecutionException; public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException;
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException; public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException;
@ -101,4 +101,6 @@ public interface Model {
public void setSuspended(boolean suspended); public void setSuspended(boolean suspended);
} }

View File

@ -36,7 +36,6 @@ public class BongaCamsModel extends AbstractModel {
private static final transient Logger LOG = LoggerFactory.getLogger(BongaCamsModel.class); private static final transient Logger LOG = LoggerFactory.getLogger(BongaCamsModel.class);
private int userId; private int userId;
private String onlineState = "n/a";
private boolean online = false; private boolean online = false;
private List<StreamSource> streamSources = new ArrayList<>(); private List<StreamSource> streamSources = new ArrayList<>();
private int[] resolution; private int[] resolution;
@ -84,11 +83,11 @@ public class BongaCamsModel extends AbstractModel {
} }
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
return onlineState; return onlineState;
} }
public void setOnlineState(String onlineState) { public void setOnlineState(STATUS onlineState) {
this.onlineState = onlineState; this.onlineState = onlineState;
} }

View File

@ -1,5 +1,7 @@
package ctbrec.sites.cam4; package ctbrec.sites.cam4;
import static ctbrec.Model.STATUS.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -39,22 +41,19 @@ public class Cam4Model extends AbstractModel {
private static final transient Logger LOG = LoggerFactory.getLogger(Cam4Model.class); private static final transient Logger LOG = LoggerFactory.getLogger(Cam4Model.class);
private String playlistUrl; private String playlistUrl;
private String onlineState = "offline";
private int[] resolution = null; private int[] resolution = null;
private boolean privateRoom = false; private boolean privateRoom = false;
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if(ignoreCache || onlineState == null) { if(ignoreCache || onlineState == UNKNOWN) {
try { try {
loadModelDetails(); loadModelDetails();
} catch (ModelDetailsEmptyException e) { } catch (ModelDetailsEmptyException e) {
return false; return false;
} }
} }
return (Objects.equals("NORMAL", onlineState) || Objects.equals("GROUP_SHOW_SELLING_TICKETS", onlineState)) return onlineState == ONLINE && StringUtil.isNotBlank(playlistUrl) && !privateRoom;
&& StringUtil.isNotBlank(playlistUrl)
&& !privateRoom;
} }
private void loadModelDetails() throws IOException, ModelDetailsEmptyException { private void loadModelDetails() throws IOException, ModelDetailsEmptyException {
@ -65,13 +64,17 @@ public class Cam4Model extends AbstractModel {
if(response.isSuccessful()) { if(response.isSuccessful()) {
JSONArray json = new JSONArray(response.body().string()); JSONArray json = new JSONArray(response.body().string());
if(json.length() == 0) { if(json.length() == 0) {
onlineState = "offline"; onlineState = OFFLINE;
throw new ModelDetailsEmptyException("Model details are empty"); throw new ModelDetailsEmptyException("Model details are empty");
} }
JSONObject details = json.getJSONObject(0); JSONObject details = json.getJSONObject(0);
onlineState = details.getString("showType"); String showType = details.getString("showType");
setOnlineStateByShowType(showType);
playlistUrl = details.getString("hlsPreviewUrl"); playlistUrl = details.getString("hlsPreviewUrl");
privateRoom = details.getBoolean("privateRoom"); privateRoom = details.getBoolean("privateRoom");
if(privateRoom) {
onlineState = PRIVATE;
}
if(details.has("resolution")) { if(details.has("resolution")) {
String res = details.getString("resolution"); String res = details.getString("resolution");
String[] tokens = res.split(":"); String[] tokens = res.split(":");
@ -83,9 +86,42 @@ public class Cam4Model extends AbstractModel {
} }
} }
public void setOnlineStateByShowType(String showType) {
switch(showType) {
case "NORMAL":
case "GROUP_SHOW_SELLING_TICKETS":
onlineState = ONLINE;
break;
case "PRIVATE_SHOW":
onlineState = PRIVATE;
break;
case "GROUP_SHOW":
onlineState = GROUP;
break;
case "OFFLINE":
onlineState = OFFLINE;
break;
default:
LOG.debug("Unknown show type {}", showType);
onlineState = UNKNOWN;
}
}
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) {
return onlineState; return onlineState;
} else {
if(onlineState == UNKNOWN) {
try {
loadModelDetails();
} catch (ModelDetailsEmptyException e) {
LOG.warn("Couldn't load model details", e.getMessage());
}
}
return onlineState;
}
} }
private String getPlaylistUrl() throws IOException { private String getPlaylistUrl() throws IOException {
@ -152,7 +188,11 @@ public class Cam4Model extends AbstractModel {
return new int[2]; return new int[2];
} else { } else {
try { try {
if(onlineState != OFFLINE) {
loadModelDetails(); loadModelDetails();
} else {
resolution = new int[2];
}
} catch (Exception e) { } catch (Exception e) {
throw new ExecutionException(e); throw new ExecutionException(e);
} }
@ -226,10 +266,6 @@ public class Cam4Model extends AbstractModel {
this.playlistUrl = playlistUrl; this.playlistUrl = playlistUrl;
} }
public void setOnlineState(String onlineState) {
this.onlineState = onlineState;
}
public class ModelDetailsEmptyException extends Exception { public class ModelDetailsEmptyException extends Exception {
public ModelDetailsEmptyException(String msg) { public ModelDetailsEmptyException(String msg) {
super(msg); super(msg);

View File

@ -1,5 +1,7 @@
package ctbrec.sites.camsoda; package ctbrec.sites.camsoda;
import static ctbrec.Model.STATUS.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
@ -36,7 +38,6 @@ public class CamsodaModel extends AbstractModel {
private static final transient Logger LOG = LoggerFactory.getLogger(CamsodaModel.class); private static final transient Logger LOG = LoggerFactory.getLogger(CamsodaModel.class);
private String streamUrl; private String streamUrl;
private List<StreamSource> streamSources = null; private List<StreamSource> streamSources = null;
private String status = "n/a";
private float sortOrder = 0; private float sortOrder = 0;
int[] resolution = new int[2]; int[] resolution = new int[2];
@ -56,7 +57,8 @@ public class CamsodaModel extends AbstractModel {
JSONObject result = new JSONObject(response.body().string()); JSONObject result = new JSONObject(response.body().string());
if(result.getBoolean("status")) { if(result.getBoolean("status")) {
JSONObject chat = result.getJSONObject("user").getJSONObject("chat"); JSONObject chat = result.getJSONObject("user").getJSONObject("chat");
status = chat.getString("status"); String status = chat.getString("status");
setOnlineStateByStatus(status);
if(chat.has("edge_servers")) { if(chat.has("edge_servers")) {
String edgeServer = chat.getJSONArray("edge_servers").getString(0); String edgeServer = chat.getJSONArray("edge_servers").getString(0);
String streamName = chat.getString("stream_name"); String streamName = chat.getString("stream_name");
@ -71,30 +73,46 @@ public class CamsodaModel extends AbstractModel {
} }
} }
@Override public void setOnlineStateByStatus(String status) {
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { switch(status) {
if(ignoreCache) { case "online":
loadModel(); onlineState = ONLINE;
break;
case "offline":
onlineState = OFFLINE;
break;
case "private":
onlineState = PRIVATE;
break;
case "limited":
onlineState = GROUP;
break;
default:
LOG.debug("Unknown show type {}", status);
onlineState = UNKNOWN;
} }
return Objects.equals(status, "online");
} }
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if(ignoreCache || onlineState == UNKNOWN) {
loadModel();
}
return onlineState == ONLINE;
}
@Override
public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) { if(failFast) {
return status; return onlineState;
} else { } else {
if(status.equals("n/a")) { if(onlineState == UNKNOWN) {
loadModel(); loadModel();
} }
return status; return onlineState;
} }
} }
public void setOnlineState(String state) {
this.status = state;
}
@Override @Override
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException { public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
String streamUrl = getStreamUrl(); String streamUrl = getStreamUrl();

View File

@ -1,5 +1,7 @@
package ctbrec.sites.chaturbate; package ctbrec.sites.chaturbate;
import static ctbrec.Model.STATUS.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -74,14 +76,46 @@ public class ChaturbateModel extends AbstractModel {
getChaturbate().streamInfoCache.invalidate(getName()); getChaturbate().streamInfoCache.invalidate(getName());
} }
public String getOnlineState() throws IOException, ExecutionException { public STATUS getOnlineState() throws IOException, ExecutionException {
return getOnlineState(false); return getOnlineState(false);
} }
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
if(failFast) {
StreamInfo info = getChaturbate().streamInfoCache.getIfPresent(getName()); StreamInfo info = getChaturbate().streamInfoCache.getIfPresent(getName());
return info != null ? info.room_status : "n/a"; setOnlineStateByRoomStatus(info.room_status);
} else {
StreamInfo info = getChaturbate().streamInfoCache.get(getName());
setOnlineStateByRoomStatus(info.room_status);
}
return onlineState;
}
private void setOnlineStateByRoomStatus(String room_status) {
if(room_status != null) {
switch(room_status) {
case "public":
onlineState = ONLINE;
break;
case "offline":
onlineState = OFFLINE;
break;
case "private":
case "hidden":
onlineState = PRIVATE;
break;
case "away":
onlineState = AWAY;
break;
case "group":
onlineState = STATUS.GROUP;
break;
default:
LOG.debug("Unknown show type {}", room_status);
onlineState = STATUS.UNKNOWN;
}
}
} }
public StreamInfo getStreamInfo() throws IOException, ExecutionException { public StreamInfo getStreamInfo() throws IOException, ExecutionException {

View File

@ -68,27 +68,25 @@ public class MyFreeCamsModel extends AbstractModel {
} }
@Override @Override
public String getOnlineState(boolean failFast) throws IOException, ExecutionException { public STATUS getOnlineState(boolean failFast) throws IOException, ExecutionException {
return state != null ? state.toString() : "offline"; switch(this.state) {
case ONLINE:
case RECORDING:
return ctbrec.Model.STATUS.ONLINE;
case AWAY:
return ctbrec.Model.STATUS.AWAY;
case PRIVATE:
return ctbrec.Model.STATUS.PRIVATE;
case GROUP_SHOW:
return ctbrec.Model.STATUS.GROUP;
case OFFLINE:
case CAMOFF:
return ctbrec.Model.STATUS.OFFLINE;
default:
LOG.debug("State {} is not mapped", this.state);
return ctbrec.Model.STATUS.UNKNOWN;
}
} }
// @Override
// public STATUS getOnlineState() {
// switch(this.state) {
// case ONLINE:
// case RECORDING:
// return ctbrec.Model.STATUS.ONLINE;
// case AWAY:
// return ctbrec.Model.STATUS.AWAY;
// case PRIVATE:
// return ctbrec.Model.STATUS.PRIVATE;
// case GROUP_SHOW:
// return ctbrec.Model.STATUS.GROUP;
// default:
// LOG.debug("State {} is not mapped", this.state);
// return ctbrec.Model.STATUS.UNKNOWN;
// }
// }
@Override @Override
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException { public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {