bigboard r7426 - trunk/bigboard/stocks/twitter



Author: marinaz
Date: Tue Aug 26 21:34:16 2008
New Revision: 7426
URL: http://svn.gnome.org/viewvc/bigboard?rev=7426&view=rev

Log:
Remove an extra step of verifying user's credentials, and request friends' updates right away.

Make the initial request for friends' updates in the main thread, since doing it inside a polling task in a separate thread was very slow.

Submit user's status update in the main thread as well and have a better visual indication that it was sent.  


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	Tue Aug 26 21:34:16 2008
@@ -8,7 +8,6 @@
 import bigboard.libbig.gutil as gutil
 from bigboard.libbig.logutil import log_except
 import bigboard.libbig as libbig
-from bigboard.libbig.http import AsyncHTTPFetcherWithAuth
 from bigboard.big_widgets import PrelightingCanvasBox, CanvasVBox, CanvasHBox, Header, CanvasURLImage
 
 _logger = logging.getLogger('bigboard.stocks.TwitterStock')
@@ -17,6 +16,7 @@
 FAILED_TO_LOGIN_STRING = "Failed to login."
 
 TYPE_TWITTER = "twitter"
+TWITTER_USER_AGENT = "gnomeonlinedesktop"
 
 TWITTER_STATUS_MAX_LENGTH = 140
 
@@ -96,8 +96,6 @@
         self.__twitter = twyt.Twitter()
         self.__twitter.set_user_agent("gnome")
 
-        self.__fetcher = AsyncHTTPFetcherWithAuth()
-
         self.__check_twitter_task = CheckTwitterTask(self)
 
         # even though the account system can theoretically return multiple Twitter accounts, this stock
@@ -176,12 +174,8 @@
             checking = hippo.CanvasText(text="Checking credentials for " + username + "...",
                                           size_mode=hippo.CANVAS_SIZE_WRAP_WORD, classes="info")
             self.__box.append(checking)
-            self.__fetcher.fetch("http://twitter.com/account/verify_credentials.json";, 
-                                 username, password,
-                                 lambda url, data: self.__on_twitter_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({}))
+            self.__get_friends_updates(username, password)
+            self.__check_twitter_task.start()
         else:
             self.__add_login_button()  
 
@@ -195,35 +189,32 @@
                 _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.set_auth(username, password)
+                self.__twitter.set_user_agent(TWITTER_USER_AGENT)  
+                try:
+                    self.__twitter.status_update(status)
+                    _logger.debug("Twitter status update went fine")
+                    gobject.timeout_add(1000 * 5, self.__get_friends_updates, username, password)
+                    self.__twitter_status_input.set_property("text", "")
+                    self.__twitter_status_counter_text.set_property("text", "sent...")
+                except twyt.TwitterException, e:
+                    _logger.debug("caught an exception %s" % e)
+                    # most likely this was an authentication failure
+                    self.__on_twitter_error(username, password)
 
-            self.__twitter_status_input.set_property("text", "")
-
-    def __on_twitter_response(self, url, data, username, password):
+    def __on_first_twitter_response(self):
         _logger.debug("Authentication must be good")
-        if self.__same_credentials(username, password):
-            self.__box.remove_all()
-            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)  
+        self.__box.remove_all()
+        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)  
        
-            # remove everything from the friends updates box in case there are friends updates from the 
-            # previous account that are stored there
-            self.__friends_updates_box.remove_all()
-            self.__box.append(self.__friends_updates_box)
-
-            loading = hippo.CanvasText(text="Loading friends' updates...",
-                                          size_mode=hippo.CANVAS_SIZE_WRAP_WORD, classes="info")
-            self.__friends_updates_box.append(loading)
-
-            self.__check_twitter_task.start()
+        # remove everything from the friends updates box in case there are friends updates from the 
+        # previous account that are stored there
+        self.__friends_updates_box.remove_all()
+        self.__box.append(self.__friends_updates_box)
 
     def do_periodic_updates(self):
         t = threading.Thread(target=self.__get_friends_updates, 
@@ -234,14 +225,25 @@
 
     def __get_friends_updates(self, username, password):
         self.__twitter.set_auth(username, password)
-        answer = self.__twitter.status_friends_timeline()
-        # _logger.debug("got answer from Twitter %s" % answer)
-        results = twyt_data.StatusList(answer)
-        # _logger.debug("number of results: %s" % str(len(results)))
-        gobject.idle_add(self.__process_friends_updates, results, username, password)
+        self.__twitter.set_user_agent(TWITTER_USER_AGENT)  
+        try:
+            # _logger.debug("will send request to twitter")
+            answer = self.__twitter.status_friends_timeline()
+            # _logger.debug("got answer from Twitter") # %s" % answer)
+            results = twyt_data.StatusList(answer)
+            # _logger.debug("number of results: %s" % str(len(results)))
+            gobject.idle_add(self.__process_friends_updates, results, username, password)
+        except twyt.TwitterException, e:
+            _logger.debug("caught an exception %s" % e)
+            # most likely this was an authentication failure
+            gobject.idle_add(self.__on_twitter_error, username, password)
 
     def __process_friends_updates(self, results, username, password):
+        # _logger.debug("processing friends' updates")
         if self.__same_credentials(username, password):
+            if len(self.__box.get_children()) == 1:
+                self.__on_first_twitter_response()
+            self.__on_status_edited(None, None)
             self.__friends_updates_box.remove_all()
             i = 0
             for result in results:
@@ -262,17 +264,8 @@
         else:
             _logger.debug("the credentials changed while we were getting friends updates from Twitter")
 
-    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"
-        _logger.debug("There was a Twitter error. Response: %s" % status)
-        if self.__same_credentials(username, password):
-            self.__add_login_button(True)
-
-    def __on_auth_failed(self, username, password):
-        _logger.debug("There was an authentication failure")
+    def __on_twitter_error(self, username, password):
+        _logger.debug("There was a Twitter error.")
         if self.__same_credentials(username, password):
             self.__add_login_button(True)
 



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