TwitterGroovy

Last modified by Ludovic Dubost on 2010/02/11 15:14

import java.util.List; import twitter4j.Twitter; import twitter4j.Paging; import org.w3c.dom.Document; import java.util.Date; import java.text.*;

public class TwitterGroovy {     Twitter twitter;

 public Twitter getTwitter(twitterID, twitterPassword) {     if (twitter!=null) return twitter; twitter = new Twitter(twitterID,twitterPassword); return twitter; }

 public Paging getPaging() {    return new Paging(); }

 public List search(String text) {    return search(text, 0); }

 public List search(String text, int nb) {     String url = "http://search.twitter.com/search.atom?q=" + URLEncoder.encode(text); if (nb!=0) url += "&rpp=" + nb; def resp = twitter.get(url, true); def xml = resp.asString(); def list = new ArrayList(); def node = new XmlSlurper().parseText(xml) for (entry in node.entry) {       String authoruri = entry.author.uri.text(); String authorid = authoruri.substring(authoruri.lastIndexOf("/") + 1); Date publishDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(entry.published.text().replaceAll("Z","+0000")); def user = [ "id" : authorid, "name" : entry.author.name.text(), "screenName" : authorid, "location" : "", "description" : "", "profileImageUrl": entry.link1.href, "URL" : authoruri, "isProtected" : "", "followersCount" : -1]; def st = text(), "source" : "", "isTruncated" : false, "inReplyToStatusId": "", "inReplyToUserId": "", "user" : user; list.add(st); }     return list; } }