bigboard r7409 - trunk/bigboard/stocks/twitter



Author: marinaz
Date: Thu Jun 26 22:51:51 2008
New Revision: 7409
URL: http://svn.gnome.org/viewvc/bigboard?rev=7409&view=rev

Log:
Allow submitting a status update to Twitter.


Modified:
   trunk/bigboard/stocks/twitter/TwitterStock.py

Modified: trunk/bigboard/stocks/twitter/TwitterStock.py
==============================================================================
--- trunk/bigboard/stocks/twitter/TwitterStock.py	(original)
+++ trunk/bigboard/stocks/twitter/TwitterStock.py	Thu Jun 26 22:51:51 2008
@@ -15,6 +15,8 @@
 
 TWITTER_KIND = "twitter"
 
+TWITTER_STATUS_MAX_LENGTH = 140
+
 class TwitterStock(Stock):
 
     def __init__(self, *args, **kwargs):
@@ -25,6 +27,13 @@
         self.__login_button = hippo.CanvasButton(text=LOGIN_TO_TWITTER_STRING)
         self.__login_button.connect('activated', lambda button: self.__open_login_dialog())
         self.__box.append(self.__login_button)
+  
+        self.__twitter_status_counter_text = hippo.CanvasText(text=str(TWITTER_STATUS_MAX_LENGTH))
+   
+        self.__twitter_status_input = hippo.CanvasEntry()
+        self.__twitter_status_input.get_property("widget").set_max_length(TWITTER_STATUS_MAX_LENGTH)
+        self.__twitter_status_input.connect("notify::text", self.__on_status_edited)
+        self.__twitter_status_input.connect("key-press-event", self.__on_status_submitted)
 
         self._add_more_button(self.__on_more_button)
 
@@ -115,13 +124,37 @@
         else:
             self.__add_login_button()  
 
+    def __on_status_edited(self, input, param_spec):
+        self.__twitter_status_counter_text.set_property("text", str(TWITTER_STATUS_MAX_LENGTH - len(self.__twitter_status_input.get_property("text"))))
+
+    def __on_status_submitted(self, entry, event):
+        if event.key == hippo.KEY_RETURN:
+            status = self.__twitter_status_input.get_property("text").strip()
+            if status != "":
+                _logger.debug("will send status: %s" % status)
+                username = self.__twitter_account.GetUsername()
+                password =  self.__twitter_account.GetPassword()
+                self.__fetcher.fetch("http://twitter.com/statuses/update.json";, 
+                                 username, password,
+                                 lambda url, data: self.__on_twitter_status_response(url, data, username, password), 
+                                 lambda url, resp: self.__on_twitter_error(url, resp, username, password),
+                                 lambda url: self.__on_auth_failed(username, password),
+                                 data = urllib.urlencode({"status":status}))
+
+            self.__twitter_status_input.set_property("text", "")
+
     def __on_twitter_response(self, url, data, username, password):
         _logger.debug("Authentication must be good")
         if self.__same_credentials(username, password):
             self.__box.remove_all()
-            hello_message = hippo.CanvasText(text="Hello Twitter User " + self.__twitter_account.GetUsername() ,
+            hello_message = hippo.CanvasText(text="Update status for " + self.__twitter_account.GetUsername() + ":",
                                              size_mode=hippo.CANVAS_SIZE_WRAP_WORD)
             self.__box.append(hello_message)
+            self.__box.append(self.__twitter_status_counter_text)
+            self.__box.append(self.__twitter_status_input)  
+
+    def __on_twitter_status_response(self, url, data, username, password):
+        _logger.debug("Twitter status update went fine")
 
     def __on_twitter_error(self, url, resp, username, password):
         status = resp and str(resp.status) or "No response"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]