summaryrefslogtreecommitdiff
path: root/src/main/Main.java
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-24 19:55:03 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-25 01:40:09 +0200
commit1594e845a2ddfbc6816f1d08fce9546eec3d715f (patch)
tree9b80a9caceee523d3c7634463fc1b7f0ae5fe807 /src/main/Main.java
parent35026b912bec0a20cd421d0ffc1bfe93ffe8777f (diff)
downloadTwitterDataAnalytics-1594e845a2ddfbc6816f1d08fce9546eec3d715f.tar.gz
Main is a command line program now
Test with: ./run.sh
Diffstat (limited to 'src/main/Main.java')
-rw-r--r--src/main/Main.java143
1 files changed, 134 insertions, 9 deletions
diff --git a/src/main/Main.java b/src/main/Main.java
index 20bbec9..a62232a 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -1,25 +1,150 @@
package main;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Scanner;
import mining.TwitterApi;
import org.json.JSONException;
import org.json.JSONObject;
/**
- *
+ * Class for manually testing the Twitter API.
*/
public class Main {
+ /**
+ * Command and parameters without options.
+ */
+ private Command command;
+ private String[] params;
+ /**
+ * Whether to use the Bearer method or OAuth-signed requests.
+ */
+ private boolean useBearer = true;
+ private TwitterApi api_cached;
+
+ public Main(String[] args) throws IOException {
+ // parse options and command and return the parameters.
+ parseGlobalOptions(args);
+ }
+
+ private TwitterApi getApi() throws IOException {
+ if (api_cached == null) {
+ if (useBearer) {
+ api_cached = TwitterApi.getAppOnly();
+ } else {
+ api_cached = TwitterApi.getOAuth(new ConsolePinSupplier());
+ }
+ }
+ return api_cached;
+ }
+
+ private String getParam(int index, String name) {
+ if (index >= params.length) {
+ System.err.println("Missing parameter: " + name);
+ System.exit(1);
+ }
+ return params[index];
+ }
+
+ private void parseGlobalOptions(String[] args) {
+ int firstParam = -1;
+ /* parse global options */
+ for (int i = 0; i < args.length; i++) {
+ if ("--oauth".equals(args[i])) {
+ useBearer = false;
+ } else if (args[i].startsWith("-")) {
+ throw new IllegalArgumentException("Invalid option: " + args[i]);
+ } else {
+ /* not an option, must be a command */
+ command = Command.fromString(args[i]);
+ firstParam = i + 1;
+ break;
+ }
+ }
+ if (firstParam == -1) {
+ throw new IllegalArgumentException("Missing command, use \"help\"");
+ }
+ params = Arrays.copyOfRange(args, firstParam, args.length);
+ }
+
public static void main(String[] args) throws IOException {
- TwitterApi api = TwitterApi.getAppOnly();
try {
- JSONObject profile = api.build("users/show")
- .param("screen_name", "Lekensteyn")
- .request();
- System.out.println(profile.toString(4));
- } catch (JSONException ex) {
- /* cannot happen */
- throw new RuntimeException(ex);
+ Main main = new Main(args);
+ main.execute();
+ } catch (IllegalArgumentException ex) {
+ System.err.println(ex.getMessage());
+ System.exit(1);
+ }
+ }
+
+ enum Command {
+
+ user,
+ help;
+
+ public static Command fromString(String command) {
+ for (Command cmd : values()) {
+ if (cmd.name().equals(command)) {
+ return cmd;
+ }
+ }
+ throw new IllegalArgumentException("Unrecognized command ");
+ }
+ };
+
+ private final static String[] HELP = {
+ "Global options:",
+ " --oauth Use OAuth (PIN) instead of Bearer tokens",
+ "",
+ "Available commands:"
+ };
+
+ public void execute() throws IOException {
+ TwitterApi.Builder req = null;
+ /* build a request for commands */
+ switch (command) {
+ case user:
+ req = getApi().build("users/show");
+ req.param("screen_name", getParam(0, "screen name"));
+ break;
+ case help:
+ for (String line : HELP) {
+ System.out.println(line);
+ }
+ for (Command cmd : Command.values()) {
+ System.out.println(" " + cmd.name());
+ }
+ break;
+ default:
+ throw new AssertionError(command.name());
+ }
+ if (req != null) {
+ System.err.println("Executing: " + req.toString());
+ JSONObject result = req.request();
+ try {
+ System.out.println(result.toString(4));
+ } catch (JSONException ex) {
+ /* cannot happen */
+ System.err.println("Warning: got JSON exception: " + ex);
+ System.out.println(result);
+ }
+ }
+ }
+
+ private static class ConsolePinSupplier implements TwitterApi.PinSupplier {
+
+ private final Scanner scanner;
+
+ public ConsolePinSupplier() {
+ scanner = new Scanner(System.in);
+ }
+
+ @Override
+ public String requestPin(String url) throws IOException {
+ System.out.println(url);
+ System.err.println("Please open the above URL and enter PIN:");
+ return scanner.nextLine();
}
}
}