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.
#!groovy
// 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;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Create images dir if one does not exit
def inDir = new File("images").mkdir()
// program to parse RSS Feed
def rssurl = "http://twitter.com/statuses/public_timeline.xml"
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"
out.close()
// delete existing file that was started
new File( "images/" + filename.toString()).delete()
return
}
out.close()
def infile = new FileInputStream( "images/" + filename.toString())
def input = new BufferedInputStream(infile)
byte[] testBytes= new byte[1285]
input.read(testBytes)
input.close()
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;
}

0 comments:
Post a Comment