forked from j62/ctbrec
1
0
Fork 0

Core cleanup

This commit is contained in:
0xb00bface 2020-12-21 20:05:22 +01:00
parent 97715aecc5
commit 086e15578f
2 changed files with 74 additions and 58 deletions

View File

@ -13,6 +13,8 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model;
@ -28,6 +30,8 @@ import okhttp3.Response;
public class MVLive extends AbstractSite {
private static final Logger LOG = LoggerFactory.getLogger(MVLive.class);
public static final String APP_HOST = "app-v1.live.manyvids.com";
public static final String WS_URL = "wss://" + APP_HOST;
public static final String WS_ORIGIN = "https://live.manyvids.com";
@ -123,46 +127,51 @@ public class MVLive extends AbstractSite {
.build();
try (Response response = getHttpClient().execute(request)) {
if (response.isSuccessful()) {
List<Model> models = new ArrayList<>();
String content = response.body().string();
Elements cards = HtmlParser.getTags(content, "div[class*=-model]");
for (Element card : cards) {
try {
String cardHtml = card.html();
Element link = HtmlParser.getTag(cardHtml, "a");
link.setBaseUri(getBaseUrl());
String name = HtmlParser.getText(cardHtml, "h4 a");
MVLiveModel model = createModel(name);
model.setUrl(link.absUrl("href"));
Element thumb = HtmlParser.getTag(cardHtml, "a img.b-lazy");
thumb.setBaseUri(getBaseUrl());
model.setPreview(thumb.absUrl("data-src"));
Element status = HtmlParser.getTag(cardHtml, "h4[class~=profile-pic-name]");
String cssClass = status.attr("class");
if(cssClass.contains("live")) {
model.setOnlineState(Model.State.ONLINE);
} else if(cssClass.contains("private")) {
model.setOnlineState(Model.State.PRIVATE);
} else {
model.setOnlineState(Model.State.UNKNOWN);
}
models.add(model);
} catch(RuntimeException e) {
if(e.getMessage().contains("No element selected by")) {
// ignore
} else {
throw e;
}
}
}
return models;
return parseModelCards(cards);
} else {
throw new HttpException(response.code(), response.message());
}
}
}
private List<Model> parseModelCards(Elements cards) {
List<Model> models = new ArrayList<>();
for (Element card : cards) {
try {
String cardHtml = card.html();
Element link = HtmlParser.getTag(cardHtml, "a");
link.setBaseUri(getBaseUrl());
String name = HtmlParser.getText(cardHtml, "h4 a");
MVLiveModel model = createModel(name);
model.setUrl(link.absUrl("href"));
Element thumb = HtmlParser.getTag(cardHtml, "a img.b-lazy");
thumb.setBaseUri(getBaseUrl());
model.setPreview(thumb.absUrl("data-src"));
Element status = HtmlParser.getTag(cardHtml, "h4[class~=profile-pic-name]");
String cssClass = status.attr("class");
if(cssClass.contains("live")) {
model.setOnlineState(Model.State.ONLINE);
} else if(cssClass.contains("private")) {
model.setOnlineState(Model.State.PRIVATE);
} else {
LOG.debug("Unknown online state {}", cssClass);
model.setOnlineState(Model.State.UNKNOWN);
}
models.add(model);
} catch(RuntimeException e) {
if(e.getMessage().contains("No element selected by")) {
// ignore
} else {
throw e;
}
}
}
return models;
}
@Override
public boolean supportsSearch() {
return true;
@ -213,29 +222,7 @@ public class MVLive extends AbstractSite {
try (Response response = getHttpClient().execute(request)) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JSONObject json = new JSONObject(responseBody);
if(json.has("stars")) {
JSONArray stars = json.getJSONArray("stars");
for (int i = 0; i < stars.length(); i++) {
JSONObject star = stars.getJSONObject(i);
String name = star.getString("label");
MVLiveModel model = createModel(name);
long id = star.getLong("id");
String url = "https://www.manyvids.com/MVLive/" + model.getDisplayName() + '/' + id + '/';
model.setUrl(url);
model.setPreview(star.getString("img"));
if (star.optString("is_live").equals("1")) {
if (star.optString("is_private").equals("1")) {
model.setOnlineState(State.PRIVATE);
} else {
model.setOnlineState(State.ONLINE);
}
} else {
model.setOnlineState(State.OFFLINE);
}
result.add(model);
}
}
parseSearchResult(result, responseBody);
} else {
throw new HttpException(response.code(), response.message());
}
@ -243,6 +230,32 @@ public class MVLive extends AbstractSite {
return result;
}
private void parseSearchResult(List<Model> result, String responseBody) {
JSONObject json = new JSONObject(responseBody);
if(json.has("stars")) {
JSONArray stars = json.getJSONArray("stars");
for (int i = 0; i < stars.length(); i++) {
JSONObject star = stars.getJSONObject(i);
String name = star.getString("label");
MVLiveModel model = createModel(name);
long id = star.getLong("id");
String url = "https://www.manyvids.com/MVLive/" + model.getDisplayName() + '/' + id + '/';
model.setUrl(url);
model.setPreview(star.getString("img"));
if (star.optString("is_live").equals("1")) {
if (star.optString("is_private").equals("1")) {
model.setOnlineState(State.PRIVATE);
} else {
model.setOnlineState(State.ONLINE);
}
} else {
model.setOnlineState(State.OFFLINE);
}
result.add(model);
}
}
}
@Override
public Model createModelFromUrl(String url) {
Matcher m = Pattern.compile("https://live.manyvids.com/(?:stream/)?(.*?)(?:/.*?)?").matcher(url.trim());

View File

@ -31,10 +31,12 @@ import com.iheartradio.m3u8.data.PlaylistData;
import ctbrec.AbstractModel;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.NotImplementedExcetion;
import ctbrec.StringUtil;
import ctbrec.io.HttpException;
import ctbrec.recorder.download.Download;
import ctbrec.recorder.download.StreamSource;
import ctbrec.sites.ModelOfflineException;
import okhttp3.Request;
import okhttp3.Response;
@ -42,8 +44,8 @@ public class MVLiveModel extends AbstractModel {
private static final transient Logger LOG = LoggerFactory.getLogger(MVLiveModel.class);
private MVLiveHttpClient httpClient;
private MVLiveClient client;
private transient MVLiveHttpClient httpClient;
private transient MVLiveClient client;
private String roomNumber;
@Override
@ -138,7 +140,7 @@ public class MVLiveModel extends AbstractModel {
roomNumber = json.getString("floorId");
} else {
LOG.debug("Room number response: {}", json.toString(2));
throw new RuntimeException(getName() + " is offline");
throw new ModelOfflineException(this);
}
}
return roomNumber;
@ -194,6 +196,7 @@ public class MVLiveModel extends AbstractModel {
@Override
public void receiveTip(Double tokens) throws IOException {
throw new NotImplementedExcetion("Sending tips is not implemeted for MVLive");
}
@Override