Twitter and GNOME integration

10 April 2008

This is part two of our look at using Python with the API of the Twitter social networking website. (Part one here)

I am assuming you are using a GNOME-based Unix/Linux system. If you use something else then sorry, you might want to read one of my other posts.

So in Twitter, you can have updates of what your friends are doing appear on the Twitter website (like this), however, it is more fun to make these updates come to you.

So I wrote a little program in Python that makes your Twitter updates pop-up on the desktop, like this:

http://commandline.org.uk/images/posts/gnome/twitter.png

This uses a feature of the Gnome desktop called 'notify', as explained by Andy W, and then followed up by me two posts ago.

Without further ado, lets get started.

1. Get python-notify

There are three very small but important dependencies. Firstly, you need the Python bindings for libnotify. These are available from your Linux distribution (you might even have them installed them already):

You will need setuptools as well. I am sure you already have that installed, but it cannot hurt to double-check.

The two computers I have here are Ubuntu and Gentoo:

On Ubuntu or Debian:

sudo apt-get install python-notify python-setuptools

On Gentoo:

sudo emerge notify-python setuptools

On your Linux/Unix, you can alter the commands slightly so it matches your package management system.

2. Get json and Twitter bindings

Now we need two more, simplejson and python-twitter

From any Linux Distribution, run:

sudo easy_install simplejson

sudo easy_install python-twitter

3. Twitter account

Next you will need to sign up to a Twitter account and add at least one friend (e.g. zeth0), in the spooky terminology of twitter, to add a friend you "follow them".

4. Get my script

Next you need to download a copy of my script and make the file extension .py. Get it from here, or type:

wget http://zeth.me.uk/python/twitnotify.txt -O twitnotify.py

chmod +x twitnotify.py

Now you need to edit the top of the script so that your username and password are correct. For historical reasons, I'm an Emacs man, but if you don't have a favourite editor, then gedit is pretty easy to use:

gedit twitnotify.py

5. Engage!

Now we are finally ready to run the script:

./twitnotify.py

If you have a lot of friends then there will be lots of updates the first time. The updates and pictures are all cached, so you don't have to download those again.

Six degrees of separation

After I made that script, I tried to be more adventurous and wrote a new script that found other people that shared your interests.

It would spider recursively through your friends, followers and their friends and followers, and their friends and followers, and their friends and followers (and so on), and returned a set of updates based on whatever keywords you specified.

It worked pretty well and the results were really fun. However, what I didn't know was that Twitter has quite sharp rate limits, so when this script stopped working I was combing through the code to find out what had gone wrong. It turned out that my code was fine but that my username had just been suspended from Twitter.

Twitter rate usage

Your username is allowed 70 requests per 60 sixty minute time period, starting from the first request.

Using my original twitnotify.py script posed no problem, the script usually uses three requests each time (one for normal updates, one for replies and one for personal "direct" messages).

So if you update every 10 minutes, then that will be about 18 requests an hour, easily under your 70 maximum.

Getting the photos of your friends is included in the above, but the first time you get a private message from a new person who is not a friend, it will take one request to download the photo.

My second script, the one to find new friends according to your interests, easily ate up 70 requests in one pass. So I had to give that up.

Fortunately, it seems that you are automatically un-suspended from Twitter after a couple of hours, and having your username temporally suspended from the API does not affect logging into the Twitter website.

Discuss this post - leave a comment

1 andylockran says...

Zeth - great post. Just one of a number of things programmers have to catch them out. I reckon that libnotifytwitter package could be packaged up for either ubuntu/gentoo - as gentoo's twitter support at the mo appeared to be nil.

Posted at 1:48 a.m. on April 11, 2008


2 rar says...

oh my. it's so easy.

just 1, 2, 3 4, and ur done.

Posted at 3:53 a.m. on April 11, 2008


3 Michael Klier says...

Great tool indeed!

However I'd like to point out that it also runs perfectly fine in a non-GNOME environment ;-) (I am using awesome WM here).

Posted at 6:08 p.m. on April 12, 2008


4 Neo says...

Great script but it didn't work here. I followed the 4 steps but it showed:

Traceback (most recent call last):
File "./twitnotify.py", line 198, in <module> main() File "./twitnotify.py", line 185, in main check_updates() File "./twitnotify.py", line 170, in check_updates twit.check_status(i) File "./twitnotify.py", line 130, in check_status send_note(username, text, imagepath ) File "./twitnotify.py", line 155, in send_note if not note.show():

System Message: WARNING/2 (<string>, line 14)

Definition list ends without a blank line; unexpected unindent.

gobject.GError: The name org.freedesktop.Notifications was not provided by any .service files

What should I do to get it run? Thanks!

Posted at 3:56 p.m. on May 4, 2008


5 Zeth says...

@Neo,

It looks like you have a dependency problem. Are you sure you have libnotify on your system?

Posted at 4:46 p.m. on May 4, 2008


6 andrew says...

Excellent work. Using python for app creation in gnome is awesome.

Do you have any interest of changing this into a gnome panel applet that would allow you to post tweets to your account as well as notify?

I could see this as having quite a bit of potential!

Posted at 2:44 p.m. on May 7, 2008


7 Grimmy says...

@Neo:

I just had the same problem on Kubuntu 8.04 (Hardy Heron). I tried various things, but eventually solved it by installing notification-daemon ( sudo apt-get install notification-daemon ).

Hope that helps.

Posted at 1:18 p.m. on May 11, 2008


8 yegle says...

@Zeth Hello Zeth,can you share your script you mentioned about the Six degrees of separation?I'm so intrested about it~

And, this is really an excellent work !Thank you for shareing !

Posted at 12:18 p.m. on June 12, 2008


9 marco says...

Hey,

thnx for this script, i love it. I made a little change wich allows to set the expire time of the notification. Created a patch for this, have a look at: http://pastebin.com/f14f1972e

So again thnx for your work

bye

marco

Posted at 5:40 p.m. on February 19, 2009


10 Christian says...

Hi,

great script! I had problems downloading it from your location, but I found it here too: http://media.commandline.org.uk//code/twitnotify.txt

bye,

Christian

Posted at 3:51 p.m. on March 27, 2009


11 Cale says...

Fun script. Thanks for posting. Like Christian mentioned, I had trouble with the download link as well. Working well on my Gentoo box, though.

Posted at 3:47 p.m. on April 11, 2009


12 suyash says...

nice script..good work dude

Posted at 8:05 p.m. on June 8, 2009


What do you have to say?

Show Editing Help

Europython

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

gutes Qualitätscasino

July 3, 2009
The paragraph is the most basic block in a reST document. Paragraphs are simply chunks of text separated by one or more blank lines. As in Python, indentation is significant ...
An Introduction to ReStructuredText

sreejith

July 3, 2009
I want to download a file from remote server in binary format. Can anyone let me know the command to do so? Thanks in advance
PuTTY Series: Using PSFTP

jythlkedl;rg

July 2, 2009
????? ??? ????????? ?????? ?? ???????? ? ????????? ???, ? ??????? ??? ??????? ??????? ? ??? ?? ???? ?? ?? ????? ???????????????? ??????????????????. ??????????????????? ????? ?? ?? ????, ?, ???, ...
Burning an iso to CD on Windows

gbi-service-ru

July 1, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

seo techniques

July 1, 2009
I would like to thank you for the inforamtion you have put on this article no matter.
Only the penitent man will pass - on captchas and cotton wool

Online Craps lernen

July 1, 2009
I would like to thank you for the making these clarifications in such a detailed manner to rebuilt the communication and enhancing the strategies of the organization which could be ...
Disclaimer: NO WARRANTY

ZK@Web Marketing Blog

July 1, 2009
Django is an amazing web framework; we built a lot of features in a very short period of time and Django [mostly] stayed out of our way. Last night as ...
Baby Steps with Django - Part 4 Django Applications and flow

overnight payday loans

July 1, 2009
I found commandline.org.uk very informative. The article is professionally written and I feel like the author knows the subject very well. commandline.org.uk keep it that way.
Only the penitent man will pass - on captchas and cotton wool

Drogo

June 30, 2009
Gotta agree with your sentiments about many modern games. The cost of a new game is prohibitive, especially for consoles (although I've noticed that PS2 games have crashed in price ...
Retro British Gaming - Part 3: Amstrad CPC Games

pppiohooddd

June 29, 2009
Free vadult video site! http://crech.us/ 1000 free video every day!
OpenSolaris, Gobuntu, and be careful who you kiss

Tesyimasystus

June 29, 2009
...Love this dude!!! http://www.esnips.com/doc/79c22395-7bd6-4299-92db-cf392e381698/kutiman---this-is-what-it-became Peace
5 Homebrew Python Games

Simon Tite

June 28, 2009
twitterfall is still there, I just tried it, and to me it beats Visible Tweets hands down. Problem with Visible Tweets: * Extremely **irritating** animations! (There are three available, but ...
Visualising your favourite keywords in Twitter

piffAltetle

June 28, 2009
??? ??? ???? ???????????? ??????,?????????? ???? ?????? ??????????? ???????,??????????? ????? mp3,??????? ??????????? ??????.
Encrypt your /home this Christmas: part three - moving your data to the encrypted partition

idhyougjdsyhfr

June 26, 2009
SMS Trap is something that never fails to help you get your partner off guard? Our software will make reading other people?s SMS as easy as ABC. Ready for some ...
Burning an iso to CD on Windows

Sozdanie-saitov-com

June 26, 2009
???? ???????? ? ?????????????????? ????????????? - ??? - ???? ?? ????? ?????. ???? ?? ??? ? ??????? ?????? ????????? ?????! ???, ?????23126 sozdanie-saitov.com@mail.ru
Burning an iso to CD on Windows

gameskillz

June 26, 2009
Killzone 2 - the best PS3 game yet?Still LittleBigPlanet for me, but Sony's new shooter is mightily impressive. What you think about my web? http://www.easyfaxlesspaydayloan.com/payday-loans-online.html
Email Syntax Check in Python

Anish

June 25, 2009
hey Moritz, Check this http://commandline.org.uk/python/my-merry-five-minutes-with-bazaar/
Setting up a bazaar server

gbi zavod 177

June 24, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

vettone

June 24, 2009
??? ????? ????? ????,??????? ?? ???,????? ???? ???.????? ?? ??????????? ????,?????????? ?????? ???????? ?? ????.???? ????????: http://euro-football.ucoz.com ????? ???? ??????????.
Burning an iso to CD on Windows

tuegjhg78kjfhuey

June 23, 2009
? ???????????????? ???? ??? ???, ?? ?????? ?? ?????????, ???, ???????????????? ??? ??????????, ???? ????? ??? ??? http://remont.ucoz.ua/
Burning an iso to CD on Windows