class Twitter(QtGui.QMainWindow): def __init__(self, parent=None): # Initialise GUI QtGui.QWidget.__init__(self, parent) self.ui = Ui_twitter_client() self.ui.setupUi(self) # set character limit for text box; set status and login info self.max = 140 self.uid = '' # add username here self.pwd = '' # add password here self.updateLabel() # set up signals and slots QtCore.QObject.connect(self.ui.button_post, QtCore.SIGNAL("clicked()"), self.postTweet) QtCore.QObject.connect(self.ui.textEdit, QtCore.SIGNAL("textChanged()"), self.updateLabel) QtCore.QObject.connect(self.ui.users_rec, QtCore.SIGNAL("selectionChanged()"), self.rec_user_list) # define posting URLs self.twitUpdate = 'http://twitter.com/statuses/update.xml' self.twitCloser = 'http://twitter.com/account/end_session' # instantiate a class with methods for getting recommended users self.rec = RecommendedUsers(self.uid, self.pwd) def postTweet(self): # convert text in GUI text box to plain text mesg = self.ui.textEdit.toPlainText() # convert text into a url-encoded string so it can be used as a POST request post = urllib.urlencode({ 'status' : mesg }) try: http = httplib2.Http() # override the default (no exceptions, status in response) http.force_exception_to_status_code = False # provide a name and password for authentication http.add_credentials(self.uid, self.pwd) # post status update to Twitter page (resp, content) = http.request(self.twitUpdate, 'POST', post) # if successful, say so if resp and resp.status == 200: self.ui.status.setText('Twitter updated!') # if not, say so else: raise httplib2.HttpLib2Error, 'No response' except httplib2.HttpLib2Error, ex: print ex self.ui.status.setText(ex.__str__()) # end Twitter session http.request(self.twitCloser)
def __init__(self, parent=None): # Initialise GUI QtGui.QWidget.__init__(self, parent) self.ui = Ui_twitter_client() self.ui.setupUi(self) # set character limit for text box; set status and login info self.max = 140 self.uid = '' # add username here self.pwd = '' # add password here self.updateLabel() # set up signals and slots QtCore.QObject.connect(self.ui.button_post, QtCore.SIGNAL("clicked()"), self.postTweet) QtCore.QObject.connect(self.ui.textEdit, QtCore.SIGNAL("textChanged()"), self.updateLabel) QtCore.QObject.connect(self.ui.users_rec, QtCore.SIGNAL("selectionChanged()"), self.rec_user_list) # define posting URLs self.twitUpdate = 'http://twitter.com/statuses/update.xml' self.twitCloser = 'http://twitter.com/account/end_session' # instantiate a class with methods for getting recommended users self.rec = RecommendedUsers(self.uid, self.pwd)