diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-04-25 12:46:12 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-04-25 12:46:12 +0200 |
commit | cc55914d61978b46ec8e3673c4b37fdc3b392e48 (patch) | |
tree | bcee0ff32af56afb4ed19ac2f07f87640fd6ea23 /src/main | |
parent | 00c8957e2bbb43b167688a877a0f9a908db03c07 (diff) | |
download | TwitterDataAnalytics-cc55914d61978b46ec8e3673c4b37fdc3b392e48.tar.gz |
Added the functionality to execute and parse commands.
* CommandParser can parse input strings to RequestCommand.
* CommandQueue will execute all ICommands in a queue.
* TODO: StreamCommands, database implementation.
* RequestCommand writes into database.txt
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/CommandParser.java | 113 | ||||
-rw-r--r-- | src/main/CommandQueue.java | 39 | ||||
-rw-r--r-- | src/main/ICommand.java | 14 | ||||
-rw-r--r-- | src/main/Main.java | 32 | ||||
-rw-r--r-- | src/main/RequestCommand.java | 118 | ||||
-rw-r--r-- | src/main/StreamCommand.java | 10 |
6 files changed, 314 insertions, 12 deletions
diff --git a/src/main/CommandParser.java b/src/main/CommandParser.java new file mode 100644 index 0000000..97f01fc --- /dev/null +++ b/src/main/CommandParser.java @@ -0,0 +1,113 @@ +package main; + +import java.lang.reflect.Array; +import java.util.Arrays; +import mining.TwitterApi; + +/** + * Class that creates executable commands from input. + * @author Maurice Laveaux + */ +public class CommandParser { + // the queue of commands. + private final CommandQueue m_queue; + + // the twitter api. + private final TwitterApi m_api; + + /** + * Default constructor, requires the TwitterApi + * @param queue A valid CommandQueue that can execute requests. + * @param api the TwitterApi to use for the commands. + */ + public CommandParser(CommandQueue queue, TwitterApi api) { + m_queue = queue; + m_api = api; + } + + /** + * Parses a string and creates an executable command for it. + * @param command The string to parse the command from. + */ + final public void parse(final String command) { + // split the code for each space ' '. + String[] seperated = command.split(" "); + + // parse the seperated code for each argument. + parse(seperated); + } + + /** + * Parses a string and creates an executable command for it. + * @param seperated A space seperated string for each argument. + */ + final public void parse(final String[] seperated) { + if (seperated.length == 0) { + return; + } + + // The main command. + String command = seperated[0]; + + // the first command issued + switch (command) { + case "help": + showFullHelp(); + return; + case "-h": + showFullHelp(); + return; + case "quit": + // TODO: implement less hack way of quiting the program. + System.exit(0); + default: + break; + } + + // there is a command without errors but it was not switched. + if (seperated.length == 1) { + showUsageHelp(command); + return; + } + + // commands with multiple parameters + String[] extra = Arrays.copyOfRange(seperated, 1, seperated.length); + + if (seperated.length < 3) { + showUsageHelp(extra.toString()); + return; + } + + String[] parameters = Arrays.copyOfRange(extra, 1, extra.length); + + try { + switch (command) { + case "get": + m_queue.add(new RequestCommand(m_api, extra[0], parameters)); + return; + } + } + catch (IllegalArgumentException ex) { + System.out.println(ex); + } + } + + /** Show the basic usage help in console */ + private void showUsageHelp(String unknown) { + System.out.println("Unknown argument: \"" + unknown + "\"."); + System.out.println(" Use help, -h for more help."); + } + + /** Show the full manual page in the console */ + private void showFullHelp() { + System.out.println("Usage: [command] [arguments], see below for options."); + + System.out.println("\n command:"); + System.out.println(" set, use the twitter Search/REST API."); + System.out.println(" stream, use the twitter Stream API."); + + System.out.println("\n arguments:"); + System.out.println(" <main>, the information to get from api.twitter.com."); + System.out.println(" <json_member>, any number of arguments for the information"); + } +}
\ No newline at end of file diff --git a/src/main/CommandQueue.java b/src/main/CommandQueue.java new file mode 100644 index 0000000..b3fde3e --- /dev/null +++ b/src/main/CommandQueue.java @@ -0,0 +1,39 @@ +package main; + +import java.util.LinkedList; +import java.util.Queue; + +/** + * The queue of commands to be executed every step. + * + * @author Maurice Laveaux + */ +public class CommandQueue { + + // the list of commands executed in this order. + private final Queue<ICommand> m_commands; + + public CommandQueue() { + m_commands = new LinkedList(); + } + + /** + * Add another command to the last position in the queue. + * + * @param command Any command that can be executed. + */ + public void add(ICommand command) { + m_commands.offer(command); + } + + /** + * Execute every command that is in the queue. + */ + public void executeAll() { + while (!m_commands.isEmpty()) { + ICommand command = m_commands.poll(); + + command.execute(); + } + } +} diff --git a/src/main/ICommand.java b/src/main/ICommand.java new file mode 100644 index 0000000..14419a2 --- /dev/null +++ b/src/main/ICommand.java @@ -0,0 +1,14 @@ +package main; + +/** + * The standard interface for any command + * + * @author Maurice Laveaux + */ +public interface ICommand { + + /** + * Execute all the steps to complete this Command. + */ + public void execute(); +} diff --git a/src/main/Main.java b/src/main/Main.java index 9221685..d1b2540 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,10 +1,8 @@ package main; import java.io.IOException; -import mining.Miner; +import java.util.Scanner; import mining.TwitterApi; -import org.json.JSONException; -import org.json.JSONObject; /** * @@ -12,16 +10,26 @@ import org.json.JSONObject; public class Main { public static void main(String[] args) throws IOException { - //Miner miner = new Miner(); + // create the api state to run request streams or REST. 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); + + // create the object that queues and executes the commands. + CommandQueue queue = new CommandQueue(); + + // create the object that parses the arguments. + CommandParser parser = new CommandParser(queue, api); + + // parse the input arguments and create the command. + parser.parse(args); + + Scanner input = new Scanner(System.in); + + // parse the receiving data through the input + while (true) { + queue.executeAll(); + + // parse new input. + parser.parse(input.nextLine()); } } } diff --git a/src/main/RequestCommand.java b/src/main/RequestCommand.java new file mode 100644 index 0000000..42ff41b --- /dev/null +++ b/src/main/RequestCommand.java @@ -0,0 +1,118 @@ +package main; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import mining.TwitterApi; +import mining.TwitterApi.Builder; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Basic contained class for a Pair, because Java doesn't provide this. + * + * @author Maurice Laveaux + */ +class Parameter { + + public Parameter(String variable, String value) { + this.variable = variable; + this.value = value; + } + + public String variable; + public String value; +} + +/** + * A single request/response command that executes once. + * + * @author Maurice Laveaux + */ +public class RequestCommand implements ICommand { + + /** + * The api to execute requests on + */ + private final TwitterApi m_api; + + private final String m_command; + + /** + * The list of parameters that are executed + */ + private final ArrayList<Parameter> m_parameters; + + /** + * Contruct a RequestCommand that executes the request and writes the + * results into a file. + * + * @param api the main twitter api. + * @param command the command of the twitter api f.e. users/show. + * @param parameters the parameters for the executed command. + */ + public RequestCommand(TwitterApi api, String command, String[] parameters) { + m_api = api; + m_command = command; + + m_parameters = new ArrayList(); + + if (parameters.length < 1) { + throw new IllegalArgumentException("Not enough parameters specified."); + } + + for (String param : parameters) { + // parse the parameter to a variable with a value. + m_parameters.add(parseParam(param)); + } + } + + @Override + public void execute() { + try { + // create the building object with the command. + Builder builder = m_api.build(m_command); + + // add every parameter to the builder. + for (Parameter param : m_parameters) { + builder = builder.param(param.variable, param.value); + } + + JSONObject result = builder.request(); + + // TODO: add the results to the main database. + // For now write to a file + File file = new File("database.txt"); + + FileWriter writer = new FileWriter(file, true); + + writer.write("\n\r" + result.toString()); + writer.flush(); + + System.out.println(result.toString(4)); + } catch (JSONException ex) { + /* cannot happen */ + throw new RuntimeException(ex); + } catch (IOException ex) { + // TODO: when ratelimit reached, add event back to queue. + throw new RuntimeException(ex); + } + } + + /** + * Parses the input string to a parameter. + * + * @param input A single line in the form variable=value. + * @return A pair of strings for variable and value. + */ + private Parameter parseParam(final String input) { + String[] seperated = input.split("="); + + if (seperated.length < 2) { + throw new IllegalArgumentException("parameter did not contain <variable>=<value>."); + } + + return new Parameter(seperated[0], seperated[1]); + } +} diff --git a/src/main/StreamCommand.java b/src/main/StreamCommand.java new file mode 100644 index 0000000..058cbd6 --- /dev/null +++ b/src/main/StreamCommand.java @@ -0,0 +1,10 @@ + +package main; + +/** + * + * @author maurice + */ +public class StreamCommand { + +} |