Download images of twitter users from the public timeline

I wanted to take the previous program and mod it to download twitter user pictures from the public timeline. I found that a number of them were just the default picture , so I through of generating a Digest ( checksum ) on the images downloaded. You may note that it takes only the first 1285 bytes of the user’s picture. That’s because that is the size of this default picture as provided by Twitter. “3yLyyOGkvQJNArEKPzvRzA==” is the digest for this image.

It might be cool to next mod this to stitch these pictures together into a wallpaper, especially if it was on ones friends instead of the random public.

// Program to download images of twitter users from the public timeline
// Does not download the default image if the user has not changed it.
// George Kowalski 2008

import sun.misc.BASE64Encoder;

// Create images dir if one does not exit
def inDir = new File("images").mkdir()

// program to parse RSS Feed
def rssurl = ""
def slurp = new XmlSlurper()
def rssObj = slurp.parse(rssurl)
rssObj.status.eachWithIndex { item , num ->
println "-------------------------------------------"
def name = item.user.screen_name.toString()
def url = item.user.profile_image_url.toString()
if ( url ) {
download(num, url, name)


// Download the picture to a file names after the user's screen name
def download(num , address, screenName)
def filename = address.tokenize("/")[-2]
def tmp = address.tokenize("/")[-1] // get the extenstion off the last part
def ext = tmp.tokenize(".")[-1]
filename = screenName << "." << ext
println "saving image file : " << filename
def outfile = new FileOutputStream( "images/" + filename.toString())
def out = new BufferedOutputStream(outfile)
try {
out << new URL(address).openStream()
} catch ( FileNotFoundException e ) {
println "Error : Could not download image"
// delete existing file that was started
new File( "images/" + filename.toString()).delete()
def infile = new FileInputStream( "images/" + filename.toString())
def input = new BufferedInputStream(infile)
byte[] testBytes= new byte[1285]
def digestResults = getMessageDigest(testBytes)
// println "getMessageDigest " << digestResults
// remove default twitter image
if ( digestResults == "3yLyyOGkvQJNArEKPzvRzA==" ) {
println "removing default pic for " << screenName
new File( "images/" + filename.toString()).delete()


// Calculate a digest ( checksum ) for the bytes passed in
static public String getMessageDigest(byte[] bytes) throws UnsupportedEncodingException {
String result = null
String algorithm = "MD5";

if (bytes != null) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {

md.update(bytes); //set digest source
byte[] raw = md.digest(); //transformed digest
result = (new BASE64Encoder()).encode(raw); //string representation of digest

return result;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s