Twitter Stream Mysterious 401:Unauthorized Status with Oauth and Clock Issue

I’ve been facing the following problem with Twitter Stream while using Python: The REST API was working fine, but the Stream API was returning me 401:Unauthorized.

Both, REST and Stream were working fine when using curl with the command provided by the Oauth tool, but while I was trying to make it work with Python, I was not being successful at all, only receiving the 401-thing. At that point nothing seemed to be strange, the response header was:

Content-Type: text/html
WWW-Authenticate: Basic realm="Firehose"
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1505
Connection: close

After 2 months struggling with that problem, I discovered that Twitter API will return 401 if your clock is not synchronized. To solve this you need to execute ntpdate. On Ubuntu you can execute the following command:

sudo ntpdate

and I guess that you will be able to connect with the Twitter Stream again. On the end, I discovered that this happened because I’ve moved to a different country, and I changed only my clock, without changing my Zone Time. I discovered this because the hour on my clock changed to the one from my home country when I used ntpdate. If this is your case, and you are using Ubuntu, change your Zone Time with the following comand:

sudo dpkg-reconfigure tzdata

Well, I learned a bunch of things in a row 🙂

Lossless JPEG rotation

Since I’ve moved and now I’m living in place in which I take a lot of pictures, I’ve been using a lot the portrait type of photo. So, most of times I need to rotate the images 90 degrees. With Gimp you can rotate, but there is a quality loss in the image. Jhead was not an option also because my digital camera save the image with Orientation flag equals 1, no matter how I took the photo. So, I did a Python script to rotate the images using Jpegtran — it is part of libjpeg-progs package.

import os, glob
path = '.'for infile in glob.glob(os.path.join(path, '*.JPG')):
os.system("jpegtran -rotate 90 %s > temp.JPG; rm %s; mv temp.JPG %s" % (infile, infile, infile))
print "Rotating %s ..." % infile

Get The Primary Buffer Content Using GTK and Python

That is it, just the three following lines:

import gtk
clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
print clipboard.wait_for_text()

More information about what are the interned strings and objects from gtk.gdk.Atom, it is here. Another interesting script in Python that deals with primary buffer is this one made to monitor the primary buffer. Another one is made to copy from the clipboard itself, it is here. Another one is a complete example how to make a textbox with Copy, Cut, and Paste features using PyGTK.

Repeat One Song plugin for Rhythmbox

So that is it, since I left Songbird and almost began to use its Linux supported branch NightIngale, I ended up returning to Rhythmbox. But there was something missing in it, the “Repeat One Song” feature. Sometimes you just got some nice song and want to listen to it thousands of times but had to use some workarounds such as filter by song’s name resulting this one the only in the list, or creating a playlist with just one song, etc.

To solve this, I coded the Repeat One Song plugin for Rhythmbox. Yesterday (literally) I barely knew how to code in Python and how to code an Rhythmbox plugin, but today it is done.

Installation requires only to copy the “repeat-one-song” folder and paste it in ~/.gnome2/rhythmbox/plugins/ — If “plugins” directory does not exist, just create it.