diff options
-rw-r--r-- | database.txt | 2 | ||||
-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 |
7 files changed, 316 insertions, 12 deletions
diff --git a/database.txt b/database.txt new file mode 100644 index 0000000..586afa9 --- /dev/null +++ b/database.txt @@ -0,0 +1,2 @@ + +
{"location":"","default_profile":true,"profile_background_tile":false,"statuses_count":4,"lang":"nl","profile_link_color":"0084B4","id":745405160,"following":null,"protected":false,"favourites_count":1,"profile_text_color":"333333","description":"Stuff","verified":false,"contributors_enabled":false,"profile_sidebar_border_color":"C0DEED","name":"MazK","profile_background_color":"C0DEED","created_at":"Wed Aug 08 15:27:55 +0000 2012","is_translation_enabled":false,"default_profile_image":false,"followers_count":1,"profile_image_url_https":"https://pbs.twimg.com/profile_images/2482341510/o83ce5xo4s54ugq7xbl9_normal.gif","geo_enabled":false,"status":{"contributors":null,"text":"@planetside2 I like the \"Time Lapse Weapon Design\" the most, because it's nice to get an impression of how this game was designed.","geo":null,"retweeted":false,"in_reply_to_screen_name":"planetside2","truncated":false,"lang":"en","entities":{"symbols":[],"urls":[],"hashtags":[],"user_mentions":[{"id":247430686,"name":"PlanetSide 2","indices":[0,12],"screen_name":"planetside2","id_str":"247430686"}]},"in_reply_to_status_id_str":"233656189089640448","id":233659630595764224,"source":"web","in_reply_to_user_id_str":"247430686","favorited":false,"in_reply_to_status_id":233656189089640448,"retweet_count":0,"created_at":"Thu Aug 09 20:22:51 +0000 2012","in_reply_to_user_id":247430686,"favorite_count":0,"id_str":"233659630595764224","place":null,"coordinates":null},"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","follow_request_sent":null,"entities":{"description":{"urls":[]}},"url":null,"utc_offset":7200,"time_zone":"Amsterdam","notifications":null,"profile_use_background_image":true,"friends_count":4,"profile_sidebar_fill_color":"DDEEF6","screen_name":"maskman113","id_str":"745405160","profile_image_url":"http://pbs.twimg.com/profile_images/2482341510/o83ce5xo4s54ugq7xbl9_normal.gif","listed_count":0,"is_translator":false}
\ No newline at end of file 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 { + +} |