Attachment 'TwitterStock_Version5.py'

Download

   1 ...
   2 from bigboard.libbig.http import AsyncHTTPFetcherWithAuth
   3 
   4 FAILED_TO_LOGIN_STRING = "Failed to login."
   5 
   6 class TwitterStock(Stock):
   7 
   8     def __init__(self, *args, **kwargs):
   9         ...
  10         self.__fetcher = AsyncHTTPFetcherWithAuth()
  11 
  12     ...
  13 
  14     def __on_twitter_account_changed(self):
  15         _logger.debug("will change stuff")
  16         username = self.__twitter_account.GetUsername()
  17         password = self.__twitter_account.GetPassword() 
  18         if password != "":
  19             self.__box.remove_all()
  20             checking = hippo.CanvasText(text="Checking credentials for " + username + "...",
  21                                           size_mode=hippo.CANVAS_SIZE_WRAP_WORD, classes="info")
  22             self.__box.append(checking)
  23             self.__fetcher.fetch("http://twitter.com/account/verify_credentials.json", 
  24                                  username, password,
  25                                  lambda url, data: self.__on_twitter_response(url, data, username, password), 
  26                                  lambda url, resp: self.__on_twitter_error(url, resp, username, password),
  27                                  lambda url: self.__on_auth_failed(username, password),
  28                                  data = urllib.urlencode({}))
  29         else:
  30             self.__add_login_button()  
  31 
  32     def __on_twitter_response(self, url, data, username, password):
  33         _logger.debug("Authentication must be good")
  34         if self.__same_credentials(username, password):
  35             self.__box.remove_all()
  36             hello_message = hippo.CanvasText(text="Hello Twitter User " + self.__twitter_account.GetUsername() ,
  37                                              size_mode=hippo.CANVAS_SIZE_WRAP_WORD)
  38             self.__box.append(hello_message)
  39 
  40     def __on_twitter_error(self, url, resp, username, password):
  41         status = resp and str(resp.status) or "No response"
  42         _logger.debug("There was a Twitter error. Response: %s" % status)
  43         if self.__same_credentials(username, password):
  44             self.__add_login_button(True)
  45 
  46     def __on_auth_failed(self, username, password):
  47         _logger.debug("There was an authentication failure")
  48         if self.__same_credentials(username, password):
  49             self.__add_login_button(True)
  50 
  51     def __same_credentials(self, username, password):
  52         return self.__twitter_account and self.__twitter_account.GetUsername() == username and self.__twitter_account.GetPassword() == password
  53 
  54     def __add_login_button(self, failed_to_login = False):
  55         self.__box.remove_all()
  56         if failed_to_login:
  57             error = hippo.CanvasText(text=FAILED_TO_LOGIN_STRING, size_mode=hippo.CANVAS_SIZE_WRAP_WORD, classes="error")
  58             self.__box.append(error) 
  59         hello_message = hippo.CanvasText(text="Hello Twitter User " + self.__twitter_account.GetUsername(),
  60                                          size_mode=hippo.CANVAS_SIZE_WRAP_WORD)
  61         self.__box.append(hello_message)                           
  62         self.__box.append(self.__login_button) 
  63 
  64 
  65 FROM stock.css:
  66 
  67 .info {
  68     color: #00AABB;
  69 }
  70 
  71 .error {
  72     color: #CC1122;
  73 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:46:11, 450.6 KB) [[attachment:GUADEC-Presentation.odp]]
  • [get | view] (2021-02-25 09:46:11, 0.6 KB) [[attachment:HippoCanvasExample.txt]]
  • [get | view] (2021-02-25 09:46:11, 0.5 KB) [[attachment:TwitterStock_Version1.py]]
  • [get | view] (2021-02-25 09:46:11, 3.9 KB) [[attachment:TwitterStock_Version2.py]]
  • [get | view] (2021-02-25 09:46:11, 0.4 KB) [[attachment:TwitterStock_Version3.py]]
  • [get | view] (2021-02-25 09:46:11, 0.9 KB) [[attachment:TwitterStock_Version4.py]]
  • [get | view] (2021-02-25 09:46:11, 3.1 KB) [[attachment:TwitterStock_Version5.py]]
  • [get | view] (2021-02-25 09:46:11, 2.4 KB) [[attachment:TwitterStock_Version6.py]]
  • [get | view] (2021-02-25 09:46:11, 14.1 KB) [[attachment:TwitterStock_Version7.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.