Skip to content


A biophysicist teaches himself how to code

No posts for a while because I haven’t actually been writing anything new. Biopython has solved many of my day-to-day problems, and I’m in love with SeqIO.

Today is Canada Day and it’s pretty quiet around the lab, so I thought I’d try to write something that would let me do two things:
First, I want to be able to view my twitter feed using conky, and secondly I’d like to be able to send updates from the console.

This also gives me the chance to work on some fundamentals like interfacing with APIs and passing options to scripts from the command line. There, I totally justified it!

To start off, I installed python-setuptools (from the Ubuntu repo), simplejson and the python-twitter API interface. To install the last two you just download the archives, extract them, and then run the following two commands from within their folders:

python build
sudo python install

Let’s start off with a pretty basic framework. This script should print out your latest five friends’ updates to the terminal, and the struts are in place to eventually add post capability:

#! /usr/bin/env python
# Simple twitter interface

# Change the following two lines with your credentials
user = ‘username’
pw = ‘password’

num_statuses = 5 # Changes number of statuses to show

import sys, twitter
api = twitter.Api(username=user, password=pw)

if sys.argv[1] == ‘-l’:
timeline = api.GetFriendsTimeline(user)
while i < num_statuses: print timeline[i] print timeline[i].text print '\n' i+=1 elif sys.argv[1] == '-p': pass else: print 'Invalid input' print 'Allowed options are:' print '-p (to post an update)' print '-l (to list friend statuses)' sys.exit(2) [/sourcecode] Adding the update functionality is facile. Just change the code as follows: [sourcecode language='python'] elif sys.argv[1] == '-p': status = api.PostUpdate(sys.argv[2]) print 'Twitter status updated' [/sourcecode] The only caveat in doing it this way is that the status update entered at the command line must be passed as a string, with quotation marks around it. Otherwise this will post a one word update, which is terse even by twitter standards. This is already fully functional in the terminal, so the last step is dumping out the statuses to a file which conky can read. Here's the code I used to make the text file: [sourcecode language='python'] if sys.argv[1] == '-l': timeline = api.GetFriendsTimeline(user) i=0 output = open(os.environ['HOME']+'/tweets.txt', 'w') while i < num_statuses: output.write(timeline[i]'\n') output.write(timeline[i].text+'\n') output.write('\n') i+=1 output.close() [/sourcecode] We then have conky read it using a file like this (I named in .conkytweets and placed it in my home directory, make sure to change your home directory below):

use_xft yes
xftfont MyriadPro-Regular:size=8
alignment top_left
xftalpha 0.8
own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes
draw_shades no
draw_outline no
draw_borders no
stippled_borders 10
border_margin 4
border_width 0
default_shade_color black
default_outline_color black
use_spacer right
no_buffers no
uppercase no
default_color 222222
maximum_width 200
minimum_size 200 5
gap_y 400
gap_x 10
text_buffer_size 1024

${font size=9}Latest Tweets:
${color}${font}${execi 600 cat /home/jason/tweets.txt | fold -w 35}

Here is a screenshot of the output on my monitor (sorry for the blur over the tasks, there are some research details in there I just didn’t feel like posting for the whole world atm)

demo of conkytweets script

demo of conkytweets script

There are a few things that don’t work very well. To my knowledge, you can’t include clickable links in conky, so URLs in tweets don’t do anything. The textwrap in conky is also a bit wonky, but I don’t know that there is a nice fix for that. I suppose one option would be to modify the text file that the twitter script generates, but I’ll leave that as an exercise to the reader.

The simplest way to use this is to add a link to your path. For me it was:

cd /usr/bin/
sudo ln -s ~/scripts/ pytwit

Then you can use it from anywhere with either:

pytwit -p 'My awesome twitter post'


pytwit -l

For extra points, you can add the listing to your crontab as follows:

sudo gedit /etc/crontab
*/15 *	* * *	jason pytwit -l

This will update the statuses every 15 minutes.

The full script is available on github, and I welcome any additions/modifications/improvements as always.


%d bloggers like this: