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 3.8.2
======================== ========================
* Fixed misconfiguration in global connection pool, which caused a lot of * 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.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.jsoup.nodes.Element; import org.json.JSONArray;
import org.jsoup.select.Elements; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.sites.cam4.Cam4; import ctbrec.sites.cam4.Cam4;
import ctbrec.sites.cam4.Cam4Model; import ctbrec.sites.cam4.Cam4Model;
@ -27,7 +21,6 @@ import okhttp3.Response;
public class Cam4FollowedUpdateService extends PaginatedScheduledService { public class Cam4FollowedUpdateService extends PaginatedScheduledService {
private static final Logger LOG = LoggerFactory.getLogger(Cam4FollowedUpdateService.class);
private Cam4 site; private Cam4 site;
private boolean showOnline = true; private boolean showOnline = true;
@ -50,46 +43,28 @@ public class Cam4FollowedUpdateService extends PaginatedScheduledService {
// login first // login first
SiteUiFactory.getUi(site).login(); SiteUiFactory.getUi(site).login();
List<Model> models = new ArrayList<>(); List<Model> models = new ArrayList<>();
String username = Config.getInstance().getSettings().cam4Username; String url = site.getBaseUrl() + "/directoryCams?directoryJson=true&online=" + showOnline + "&url=true&friends=true&favorites=true&resultsPerPage=90";
String url = site.getBaseUrl() + '/' + username + "/edit/friends_favorites";
Request req = new Request.Builder().url(url).build(); Request req = new Request.Builder().url(url).build();
try(Response response = site.getHttpClient().execute(req)) { try (Response response = site.getHttpClient().execute(req)) {
if(response.isSuccessful()) { if (response.isSuccessful()) {
String content = response.body().string(); String content = response.body().string();
Elements cells = HtmlParser.getTags(content, "div#favorites div.ff_thumb"); JSONObject json = new JSONObject(content);
for (Element cell : cells) { JSONArray users = json.getJSONArray("users");
String cellHtml = cell.html(); for (int i = 0; i < users.length(); i++) {
Element link = HtmlParser.getTag(cellHtml, "div.ff_img a"); JSONObject modelJson = users.getJSONObject(i);
String path = link.attr("href"); String username = modelJson.optString("username");
String modelName = path.substring(1); Cam4Model model = site.createModel(username);
Cam4Model model = (Cam4Model) site.createModel(modelName); model.setPreview(modelJson.optString("snapshotImageLink"));
model.setPreview("https://snapshots.xcdnpro.com/thumbnails/"+model.getName()+"?s=" + System.currentTimeMillis()); model.setOnlineStateByShowType(modelJson.optString("showType"));
model.setOnlineStateByShowType(parseOnlineState(cellHtml)); model.setDescription(modelJson.optString("statusMessage"));
models.add(model); models.add(model);
} }
return models.stream() return models;
.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());
} else { } else {
throw new HttpException(response.code(), response.message()); 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 @Override
public Model createModel(String name) { public Cam4Model createModel(String name) {
Cam4Model m = new Cam4Model(); Cam4Model m = new Cam4Model();
m.setSite(this); m.setSite(this);
m.setName(name); m.setName(name);

View File

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