forked from j62/ctbrec
1
0
Fork 0

Add follow / unfollow for livejasmin

This commit is contained in:
0xboobface 2018-12-22 20:53:41 +01:00
parent 2425a9dc60
commit c364250440
7 changed files with 230 additions and 10 deletions

View File

@ -0,0 +1,76 @@
package ctbrec.ui.sites.jasmin;
import ctbrec.sites.jasmin.LiveJasmin;
import ctbrec.ui.FollowedTab;
import ctbrec.ui.ThumbOverviewTab;
import javafx.concurrent.WorkerStateEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
public class LiveJasminFollowedTab extends ThumbOverviewTab implements FollowedTab {
private Label status;
public LiveJasminFollowedTab(LiveJasmin liveJasmin) {
super("Followed", new LiveJasminFollowedUpdateService(liveJasmin), liveJasmin);
status = new Label("Logging in...");
grid.getChildren().add(status);
}
@Override
protected void createGui() {
super.createGui();
addOnlineOfflineSelector();
}
private void addOnlineOfflineSelector() {
ToggleGroup group = new ToggleGroup();
RadioButton online = new RadioButton("online");
online.setToggleGroup(group);
RadioButton offline = new RadioButton("offline");
offline.setToggleGroup(group);
pagination.getChildren().add(online);
pagination.getChildren().add(offline);
HBox.setMargin(online, new Insets(5,5,5,40));
HBox.setMargin(offline, new Insets(5,5,5,5));
online.setSelected(true);
group.selectedToggleProperty().addListener((e) -> {
((LiveJasminFollowedUpdateService)updateService).setShowOnline(online.isSelected());
queue.clear();
updateService.restart();
});
}
@Override
protected void onSuccess() {
grid.getChildren().remove(status);
super.onSuccess();
}
@Override
protected void onFail(WorkerStateEvent event) {
status.setText("Login failed");
super.onFail(event);
}
@Override
public void selected() {
status.setText("Logging in...");
super.selected();
}
public void setScene(Scene scene) {
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if(this.isSelected()) {
if(event.getCode() == KeyCode.DELETE) {
follow(selectedThumbCells, false);
}
}
});
}
}

View File

@ -0,0 +1,91 @@
package ctbrec.ui.sites.jasmin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpException;
import ctbrec.sites.jasmin.LiveJasmin;
import ctbrec.sites.jasmin.LiveJasminModel;
import ctbrec.ui.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import okhttp3.Response;
public class LiveJasminFollowedUpdateService extends PaginatedScheduledService {
private static final transient Logger LOG = LoggerFactory.getLogger(LiveJasminFollowedUpdateService.class);
private LiveJasmin liveJasmin;
private String url;
private boolean showOnline = true;
public LiveJasminFollowedUpdateService(LiveJasmin liveJasmin) {
this.liveJasmin = liveJasmin;
long ts = System.currentTimeMillis();
this.url = liveJasmin.getBaseUrl() + "/en/free/favourite/get-favourite-list?_dc=" + ts;
}
@Override
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
@Override
public List<Model> call() throws IOException {
//String _url = url + ((page-1) * 36); // TODO find out how to switch pages
//LOG.debug("Fetching page {}", url);
Request request = new Request.Builder()
.url(url)
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.header("Accept", "*/*")
.header("Accept-Language", "en")
.header("Referer", liveJasmin.getBaseUrl() + "/en/free/favorite")
.header("X-Requested-With", "XMLHttpRequest")
.build();
try (Response response = liveJasmin.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
String body = response.body().string();
List<Model> models = new ArrayList<>();
JSONObject json = new JSONObject(body);
LOG.debug(json.toString(2));
if(json.has("success")) {
JSONObject data = json.getJSONObject("data");
JSONArray performers = data.getJSONArray("performers");
for (int i = 0; i < performers.length(); i++) {
JSONObject m = performers.getJSONObject(i);
String name = m.optString("pid");
if(name.isEmpty()) {
continue;
}
LiveJasminModel model = (LiveJasminModel) liveJasmin.createModel(name);
model.setId(m.getString("id"));
model.setPreview(m.getString("profilePictureUrl"));
Model.State onlineState = LiveJasminModel.mapStatus(m.getInt("status"));
boolean online = onlineState == Model.State.ONLINE;
model.setOnlineState(onlineState);
if(online == showOnline) {
models.add(model);
}
}
} else {
LOG.error("Request failed:\n{}", body);
throw new IOException("Response was not successful");
}
return models;
} else {
throw new HttpException(response.code(), response.message());
}
}
}
};
}
public void setShowOnline(boolean showOnline) {
this.showOnline = showOnline;
}
}

View File

@ -12,6 +12,7 @@ import javafx.scene.control.Tab;
public class LiveJasminTabProvider extends TabProvider {
private LiveJasmin liveJasmin;
private LiveJasminFollowedTab followedTab;
public LiveJasminTabProvider(LiveJasmin liveJasmin) {
this.liveJasmin = liveJasmin;
@ -26,11 +27,16 @@ public class LiveJasminTabProvider extends TabProvider {
ThumbOverviewTab tab = new ThumbOverviewTab("Girls", s, liveJasmin);
tab.setRecorder(liveJasmin.getRecorder());
tabs.add(tab);
followedTab = new LiveJasminFollowedTab(liveJasmin);
followedTab.setRecorder(liveJasmin.getRecorder());
tabs.add(followedTab);
return tabs;
}
@Override
public Tab getFollowedTab() {
return null;
return followedTab;
}
}

View File

@ -35,7 +35,7 @@ public class LiveJasminUpdateService extends PaginatedScheduledService {
return new Task<List<Model>>() {
@Override
public List<Model> call() throws IOException {
String _url = url + ((page-1) * 36); // TODO find out how to switch pages
//String _url = url + ((page-1) * 36); // TODO find out how to switch pages
LOG.debug("Fetching page {}", url);
Request request = new Request.Builder()
.url(url)
@ -64,8 +64,7 @@ public class LiveJasminUpdateService extends PaginatedScheduledService {
LiveJasminModel model = (LiveJasminModel) liveJasmin.createModel(name);
model.setId(m.getString("id"));
model.setPreview(m.getString("profilePictureUrl"));
model.setOnline(true);
model.setOnlineState(ctbrec.Model.State.ONLINE);
model.setOnlineState(LiveJasminModel.mapStatus(m.optInt("status")));
models.add(model);
}
} else {

View File

@ -2,6 +2,7 @@ package ctbrec.sites.jasmin;
import java.io.IOException;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpClient;
import ctbrec.sites.AbstractSite;
@ -77,7 +78,7 @@ public class LiveJasmin extends AbstractSite {
@Override
public boolean supportsFollow() {
return false;
return true;
}
@Override
@ -87,7 +88,7 @@ public class LiveJasmin extends AbstractSite {
@Override
public boolean credentialsAvailable() {
return false;
return !Config.getInstance().getSettings().livejasminSession.isEmpty();
}
}

View File

@ -2,6 +2,7 @@ package ctbrec.sites.jasmin;
import java.io.IOException;
import java.util.Collections;
import java.util.NoSuchElementException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -169,4 +170,13 @@ public class LiveJasminHttpClient extends HttpClient {
}
}
}
public String getSessionId() {
Cookie sessionCookie = getCookieJar().getCookie(HttpUrl.parse("https://www.livejasmin.com"), "session");
if(sessionCookie != null) {
return sessionCookie.value();
} else {
throw new NoSuchElementException("session cookie not found");
}
}
}

View File

@ -89,7 +89,7 @@ public class LiveJasminModel extends AbstractModel {
}
}
private State mapStatus(int status) {
public static State mapStatus(int status) {
switch(status) {
case 0:
return State.OFFLINE;
@ -99,11 +99,17 @@ public class LiveJasminModel extends AbstractModel {
case 3:
return State.PRIVATE;
default:
LOG.debug("Unkown state {} {}", status, getUrl());
LOG.debug("Unkown state {}", status);
return State.UNKNOWN;
}
}
@Override
public void setOnlineState(State status) {
super.setOnlineState(status);
online = status == State.ONLINE;
}
@Override
public List<StreamSource> getStreamSources() throws IOException, ExecutionException, ParseException, PlaylistException {
String masterUrl = getMasterPlaylistUrl();
@ -195,12 +201,43 @@ public class LiveJasminModel extends AbstractModel {
@Override
public boolean follow() throws IOException {
return false;
return follow(true);
}
@Override
public boolean unfollow() throws IOException {
return false;
return follow(false);
}
private boolean follow(boolean follow) throws IOException {
if (id == null) {
loadModelInfo();
}
String sessionId = ((LiveJasminHttpClient) site.getHttpClient()).getSessionId();
String url;
if (follow) {
url = site.getBaseUrl() + "/en/free/favourite/add-favourite?session=" + sessionId + "&performerId=" + id;
} else {
url = site.getBaseUrl() + "/en/free/favourite/delete-favourite?session=" + sessionId + "&performerId=" + id;
}
Request request = new Request.Builder()
.url(url)
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.addHeader("Accept", "*/*")
.addHeader("Accept-Language", "en")
.addHeader("Referer", getUrl())
.addHeader("X-Requested-With", "XMLHttpRequest")
.build();
try (Response response = site.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
String body = response.body().string();
JSONObject json = new JSONObject(body);
return json.optString("status").equalsIgnoreCase("ok");
} else {
throw new HttpException(response.code(), response.message());
}
}
}
public String getId() {