From 5adb980da66ada0111fac0d4e3fb5e760c2e7170 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 8 Oct 2018 15:23:20 +0200 Subject: [PATCH] Add CTBREC_DEV mode for all token related requests If the env virable CTBREC_DEV is set to 1 all token related requests are simulated and not actually send to chaturbate. --- src/main/java/ctbrec/Model.java | 30 +++++++------- .../java/ctbrec/ui/CtbrecApplication.java | 32 ++++++++------- src/main/java/ctbrec/ui/TipDialog.java | 41 +++++++++++-------- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/src/main/java/ctbrec/Model.java b/src/main/java/ctbrec/Model.java index 2af0b540..d1f92de3 100644 --- a/src/main/java/ctbrec/Model.java +++ b/src/main/java/ctbrec/Model.java @@ -220,20 +220,22 @@ public class Model { } public void sendTip(String name, int tokens) throws IOException { - RequestBody body = new FormBody.Builder() - .add("csrfmiddlewaretoken", client.getToken()) - .add("tip_amount", Integer.toString(tokens)) - .add("tip_room_type", "public") - .build(); - Request req = new Request.Builder() - .url("https://chaturbate.com/tipping/send_tip/"+name+"/") - .post(body) - .addHeader("Referer", "https://chaturbate.com/"+name+"/") - .addHeader("X-Requested-With", "XMLHttpRequest") - .build(); - try(Response response = client.execute(req, true)) { - if(!response.isSuccessful()) { - throw new IOException(response.code() + " " + response.message()); + if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) { + RequestBody body = new FormBody.Builder() + .add("csrfmiddlewaretoken", client.getToken()) + .add("tip_amount", Integer.toString(tokens)) + .add("tip_room_type", "public") + .build(); + Request req = new Request.Builder() + .url("https://chaturbate.com/tipping/send_tip/"+name+"/") + .post(body) + .addHeader("Referer", "https://chaturbate.com/"+name+"/") + .addHeader("X-Requested-With", "XMLHttpRequest") + .build(); + try(Response response = client.execute(req, true)) { + if(!response.isSuccessful()) { + throw new IOException(response.code() + " " + response.message()); + } } } } diff --git a/src/main/java/ctbrec/ui/CtbrecApplication.java b/src/main/java/ctbrec/ui/CtbrecApplication.java index 6d7a671c..6cfec90a 100644 --- a/src/main/java/ctbrec/ui/CtbrecApplication.java +++ b/src/main/java/ctbrec/ui/CtbrecApplication.java @@ -194,22 +194,26 @@ public class CtbrecApplication extends Application { Task task = new Task() { @Override protected Integer call() throws Exception { - String username = Config.getInstance().getSettings().username; - if (username == null || username.trim().isEmpty()) { - throw new IOException("Not logged in"); - } + if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) { + String username = Config.getInstance().getSettings().username; + if (username == null || username.trim().isEmpty()) { + throw new IOException("Not logged in"); + } - String url = "https://chaturbate.com/p/" + username + "/"; - HttpClient client = HttpClient.getInstance(); - Request req = new Request.Builder().url(url).build(); - Response resp = client.execute(req, true); - if (resp.isSuccessful()) { - String profilePage = resp.body().string(); - String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); - int tokens = Integer.parseInt(tokenText); - return tokens; + String url = "https://chaturbate.com/p/" + username + "/"; + HttpClient client = HttpClient.getInstance(); + Request req = new Request.Builder().url(url).build(); + Response resp = client.execute(req, true); + if (resp.isSuccessful()) { + String profilePage = resp.body().string(); + String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); + int tokens = Integer.parseInt(tokenText); + return tokens; + } else { + throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); + } } else { - throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); + return 1_000_000; } } diff --git a/src/main/java/ctbrec/ui/TipDialog.java b/src/main/java/ctbrec/ui/TipDialog.java index ec28a3d4..ec728d34 100644 --- a/src/main/java/ctbrec/ui/TipDialog.java +++ b/src/main/java/ctbrec/ui/TipDialog.java @@ -3,6 +3,7 @@ package ctbrec.ui; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ExecutionException; import org.slf4j.Logger; @@ -36,26 +37,30 @@ public class TipDialog extends TextInputDialog { Task task = new Task() { @Override protected Integer call() throws Exception { - String username = Config.getInstance().getSettings().username; - if (username == null || username.trim().isEmpty()) { - throw new IOException("Not logged in"); - } + if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) { + String username = Config.getInstance().getSettings().username; + if (username == null || username.trim().isEmpty()) { + throw new IOException("Not logged in"); + } - String url = "https://chaturbate.com/p/" + username + "/"; - HttpClient client = HttpClient.getInstance(); - Request req = new Request.Builder().url(url).build(); - Response resp = client.execute(req, true); - if (resp.isSuccessful()) { - String profilePage = resp.body().string(); - String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); - int tokens = Integer.parseInt(tokenText); - Map event = new HashMap<>(); - event.put("event", "tokens"); - event.put("amount", tokens); - CtbrecApplication.bus.post(event); - return tokens; + String url = "https://chaturbate.com/p/" + username + "/"; + HttpClient client = HttpClient.getInstance(); + Request req = new Request.Builder().url(url).build(); + Response resp = client.execute(req, true); + if (resp.isSuccessful()) { + String profilePage = resp.body().string(); + String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); + int tokens = Integer.parseInt(tokenText); + Map event = new HashMap<>(); + event.put("event", "tokens"); + event.put("amount", tokens); + CtbrecApplication.bus.post(event); + return tokens; + } else { + throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); + } } else { - throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); + return 1_000_000; } }