h-twt
Platform: Python
Published Jun 01, 2009
Updated Aug 06, 2009
a small gui in python and pyqt4 to update your status to twitter.
to run u need:
1. Python -
http://www.python.org/download
2. PyQt4 -
http://www.riverbankcomputing.co.uk/software/pyqt/download - sudo apt-get install python-qt4
3. HttpLib2 -
http://code.google.com/p/httplib2/downloads/list #!/usr/bin/python
"""
Copyright (c) 2009 Harrison Erd
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
"""
import sys, urllib, httplib2
from PyQt4 import QtCore, QtGui
import webbrowser
class PTwitter(QtGui.QWidget):
max = 140 # keep this field!
username = 'username_here' # change 'username_here' to your twitter username.
password = 'password_here' # change 'password_here' to your twitter password.
twitUpdate = 'http://twitter.com/statuses/update.xml'
twitCloser = 'http://twitter.com/account/end_session'
def Timeline(self):
print('Opening Twitter.com')
webbrowser.open_new_tab('http://twitter.com/')
def postTweet(self):
mesg = self.txt.toPlainText()
post = urllib.urlencode({ 'status' : mesg })
try:
http = httplib2.Http()
http.force_exception_to_status_code = False
http.add_credentials(self.username, self.password)
resp, content = http.request(self.twitUpdate, 'POST', post)
if resp and resp.status == 200:
self.lbl.setText('Twitter updated!')
else:
raise httplib2.HttpLib2Error, 'We could not update Twitter.'
except httplib2.HttpLib2Error, ex:
print ex
self.lbl.setText(ex.__str__())
http.request(self.twitCloser)
def updateLabel(self):
len = self.txt.toPlainText().size()
self.lbl.setText('%d' % (self.max-len))
self.pb1.setEnabled(len <= max)
def __init__(self):
QtGui.QWidget.__init__(self)
self.pb1 = QtGui.QPushButton('Update Twitter')
self.pb2 = QtGui.QPushButton('View Timeline')
self.txt = QtGui.QTextEdit()
self.lbl = QtGui.QLabel()
tab = QtGui.QGridLayout()
tab.setSpacing(5)
tab.addWidget(self.txt, 0, 0, 1, 2)
tab.addWidget(self.lbl, 1, 0, 1, 2)
tab.addWidget(self.pb1, 2, 0)
tab.addWidget(self.pb2, 2, 1)
self.setLayout(tab)
self.setWindowTitle('h-twt')
self.updateLabel()
self.connect(self.txt, QtCore.SIGNAL('textChanged()'),
self.updateLabel)
self.connect(self.pb1, QtCore.SIGNAL('clicked()'),
self.postTweet)
self.connect(self.pb2, QtCore.SIGNAL('clicked()'),
self.Timeline)
app = QtGui.QApplication(sys.argv)
win = PTwitter()
win.show()
sys.exit(app.exec_())