#!/bin/env groovy
def MAX_THREADS = 24
class MyThread extends Thread {
private i ;
public MyThread( int code ) {
this.i = code;
}
public void run() {
def rand = new java.util.Random()
sleep(1 + rand.nextInt(2000) );
println "HELLO " + i
}
}
println " Started "
def Thread[] results = new Thread[MAX_THREADS]
for ( int i = 0 ; i < MAX_THREADS ; i++ ) {
println "Started thread " + i
results[i] = new MyThread(i);
results[i].start()
}
for (int i = 0; i < results.length; i++) {
try {
results[i].join();
} catch (InterruptedException ignore) {}
}
println "Done ... "
Thursday, February 24, 2011
Running multithreaded example groovy
Thursday, December 31, 2009
New Android Site ...
I have a new site where I'll be posting further Android code . .. See: GeorgeKowalski.com
Tuesday, December 9, 2008
Geocoding with Groovy and Google API
Nice article on how to geocode data with Groovy and the Google API : Here..
Friday, December 5, 2008
Deploying Grails Application on Oracle Database
The following article presents a couple of lessons learned on Deploying a Grails app on Oracle DB Here.
Thursday, December 4, 2008
Sunday, November 16, 2008
Thursday, November 13, 2008
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.
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;
}
Subscribe to:
Posts (Atom)
