quick tutorial on installing grails application on glassfish prelude

See suns demo Here

Advertisements

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.



#!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;
}



groovy script to parse RSS with namespaces and download images



#!groovy
// program to parse RSS Feed and download images from digg
def rssurl = "http://feeds.digg.com/digg/news/popular.rss"
def slurp = new XmlSlurper()
def rssObj = slurp.parse(rssurl).declareNamespace(digg: "http://digg.com/docs/diggrss/", media: "http://search.yahoo.com/mrss/")
rssObj.channel.item.eachWithIndex { item , num ->
println "-------------------------------------------"
println item.title
println item."digg:category"
def url = item."media:thumbnail".@url.text()
if ( url ) {
println url
download(num, url)
}

}


def download(num , address)
{
def filename = address.tokenize("/")[-2]
def tmp = address.tokenize("/")[-1]
def ext = tmp.tokenize(".")[-1]
filename = filename << "." << ext
println "saving image file : " << filename
def file = new FileOutputStream(filename.toString())
def out = new BufferedOutputStream(file)
out << new URL(address).openStream()
out.close()
}

groovy screen scraping example


#!/usr/bin/env groovy
// Depends on tagsoup library:
// http://ccil.org/~cowan/XML/tagsoup/

def slurper = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())

def url = new URL("http://fcd.mcw.edu/?module=faculty&func=view&id=1674")

url.withReader { reader ->

html = slurper.parse(reader)



//we should now have a parsed file



def value = html.body.div.div.div[2].ul.li

value.list().each { f ->

println "\nPub : " << f.toString()[0..80] << "..."

}



}

[gkowalski]$ ./screenScrape.groovy
Pub : Role of cannabinoids and endocannabinoids in cerebral ischemia. (Hillard CJ) Cur…
Pub : Regional alterations in the endocannabinoid system in an animal model of depressi…
Pub : Mediation of Cannabidiol Anti-inflammation in the Retina by Equilibrative Nucleos…