~ubuntu-branches/ubuntu/lucid/groovy/lucid

« back to all changes in this revision

Viewing changes to src/examples/swing/greet/TwitterAPI.groovy

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Torsten Werner, Varun Hiremath
  • Date: 2009-04-01 19:24:19 UTC
  • mfrom: (3.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090401192419-c5mpylqhcdkv3zuv
Tags: 1.6.0-1
[ Torsten Werner ]
* New upstream release (Closes: #521648)
* Remove Build-Depends: libclassworlds-java.
* Switch to source and target version 1.5.

[ Varun Hiremath ]
* Fix build.xml file
* Add ivy to Build-Depends
* Remove unnecessary Depends -- collections3, mx4j and xpp3 
* Add build.diff patch to fix a build error
* Use quilt to manage patches
* Update manpage (Closes: #507862)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Created by IntelliJ IDEA.
 
3
 * User: Danno.Ferrin
 
4
 * Date: Apr 25, 2008
 
5
 * Time: 9:47:20 PM
 
6
 */
 
7
package swing.greet
 
8
 
 
9
import groovy.beans.Bindable
 
10
 
 
11
class TwitterAPI {
 
12
 
 
13
    @Bindable String status = "\u00a0"
 
14
    def authenticatedUser
 
15
    XmlSlurper slurper = new XmlSlurper()
 
16
    def imageMap = [:]
 
17
 
 
18
    def withStatus(status, c) {
 
19
        setStatus(status)
 
20
        try {
 
21
            def o = c()
 
22
            setStatus("\u00a0")
 
23
            return o
 
24
        } catch (Throwable t) {
 
25
            setStatus("Error $status : ${t.message =~ '400'?'Rate Limit Reached':t}")
 
26
            throw t
 
27
        }
 
28
    }
 
29
 
 
30
 
 
31
    boolean login(def name, def password) {
 
32
        withStatus("Logging in") {
 
33
            Authenticator.setDefault(
 
34
                [getPasswordAuthentication : {
 
35
                    return new PasswordAuthentication(name, password) }
 
36
                ] as Authenticator)
 
37
            authenticatedUser = getUser(name)
 
38
            return true
 
39
        }
 
40
    }
 
41
 
 
42
    def getFriends() {
 
43
        getFriends(authenticatedUser)
 
44
    }
 
45
 
 
46
    def getFriends(String user) {
 
47
        return getFriends(getUser(user))
 
48
    }
 
49
 
 
50
    def getFriends(user) {
 
51
        def friends = [user]
 
52
        withStatus("Loading Friends") {
 
53
            def page = 1
 
54
            def list = slurper.parse(new URL("http://twitter.com/statuses/friends/${user.screen_name}.xml").openStream())
 
55
            while (list.length) {
 
56
                list.user.collect(friends) {it}
 
57
                page++
 
58
                try {
 
59
                  list = slurper.parse("http://twitter.com/statuses/friends/${user.screen_name}.xml&page=$page")
 
60
                } catch (Exception e) { break }
 
61
            }
 
62
        }
 
63
        withStatus("Loading Friends Images") {
 
64
            return friends.each {
 
65
                loadImage(it.profile_image_url as String)
 
66
            }
 
67
        }
 
68
    }
 
69
 
 
70
    def getFriendsTimeline() {
 
71
        getFriendsTimeline(user)
 
72
    }
 
73
 
 
74
    def getFriendsTimeline(String friend) {
 
75
        getFriendsTimeline(getUser(friend))
 
76
    }
 
77
 
 
78
    def getFriendsTimeline(user) {
 
79
        def timeline = []
 
80
        withStatus("Loading Timeline") {
 
81
            timeline =  slurper.parse(
 
82
                    new URL("http://twitter.com/statuses/friends_timeline/${user.screen_name}.xml").openStream()
 
83
                ).status.collect{it}
 
84
        }
 
85
        withStatus("Loading Timeline Images") {
 
86
            return timeline.each {
 
87
                loadImage(it.user.profile_image_url as String)
 
88
            }
 
89
        }
 
90
    }
 
91
 
 
92
    def getTweets() {
 
93
        return getTweets(user)
 
94
    }
 
95
 
 
96
    def getTweets(String friend) {
 
97
        return getTweets(getUser(frield))
 
98
    }
 
99
 
 
100
    def getTweets(friend) {
 
101
        def tweets = []
 
102
        withStatus("Loading Tweets") {
 
103
            tweets = slurper.parse(
 
104
                    new URL("http://twitter.com/statuses/user_timeline/${friend.screen_name}.xml").openStream()
 
105
                ).status.collect{it}
 
106
        }
 
107
        withStatus("Loading Tweet Images") {
 
108
            return tweets.each {
 
109
                loadImage(it.user.profile_image_url as String)
 
110
            }
 
111
        }
 
112
    }
 
113
 
 
114
    def getUser(String screen_name) {
 
115
        withStatus("Loading User $screen_name") {
 
116
            if (screen_name.contains('@')) {
 
117
                return slurper.parse(
 
118
                        new URL("http://twitter.com/users/show.xml?email=${screen_name}").openStream()
 
119
                    )
 
120
            } else {
 
121
                return slurper.parse(
 
122
                        new URL("http://twitter.com/users/show/${screen_name}.xml").openStream()
 
123
                    )
 
124
            }
 
125
        }
 
126
    }
 
127
 
 
128
    def tweet(message) {
 
129
        withStatus("Tweeting") {
 
130
            def urlConnection = new URL("http://twitter.com/statuses/update.xml").openConnection()
 
131
            urlConnection.doOutput = true
 
132
            urlConnection.outputStream << "status=${URLEncoder.encode(message, 'UTF-8')}"
 
133
            return slurper.parse(urlConnection.inputStream)
 
134
        }
 
135
    }
 
136
 
 
137
    // no need to read these, swing seems to cache these so the EDT won't stall
 
138
    def loadImage(image) {
 
139
        if (!imageMap[image]) {
 
140
            Thread.start {imageMap[image] = new javax.swing.ImageIcon(new URL(image))}
 
141
        }
 
142
    }
 
143
 
 
144
}
 
 
b'\\ No newline at end of file'