forked from j62/ctbrec
1
0
Fork 0

Fix Cam4 favorites tab

This commit is contained in:
0xb00bface 2020-07-16 19:51:14 +02:00
parent 03b6de626c
commit 4b2e17d0b1
4 changed files with 24 additions and 42 deletions

View File

@ -1,3 +1,10 @@
3.8.3
========================
* Fixed Streamate
* Fixed favorites tab for Cam4; kind of, because only the online tab works. I currently don't see
a way to retrieve the offline favorites
3.8.2
========================
* Fixed misconfiguration in global connection pool, which caused a lot of

View File

@ -3,19 +3,13 @@ package ctbrec.ui.sites.cam4;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpException;
import ctbrec.sites.cam4.Cam4;
import ctbrec.sites.cam4.Cam4Model;
@ -27,7 +21,6 @@ import okhttp3.Response;
public class Cam4FollowedUpdateService extends PaginatedScheduledService {
private static final Logger LOG = LoggerFactory.getLogger(Cam4FollowedUpdateService.class);
private Cam4 site;
private boolean showOnline = true;
@ -50,46 +43,28 @@ public class Cam4FollowedUpdateService extends PaginatedScheduledService {
// login first
SiteUiFactory.getUi(site).login();
List<Model> models = new ArrayList<>();
String username = Config.getInstance().getSettings().cam4Username;
String url = site.getBaseUrl() + '/' + username + "/edit/friends_favorites";
String url = site.getBaseUrl() + "/directoryCams?directoryJson=true&online=" + showOnline + "&url=true&friends=true&favorites=true&resultsPerPage=90";
Request req = new Request.Builder().url(url).build();
try(Response response = site.getHttpClient().execute(req)) {
if(response.isSuccessful()) {
try (Response response = site.getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String content = response.body().string();
Elements cells = HtmlParser.getTags(content, "div#favorites div.ff_thumb");
for (Element cell : cells) {
String cellHtml = cell.html();
Element link = HtmlParser.getTag(cellHtml, "div.ff_img a");
String path = link.attr("href");
String modelName = path.substring(1);
Cam4Model model = (Cam4Model) site.createModel(modelName);
model.setPreview("https://snapshots.xcdnpro.com/thumbnails/"+model.getName()+"?s=" + System.currentTimeMillis());
model.setOnlineStateByShowType(parseOnlineState(cellHtml));
JSONObject json = new JSONObject(content);
JSONArray users = json.getJSONArray("users");
for (int i = 0; i < users.length(); i++) {
JSONObject modelJson = users.getJSONObject(i);
String username = modelJson.optString("username");
Cam4Model model = site.createModel(username);
model.setPreview(modelJson.optString("snapshotImageLink"));
model.setOnlineStateByShowType(modelJson.optString("showType"));
model.setDescription(modelJson.optString("statusMessage"));
models.add(model);
}
return models.stream()
.filter(m -> {
try {
return m.isOnline() == showOnline;
} catch (IOException | ExecutionException e) {
LOG.error("Couldn't determine online state", e);
return false;
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Couldn't determine online state", e);
return false;
}
}).collect(Collectors.toList());
return models;
} else {
throw new HttpException(response.code(), response.message());
}
}
}
private String parseOnlineState(String cellHtml) {
Element state = HtmlParser.getTag(cellHtml, "div.ff_name div");
return state.attr("class").equals("online") ? "NORMAL" : "OFFLINE";
}
};
}

View File

@ -44,7 +44,7 @@ public class Cam4 extends AbstractSite {
}
@Override
public Model createModel(String name) {
public Cam4Model createModel(String name) {
Cam4Model m = new Cam4Model();
m.setSite(this);
m.setName(name);

View File

@ -103,7 +103,7 @@ public class Cam4Model extends AbstractModel {
onlineState = OFFLINE;
break;
default:
LOG.debug("Unknown show type {}", showType);
LOG.debug("Unknown show type [{}]", showType);
onlineState = UNKNOWN;
}