h-twt

By PATX on Jun 01, 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_())

Comments

Sign in to comment.
Hawkee   -  Jun 20, 2009

Yes, py2exe is the Windows equivalent of py2app.

 Respond  
PATX   -  Jun 20, 2009

py2app uh? sounds cool. all i have ever played around with is py2exe anyway i will take a look at Expat. thanks for the feedback.

 Respond  
Hawkee   -  Jun 20, 2009

PyQt is more of a GUI library, so you'll need to use something else for XML. It seems like Expat would be a good choice. Have a look at the documentation here:

http://docs.python.org/library/pyexpat.html

If I want to see @mentions in my web browser I'll just open up a tab as I normally do. I don't need this to open it. The nice thing about building a standalone Twitter app is you can create your own unique user experience.

Anyway, I was playing around with this a bit more and was able to turn this into standalone Mac OS X app with py2app. It worked pretty well.

 Respond  
PATX   -  Jun 20, 2009

@Hawkee I does not lock up for me.... Sorry about that binary. I just saw the title and was like this may work... Sorry. I really wanted to use the built in time line in this script. But can not find out how to do it easily.... Because the API returns XML not HTML and I am not sure if PyQt4 has a XML parasing option. Tho I will work on it. Glad you like it. For now (I heard you only like to see @s) you can change the

webbrowser.open_new_tab('http://twitter.com/') 

to

webbrowser.open_new_tab('http://twitter.com/#replies')

Thanks for going through the trouble to try it out.

 Respond  
Hawkee   -  Jun 20, 2009

I got it! It turns out PyQt 4.5 was buggy, thus the error. I compiled the recently released 4.5.1 and it worked like a charm.

So about the script. It locked up when I posted a Tweet, but it did post to Twitter. I had to reopen the script to stop the hourglass from spinning. The thing it really lacks is a built-in timeline and functionality like @mention alerts and PM support. It's a nice start and I'd love to see it grow.

 Respond  
Hawkee   -  Jun 20, 2009

The error I'm getting is this:

ImportError: cannot import name QtCore

What I don't get is why it can't use the QtCore.so file that's in the sys.path. It seems there is some sort of dependency problem or the PyQt library was compiled incorrectly.

I tried the old binary you sent me, but that's for Python 2.3 and is quite old. It didn't work at all.

 Respond  
PATX   -  Jun 20, 2009

@Hawkee Well... I am bummed. If you were running windows i could have sent you py2exe .exe version of this. but i have no clue what mac executable files are.... also when you try running the script (in your terminal) what error comes up? if it an import error then u did not build pyqt4 properly... however if it is something else then its myself and pls lemme know about it.... tho u probably no that :/

 Respond  
PATX   -  Jun 20, 2009

Hawkee. I will take a look. And no I am sorry but I am running Ubuntu Linux. Tho I have tested this on Windows XP....

 Respond  
Hawkee   -  Jun 19, 2009

PATX, unfortunately I couldn't get everything to compile properly to test this. I spent a great deal of time and really don't know what went wrong. I know you probably aren't running this on OS X, but here is my plight in the comments here: http://www.oak-tree.us/blog/index.php/2009/05/12/pyqt-mac

 Respond  
PATX   -  Jun 15, 2009

OK updated! also @Hawkee if you need more help or want a screen shot of it you can always go to http://patx44.appspot.com/h-twt << some very help links there along with a readme.

Alllsoooo.... u need to change

    username = 'username_here'  # change 'username_here' to your twitter username.
    password = 'password_here'  # change 'password_here' to your twitter password.

to what is says to do! or it will not work! also to make the timeline button work on the gui make sure your are logged into twitter with "Remember Me" turned on. that button is simply a link to your browser cos lets face it this a SIMPLE client.

 Respond  
PATX   -  Jun 15, 2009

Yeah sure lemme update that...

 Respond  
Hawkee   -  Jun 15, 2009

I wanted to give this a test run but I just couldn't quite get Python set up correctly for it. Maybe a list of links to each dependency would help.

 Respond  
PATX   -  Jun 13, 2009

:( no comments :(

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.