diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-04-25 12:55:46 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-04-25 12:55:46 +0200 |
commit | d03f2bca4a7d33b1347db487aa588a5d163e5615 (patch) | |
tree | 58e4df0c9f025b69898ad84a998bb8814b209677 /src/main/Main.java | |
parent | cc55914d61978b46ec8e3673c4b37fdc3b392e48 (diff) | |
download | TwitterDataAnalytics-d03f2bca4a7d33b1347db487aa588a5d163e5615.tar.gz |
Merged and resolved conflicts.
* No functionality of Command is currently used.
Diffstat (limited to 'src/main/Main.java')
-rw-r--r-- | src/main/Main.java | 152 |
1 files changed, 146 insertions, 6 deletions
diff --git a/src/main/Main.java b/src/main/Main.java index d1b2540..917aaeb 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,17 +1,158 @@ 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 { - // create the api state to run request streams or REST. - TwitterApi api = TwitterApi.getAppOnly(); + try { + 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(); + } + } +} + +/** + * OLD main + * public static void main(String[] args) throws IOException { + * TwitterApi api = TwitterApi.getAppOnly(); // create the object that queues and executes the commands. CommandQueue queue = new CommandQueue(); @@ -29,7 +170,6 @@ public class Main { queue.executeAll(); // parse new input. - parser.parse(input.nextLine()); + parser.parse(scanner.nextLine()); } - } -} + */
\ No newline at end of file |