Wiki source code of TwitterGroovy
Last modified by Ludovic Dubost on 2010/02/11 15:14
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | import java.util.List; | ||
| 4 | import twitter4j.Twitter; | ||
| 5 | import twitter4j.Paging; | ||
| 6 | import org.w3c.dom.Document; | ||
| 7 | import java.util.Date; | ||
| 8 | import java.text.*; | ||
| 9 | |||
| 10 | public class TwitterGroovy { | ||
| 11 | Twitter twitter; | ||
| 12 | |||
| 13 | public Twitter getTwitter(twitterID, twitterPassword) { | ||
| 14 | if (twitter!=null) | ||
| 15 | return twitter; | ||
| 16 | |||
| 17 | twitter = new Twitter(twitterID,twitterPassword); | ||
| 18 | return twitter; | ||
| 19 | } | ||
| 20 | |||
| 21 | public Paging getPaging() { | ||
| 22 | return new Paging(); | ||
| 23 | } | ||
| 24 | |||
| 25 | public List search(String text) { | ||
| 26 | return search(text, 0); | ||
| 27 | } | ||
| 28 | |||
| 29 | public List search(String text, int nb) { | ||
| 30 | String url = "http://search.twitter.com/search.atom?q=" + URLEncoder.encode(text); | ||
| 31 | if (nb!=0) | ||
| 32 | url += "&rpp=" + nb; | ||
| 33 | def resp = twitter.get(url, true); | ||
| 34 | def xml = resp.asString(); | ||
| 35 | def list = new ArrayList(); | ||
| 36 | def node = new XmlSlurper().parseText(xml) | ||
| 37 | for (entry in node.entry) { | ||
| 38 | String authoruri = entry.author.uri.text(); | ||
| 39 | String authorid = authoruri.substring(authoruri.lastIndexOf("/") + 1); | ||
| 40 | Date publishDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(entry.published.text().replaceAll("Z","+0000")); | ||
| 41 | def user = [ "id" : authorid, "name" : entry.author.name.text(), "screenName" : authorid, | ||
| 42 | "location" : "", "description" : "", "profileImageUrl": entry.link[1].href, "URL" : authoruri, "isProtected" : "", "followersCount" : -1]; | ||
| 43 | def st = [ "id" : entry.id.text(), "createdAt" : publishDate, "text" : entry.title.text(), "content" : entry.content.text(), "source" : "", "isTruncated" : false, "inReplyToStatusId": "", "inReplyToUserId": "", "user" : user ]; | ||
| 44 | list.add(st); | ||
| 45 | } | ||
| 46 | return list; | ||
| 47 | } | ||
| 48 | } |