Fix bug in MyFreeCams online detection

This commit is contained in:
0xb00bface 2022-06-11 19:18:23 +02:00
parent 73598fa590
commit cdd8ae0d7a
2 changed files with 52 additions and 70 deletions

View File

@ -22,12 +22,12 @@ import org.slf4j.LoggerFactory;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import static ctbrec.io.HttpConstants.*; import static ctbrec.io.HttpConstants.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.ofNullable; import static java.util.Optional.ofNullable;
public class MyFreeCamsModel extends AbstractModel { public class MyFreeCamsModel extends AbstractModel {
@ -44,7 +44,9 @@ public class MyFreeCamsModel extends AbstractModel {
/** /**
* This constructor exists only for deserialization. Please don't call it directly * This constructor exists only for deserialization. Please don't call it directly
*/ */
public MyFreeCamsModel() {} @SuppressWarnings("unused")
public MyFreeCamsModel() {
}
MyFreeCamsModel(MyFreeCams site) { MyFreeCamsModel(MyFreeCams site) {
this.site = site; this.site = site;
@ -62,6 +64,7 @@ public class MyFreeCamsModel extends AbstractModel {
List<ctbrec.Model> searchResult = MyFreeCamsClient.getInstance().search(getName()); List<ctbrec.Model> searchResult = MyFreeCamsClient.getInstance().search(getName());
if (!searchResult.isEmpty()) { if (!searchResult.isEmpty()) {
MyFreeCamsModel m = (MyFreeCamsModel) searchResult.get(0); MyFreeCamsModel m = (MyFreeCamsModel) searchResult.get(0);
this.state = m.state;
this.onlineState = m.getOnlineState(true); this.onlineState = m.getOnlineState(true);
} }
} }
@ -248,20 +251,10 @@ public class MyFreeCamsModel extends AbstractModel {
setPreview(previewUrl); setPreview(previewUrl);
// tags // tags
ofNullable(state.getM()).map(Model::getTags).ifPresent(tags -> { ofNullable(state.getM()).map(Model::getTags).ifPresent(tags -> setTags(new ArrayList<>(tags)));
ArrayList<String> t = new ArrayList<>();
t.addAll(tags);
setTags(t);
});
// description // description
ofNullable(state.getM()).map(Model::getTopic).ifPresent(topic -> { ofNullable(state.getM()).map(Model::getTopic).ifPresent(topic -> setDescription(URLDecoder.decode(topic, UTF_8)));
try {
setDescription(URLDecoder.decode(topic, "utf-8"));
} catch (UnsupportedEncodingException e) {
LOG.warn("Couldn't url decode topic", e);
}
});
viewerCount = ofNullable(state.getM()).map(Model::getRc).orElse(0); viewerCount = ofNullable(state.getM()).map(Model::getRc).orElse(0);
} }

View File

@ -15,38 +15,27 @@ public enum State {
OFFLINE("offline"), OFFLINE("offline"),
UNKNOWN("unknown"); UNKNOWN("unknown");
String literal; final String literal;
State(String literal) { State(String literal) {
this.literal = literal; this.literal = literal;
} }
public static State of(Integer vs) { public static State of(Integer vs) {
Integer s = Optional.ofNullable(vs).orElse(Integer.MAX_VALUE); Integer s = Optional.ofNullable(vs).orElse(Integer.MAX_VALUE);
switch (s) { return switch (s) {
case 0: case 0 -> ONLINE;
return ONLINE; case 90 -> CAMOFF;
case 90: case -4 -> RECORDING;
return CAMOFF; case -3 -> INCLUDE;
case -4: case -2 -> EXCLUDE;
return RECORDING; case -1 -> DELETE;
case -3: case 2 -> AWAY;
return INCLUDE; case 12, 91 -> PRIVATE;
case -2: case 13 -> GROUP_SHOW;
return EXCLUDE; case 127 -> OFFLINE;
case -1: default -> UNKNOWN;
return DELETE; };
case 2:
return AWAY;
case 12:
case 91:
return PRIVATE;
case 13:
return GROUP_SHOW;
case 127:
return OFFLINE;
default:
return UNKNOWN;
}
} }
@Override @Override