[tuxdroid-svn] r5726 - software_suite_v3/software/plugin/plugin-twitter/branches/random_sentences/p
Status: Beta
Brought to you by:
ks156
|
From: ks156 <c2m...@c2...> - 2009-10-19 09:17:44
|
Author: ks156
Date: 2009-10-19 11:17:32 +0200 (Mon, 19 Oct 2009)
New Revision: 5726
Modified:
software_suite_v3/software/plugin/plugin-twitter/branches/random_sentences/plugin-twitter/src/TwitterPlugin.java
Log:
* Don't know what happens with this file, but it wasn't indented properly.
Modified: software_suite_v3/software/plugin/plugin-twitter/branches/random_sentences/plugin-twitter/src/TwitterPlugin.java
===================================================================
--- software_suite_v3/software/plugin/plugin-twitter/branches/random_sentences/plugin-twitter/src/TwitterPlugin.java 2009-10-19 09:16:09 UTC (rev 5725)
+++ software_suite_v3/software/plugin/plugin-twitter/branches/random_sentences/plugin-twitter/src/TwitterPlugin.java 2009-10-19 09:17:32 UTC (rev 5726)
@@ -34,506 +34,506 @@
public class TwitterPlugin extends SimplePlugin<Configuration>{
- private static TwitterDatasStruct savedStruct;
- private static byte REPLY = 0;
- private static byte MESSAGE = 1;
-
- private List<String> messagesList = new ArrayList<String>();
- private List<String> repliesList = new ArrayList<String>();
-
- private boolean throwed = false;
-
- /***
- This function set the new user status.
- Préconditions :
- - To be in run mode
- - Activate targeted option.
- ***/
- private boolean setUserStatus(Twitter twitter)
- {
- if(this.getCommand().equalsIgnoreCase("run"))
- {
- //Set user status
- if(this.configuration().getUpdateStatus())
- {
- try
- {
- twitter.updateStatus(this.configuration().getMyStatus());
-
- if ( twitter.getStatus().getText().equalsIgnoreCase(this.configuration().getMyStatus()) )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch(TwitterException notConnected)
- {
- if (!this.getCommand().equalsIgnoreCase("check"))
- {
- throwMessage("You do not seem to be connected. Please, verify you username and password, then try again.");
- }
- this.quitGadget();
- }
- }
- }
-
- return false;
- }
-
-
- /***
- * Get all new friend statuses.
- * Automatic option.
- ***/
- private int getNewFriendStatusesCount(Twitter twitter)
- {
- int statusesCount = 0;
- List<User> friend = twitter.getFriends();
-
- for( int i = 0 ; i < friend.size() ; i++ )
- {
- try
- {
- Status status = friend.get(i).getStatus();
-
- String statusText = status.getText();
- String username = status.getUser().getName();
-
-
- //if a new status was set for this user text.
- boolean said = savedStruct.isRegistered(TwitterDatasStruct.FRIENDS_STATUSES, username, statusText);
-
- if((statusText != "null") && !said)
- {
- String stat = statusText.replace("&", "");
- stat = this.urlFilter(stat);
-
- if (! isReplyToOtherUser(stat))
- {
- statusesCount += 1;
- //Saving last entry.
- savedStruct.addFriendStatus(username, statusText);
- }
- }
- }
- catch(NullPointerException statusNotExists)
- {
- //Simply pass in case of exception, friend status change will not be counted.
- ;
- }
-
- }
-
- return statusesCount;
- }
-
-
-
- /***
- * This function return how many messages user has into his private Twitter mailbox.
- * @return an integer that represent user privates messages count.
- ***/
- private int getPrivateMessagesCount(Twitter twitter)
- {
- List<Message> messages = twitter.getDirectMessages();
- if(messages.size() > 0)
- {
- try
- {
- //then give messages.
- for(Message message : messages)
- {
- String username = message.getSender().getName();
-
- boolean said = savedStruct.isRegistered(TwitterDatasStruct.MESSAGES, username, message.getText());
-
- if((message.getText() != "null") && !said)
- {
- messagesList.add(username);
- savedStruct.addMessage(username, message.getText());
- }
- }
- }
- catch(NullPointerException pointerError)
- {
- ;
- }
- }
-
- return messagesList.size();
- }
-
-
-
- /***
- * This function return how many messages user has into his public Twitter mailbox.
- * @return an integer that represent user public messages count.
- ***/
- private int getPublicMessagesCount(Twitter twitter)
- {
- List<Message> replies = twitter.getReplies();
+ private static TwitterDatasStruct savedStruct;
+ private static byte REPLY = 0;
+ private static byte MESSAGE = 1;
- if(replies.size() > 0)
- {
- try
- {
- for(Message reply : replies)
- {
- String username = reply.getSender().getName();
-
- boolean said = savedStruct.isRegistered(TwitterDatasStruct.REPLIES, username, reply.getText());
-
- if((reply.getText() != "null") && !said)
- {
- repliesList.add(username);
- savedStruct.addReply(username, reply.getText());
- }
-
- }
- }
- catch(NullPointerException pointerError)
- {
- ;
- }
- }
- return repliesList.size();
- }
-
-
- private void throwRes()
- {
- if ( ( !throwed ) && ( this.getCommand().equalsIgnoreCase("check") ))
- {
- this.throwed = true;
- this.throwResult(true);
- this.throwMessage("Twitter");
- }
- }
-
-
- @Override
- protected void start() throws Throwable
- {
-
- if(( this.configuration().getPassword() == null) || (this.configuration().getUsername() == null))
- {
- if( ! this.getCommand().equalsIgnoreCase("check"))
- {
- this.throwMessage("Sorry, there was an error when connecting to Twitter. Please check your Twitter configuration.");
- }
- this.quitGadget();
- }
-
- //Creating / getting sav context.
- savedStruct = new TwitterDatasStruct(this.configuration().getUsername());
-
- boolean secondCycle = ( this.getCommand().equalsIgnoreCase("check") && savedStruct.isFirstCycle() ) ? false : true;
-
- /***
- * Getting connected to twitter and setting update source ( here TuxDroid ).
- ***/
- Twitter twitter = null;
- twitter = new Twitter(this.configuration().getUsername(), this.configuration().getPassword());
- twitter.setSource("tuxdroid");
-
- //Getting Twitter informations.
- try
- {
- if (! this.configuration().getUpdateStatus())
- {
- /**
- * Getting new friend statuses count if option is enabled.
- */
- if(this.configuration().getCheckFriends())
- {
- int updateCount = getNewFriendStatusesCount(twitter);
-
- //First check insilent mode.
- if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
- {
-
- if ( updateCount == 1 )
- {
- throwRes();
- throwMessage("There is a new Tweet available.");
- }
- else if ( updateCount > 1 )
- {
- throwRes();
- throwMessage("You have {0} new Tweets.", String.valueOf(updateCount));
- }
- else
- {
- if ( ! this.getCommand().equalsIgnoreCase("check") )
- {
- throwMessage("You don't have any new Tweets.");
- }
- }
- }
- }
-
-
- /**
- * Getting, sorting new user private / public ( replies ) messages.
- * if option is enabled.
- */
- if (this.configuration().getCheckMessages())
- {
- if ( getPrivateMessagesCount(twitter) > 0 )
- {
+ private List<String> messagesList = new ArrayList<String>();
+ private List<String> repliesList = new ArrayList<String>();
+
+ private boolean throwed = false;
+
+ /***
+ This function set the new user status.
+ Préconditions :
+ - To be in run mode
+ - Activate targeted option.
+ ***/
+ private boolean setUserStatus(Twitter twitter)
+ {
+ if(this.getCommand().equalsIgnoreCase("run"))
+ {
+ //Set user status
+ if(this.configuration().getUpdateStatus())
+ {
+ try
+ {
+ twitter.updateStatus(this.configuration().getMyStatus());
+
+ if ( twitter.getStatus().getText().equalsIgnoreCase(this.configuration().getMyStatus()) )
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ catch(TwitterException notConnected)
+ {
+ if (!this.getCommand().equalsIgnoreCase("check"))
+ {
+ throwMessage("You do not seem to be connected. Please, verify you username and password, then try again.");
+ }
+ this.quitGadget();
+ }
+ }
+ }
+
+ return false;
+ }
+
+
+ /***
+ * Get all new friend statuses.
+ * Automatic option.
+ ***/
+ private int getNewFriendStatusesCount(Twitter twitter)
+ {
+ int statusesCount = 0;
+ List<User> friend = twitter.getFriends();
+
+ for( int i = 0 ; i < friend.size() ; i++ )
+ {
+ try
+ {
+ Status status = friend.get(i).getStatus();
+
+ String statusText = status.getText();
+ String username = status.getUser().getName();
+
+
+ //if a new status was set for this user text.
+ boolean said = savedStruct.isRegistered(TwitterDatasStruct.FRIENDS_STATUSES, username, statusText);
+
+ if((statusText != "null") && !said)
+ {
+ String stat = statusText.replace("&", "");
+ stat = this.urlFilter(stat);
+
+ if (! isReplyToOtherUser(stat))
+ {
+ statusesCount += 1;
+ //Saving last entry.
+ savedStruct.addFriendStatus(username, statusText);
+ }
+ }
+ }
+ catch(NullPointerException statusNotExists)
+ {
+ //Simply pass in case of exception, friend status change will not be counted.
+ ;
+ }
+
+ }
+
+ return statusesCount;
+ }
+
+
+
+ /***
+ * This function return how many messages user has into his private Twitter mailbox.
+ * @return an integer that represent user privates messages count.
+ ***/
+ private int getPrivateMessagesCount(Twitter twitter)
+ {
+ List<Message> messages = twitter.getDirectMessages();
+ if(messages.size() > 0)
+ {
+ try
+ {
+ //then give messages.
+ for(Message message : messages)
+ {
+ String username = message.getSender().getName();
+
+ boolean said = savedStruct.isRegistered(TwitterDatasStruct.MESSAGES, username, message.getText());
+
+ if((message.getText() != "null") && !said)
+ {
+ messagesList.add(username);
+ savedStruct.addMessage(username, message.getText());
+ }
+ }
+ }
+ catch(NullPointerException pointerError)
+ {
+ ;
+ }
+ }
+
+ return messagesList.size();
+ }
+
+
+
+ /***
+ * This function return how many messages user has into his public Twitter mailbox.
+ * @return an integer that represent user public messages count.
+ ***/
+ private int getPublicMessagesCount(Twitter twitter)
+ {
+ List<Message> replies = twitter.getReplies();
+
+ if(replies.size() > 0)
+ {
+ try
+ {
+ for(Message reply : replies)
+ {
+ String username = reply.getSender().getName();
+
+ boolean said = savedStruct.isRegistered(TwitterDatasStruct.REPLIES, username, reply.getText());
+
+ if((reply.getText() != "null") && !said)
+ {
+ repliesList.add(username);
+ savedStruct.addReply(username, reply.getText());
+ }
+
+ }
+ }
+ catch(NullPointerException pointerError)
+ {
+ ;
+ }
+ }
+ return repliesList.size();
+ }
+
+
+ private void throwRes()
+ {
+ if ( ( !throwed ) && ( this.getCommand().equalsIgnoreCase("check") ))
+ {
+ this.throwed = true;
+ this.throwResult(true);
+ this.throwMessage("Twitter");
+ }
+ }
+
+
+ @Override
+ protected void start() throws Throwable
+ {
+
+ if(( this.configuration().getPassword() == null) || (this.configuration().getUsername() == null))
+ {
+ if( ! this.getCommand().equalsIgnoreCase("check"))
+ {
+ this.throwMessage("Sorry, there was an error when connecting to Twitter. Please check your Twitter configuration.");
+ }
+ this.quitGadget();
+ }
+
+ //Creating / getting sav context.
+ savedStruct = new TwitterDatasStruct(this.configuration().getUsername());
+
+ boolean secondCycle = ( this.getCommand().equalsIgnoreCase("check") && savedStruct.isFirstCycle() ) ? false : true;
+
+ /***
+ * Getting connected to twitter and setting update source ( here TuxDroid ).
+ ***/
+ Twitter twitter = null;
+ twitter = new Twitter(this.configuration().getUsername(), this.configuration().getPassword());
+ twitter.setSource("tuxdroid");
+
+ //Getting Twitter informations.
+ try
+ {
+ if (! this.configuration().getUpdateStatus())
+ {
+ /**
+ * Getting new friend statuses count if option is enabled.
+ */
+ if(this.configuration().getCheckFriends())
+ {
+ int updateCount = getNewFriendStatusesCount(twitter);
+
//First check insilent mode.
- if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
- {
- this.sort(this.messagesList);
- //Counting by name and throwing messages.
- this.getByNames(this.messagesList, MESSAGE);
- }
- }
- else
- {
- if ( ! this.getCommand().equalsIgnoreCase("check") )
- {
- throwMessage("You don't have any messages");
- }
- }
-
- //Getting and sorting new user replies.
- if ( getPublicMessagesCount(twitter) > 0 )
- {
- //First check insilent mode.
- if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
- {
- this.sort(this.repliesList);
- //Counting by name and throwing messages.
- this.getByNames(this.repliesList, REPLY);
- }
- }
- else
- {
- if ( ! this.getCommand().equalsIgnoreCase("check") )
- {
- throwMessage("You don't have any replies");
- }
- }
-
- //Registering couted messages.
- savedStruct.registerStruct();
-
- }
- }
- else
- {
- //Updating user status.
- if( ! this.setUserStatus(twitter) )
- {
- this.throwMessage("Your new Tweet was not set, please, try again later");
- }
- else
- {
- this.throwMessage("Your Tweet was added");
- }
- }
- }
- catch(TwitterException badRequest)
- {
- if ( ! this.getCommand().equalsIgnoreCase("check") )
- {
- throwMessage("Twitter is busy for now, please, try again later.");
- }
-
- this.quitGadget();
- }
-
- }
-
-
- /***
- * Sort a typ string list.
- * @param list
- */
- private void sort(List<String> list)
- {
- Collections.sort(list);
- }
-
-
- /***
- * Throw messages by user names.
- * @param list
- */
- private void getByNames(List<String> list, byte type)
- {
- Vector<String> dtUsers = new Vector<String>();
- Vector<Integer> dtCount = new Vector<Integer>();
- int count = 1;
-
- //Handling no replies / no messages.
- if (list.size() == 0)
- {
- return;
- }
-
- //Counting by names.
- for( int i = 0 ; i != list.size() ; i ++)
- {
-
- if (dtUsers.contains(list.get(i)))
- {
- dtCount.set(dtUsers.indexOf(list.get(i)), dtCount.get(dtUsers.indexOf(list.get(i))) + 1);
- }
- else
- {
- dtUsers.add(list.get(i));
- dtCount.add(1);
- }
-
- }
-
- if (dtUsers.size() > 0)
- {
- throwRes();
- }
-
- for(String it : dtUsers)
- {
- count = dtCount.get(dtUsers.indexOf(it));
-
- if ( type == REPLY )
- {
- if ( count == 1 )
- {
- throwMessage("One reply from {0}", it);
- }
- else
- {
- throwMessage ("{0} replies from {1}", count, it);
- }
- }
- else
- {
- if ( count == 1 )
- {
- throwMessage("One message from {0}", it);
- }
- else
- {
- throwMessage("{0} messages from {1}", count, it);
- }
- }
- }
- }
+ if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
+ {
+ if ( updateCount == 1 )
+ {
+ throwRes();
+ throwMessage("There is a new Tweet available.");
+ }
+ else if ( updateCount > 1 )
+ {
+ throwRes();
+ throwMessage("You have {0} new Tweets.", String.valueOf(updateCount));
+ }
+ else
+ {
+ if ( ! this.getCommand().equalsIgnoreCase("check") )
+ {
+ throwMessage("You don't have any new Tweets.");
+ }
+ }
+ }
+ }
- /**
- * Return true if the text value indicates that the message was sent from a user
- * to an other user , but not the TuxDroid owner.
- * @param value
- * @return
- */
- private boolean isReplyToOtherUser(String value)
- {
- if(value.charAt(0) == '@')
- {
- return true;
- }
- return false;
- }
+ /**
+ * Getting, sorting new user private / public ( replies ) messages.
+ * if option is enabled.
+ */
+ if (this.configuration().getCheckMessages())
+ {
+ if ( getPrivateMessagesCount(twitter) > 0 )
+ {
+ //First check insilent mode.
+ if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
+ {
+ this.sort(this.messagesList);
+ //Counting by name and throwing messages.
+ this.getByNames(this.messagesList, MESSAGE);
+ }
+ }
+ else
+ {
+ if ( ! this.getCommand().equalsIgnoreCase("check") )
+ {
+ throwMessage("You don't have any messages");
+ }
+ }
- /***
- * Remove Url's from text.
- * @param string
- * @return
- ***/
- public String urlFilter(String string)
- {
- while(string.contains("http://"))
- {
- try
- {
- int start = string.indexOf("http://");
- int stop = string.indexOf(" ", start);
-
- if( stop == -1 )
- {
- stop = string.length() ;
- }
-
- String url = string.substring(start, stop);
- string = string.replace(url, ".");
- }
- catch(StringIndexOutOfBoundsException e)
- {
- return string;
- }
- catch(NullPointerException no)
- {
- return string;
- }
- }
-
- while(string.contains("ftp://"))
- {
- try
- {
- int start = string.indexOf("http://");
- int stop = string.indexOf(" ", start);
-
- if( stop == -1 )
- {
- stop = string.length() ;
- }
-
- String url = string.substring(start, stop);
- string = string.replace(url, ".");
- }
- catch(StringIndexOutOfBoundsException e)
- {
- return string;
- }
- catch(NullPointerException no)
- {
- return string;
- }
- }
-
- return string;
- }
-
-
- /**
- * Quit the gadget.
- */
- public void quitGadget()
- {
- if(this.getCommand().equalsIgnoreCase("check") && this.throwed)
- {
- this.throwResult(false);
- }
- System.exit(0);
- }
-
-
- public static void main(String[] args)
- {
- new TwitterPlugin().boot(args, new Configuration());
- }
+ //Getting and sorting new user replies.
+ if ( getPublicMessagesCount(twitter) > 0 )
+ {
+ //First check insilent mode.
+ if(this.getCommand().equalsIgnoreCase("run") || secondCycle)
+ {
+ this.sort(this.repliesList);
+ //Counting by name and throwing messages.
+ this.getByNames(this.repliesList, REPLY);
+ }
+ }
+ else
+ {
+ if ( ! this.getCommand().equalsIgnoreCase("check") )
+ {
+ throwMessage("You don't have any replies");
+ }
+ }
+ //Registering couted messages.
+ savedStruct.registerStruct();
- @Override
- protected void onPluginEvent(String arg0, String[] arg1) throws Throwable
- {
- ;
- }
+ }
+ }
+ else
+ {
+ //Updating user status.
+ if( ! this.setUserStatus(twitter) )
+ {
+ this.throwMessage("Your new Tweet was not set, please, try again later");
+ }
+ else
+ {
+ this.throwMessage("Your Tweet was added");
+ }
+ }
+ }
+ catch(TwitterException badRequest)
+ {
+ if ( ! this.getCommand().equalsIgnoreCase("check") )
+ {
+ throwMessage("Twitter is busy for now, please, try again later.");
+ }
+ this.quitGadget();
+ }
- @Override
- protected void onPluginStop() throws Throwable
- {
- ;
- }
+ }
+
+ /***
+ * Sort a typ string list.
+ * @param list
+ */
+ private void sort(List<String> list)
+ {
+ Collections.sort(list);
+ }
+
+
+ /***
+ * Throw messages by user names.
+ * @param list
+ */
+ private void getByNames(List<String> list, byte type)
+ {
+ Vector<String> dtUsers = new Vector<String>();
+ Vector<Integer> dtCount = new Vector<Integer>();
+ int count = 1;
+
+ //Handling no replies / no messages.
+ if (list.size() == 0)
+ {
+ return;
+ }
+
+ //Counting by names.
+ for( int i = 0 ; i != list.size() ; i ++)
+ {
+
+ if (dtUsers.contains(list.get(i)))
+ {
+ dtCount.set(dtUsers.indexOf(list.get(i)), dtCount.get(dtUsers.indexOf(list.get(i))) + 1);
+ }
+ else
+ {
+ dtUsers.add(list.get(i));
+ dtCount.add(1);
+ }
+
+ }
+
+ if (dtUsers.size() > 0)
+ {
+ throwRes();
+ }
+
+ for(String it : dtUsers)
+ {
+ count = dtCount.get(dtUsers.indexOf(it));
+
+ if ( type == REPLY )
+ {
+ if ( count == 1 )
+ {
+ throwMessage("One reply from {0}", it);
+ }
+ else
+ {
+ throwMessage ("{0} replies from {1}", count, it);
+ }
+ }
+ else
+ {
+ if ( count == 1 )
+ {
+ throwMessage("One message from {0}", it);
+ }
+ else
+ {
+ throwMessage("{0} messages from {1}", count, it);
+ }
+ }
+ }
+ }
+
+
+ /**
+ * Return true if the text value indicates that the message was sent from a user
+ * to an other user , but not the TuxDroid owner.
+ * @param value
+ * @return
+ */
+ private boolean isReplyToOtherUser(String value)
+ {
+ if(value.charAt(0) == '@')
+ {
+ return true;
+ }
+ return false;
+ }
+
+
+ /***
+ * Remove Url's from text.
+ * @param string
+ * @return
+ ***/
+ public String urlFilter(String string)
+ {
+ while(string.contains("http://"))
+ {
+ try
+ {
+ int start = string.indexOf("http://");
+ int stop = string.indexOf(" ", start);
+
+ if( stop == -1 )
+ {
+ stop = string.length() ;
+ }
+
+ String url = string.substring(start, stop);
+ string = string.replace(url, ".");
+ }
+ catch(StringIndexOutOfBoundsException e)
+ {
+ return string;
+ }
+ catch(NullPointerException no)
+ {
+ return string;
+ }
+ }
+
+ while(string.contains("ftp://"))
+ {
+ try
+ {
+ int start = string.indexOf("http://");
+ int stop = string.indexOf(" ", start);
+
+ if( stop == -1 )
+ {
+ stop = string.length() ;
+ }
+
+ String url = string.substring(start, stop);
+ string = string.replace(url, ".");
+ }
+ catch(StringIndexOutOfBoundsException e)
+ {
+ return string;
+ }
+ catch(NullPointerException no)
+ {
+ return string;
+ }
+ }
+
+ return string;
+ }
+
+
+ /**
+ * Quit the gadget.
+ */
+ public void quitGadget()
+ {
+ if(this.getCommand().equalsIgnoreCase("check") && this.throwed)
+ {
+ this.throwResult(false);
+ }
+ System.exit(0);
+ }
+
+
+ public static void main(String[] args)
+ {
+ new TwitterPlugin().boot(args, new Configuration());
+ }
+
+
+ @Override
+ protected void onPluginEvent(String arg0, String[] arg1) throws Throwable
+ {
+ ;
+ }
+
+
+ @Override
+ protected void onPluginStop() throws Throwable
+ {
+ ;
+ }
+
}
|