-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterAPI.pde
154 lines (127 loc) · 4.4 KB
/
TwitterAPI.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
*
* EXCERPT FROM http://code.google.com/p/processing/wiki/LibraryOverview
* =====================================================================
*
* Using the code folder
* ---------------------
*
* If all you want to do is add some Java code to a sketch, sometimes you don't
* even need to build a full library. If you add a .jar file to your sketch
* (using Sketch → Add File), the file will be copied to a folder named code
* inside your sketch folder. When running the sketch, all packages found in the
* .jar file will automatically be added as import statements in the sketch
* (though invisible to the user).
*
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.*;
class TwitterAPI {
/**
* Debug Flag
*/
private final boolean verbose = false;
/**
* Cache Size
*
* Always try to keep X tweets.
*/
private int cacheSize = 100;
/**
* Return Threshold
*
* Block execution until threshold is reached.
*/
private int blockUntil = 20;
/**
* Cached Tweets
*
* Every time <code>getTweet()</code> is called, one tweet JSONObject is taken
* out from <code>cachedTweets</code>. When we runs out of cached tweets,
* <code>getTweet()</code> will fetch more from Twitter API and store them.
*/
private RollingStringArrayList cachedTweets = new RollingStringArrayList(cacheSize);
/**
* Twitter stuff
*
* It would make sense if Google Docs / FusionTables asked for OAuth
* authentication, but why do I have to be OAuth-authenticated to search
* Twitter... Why not just use IP-based rate limiting...
*
* Do not abuse my keys... anyone using this combination shares the
* 180reqs/15mins quota, or the 1-stream-at-all-times limit.
*/
private String consumerKey;
private String consumerSecret;
private String accessToken;
private String accessTokenSecret;
TwitterAPI(String consumerKey,
String consumerSecret,
String accessToken,
String accessTokenSecret) {
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.accessToken = accessToken;
this.accessTokenSecret = accessTokenSecret;
streamInit();
// dont return until we have some tweets
while (cachedTweets.size() <= blockUntil) {
try{Thread.sleep(100);}catch(Exception e){}
}
}
private void streamInit() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(false)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessTokenSecret);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
// System.out.println(/*"@" + status.getUser().getScreenName() + ": " + */status.getText());
cachedTweets.add(status.getText());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
// omg do i really have to implement these...
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onScrubGeo(long userId, long upToStatusId) {}
};
twitterStream.addListener(listener);
// filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.sample();
}
String getTweet() {
return teddyFilter(cachedTweets.pop());
}
private String teddyFilter(String raw) {
Pattern pattern;
Matcher matcher;
// line breaks
pattern = Pattern.compile("[\n\r]+");
matcher = pattern.matcher(raw);
raw = matcher.replaceAll(" ");
// <
pattern = Pattern.compile("<");
matcher = pattern.matcher(raw);
raw = matcher.replaceAll("<");
// >
pattern = Pattern.compile(">");
matcher = pattern.matcher(raw);
raw = matcher.replaceAll(">");
// &
pattern = Pattern.compile("&");
matcher = pattern.matcher(raw);
raw = matcher.replaceAll("&");
return raw;
}
}