-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.pde
55 lines (46 loc) · 1.77 KB
/
twitter.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
void initTwitter() {
AtomFeed feed;
if (ONLINE) {
feed = AtomFeed.newFromURL("http://search.twitter.com/search.atom?q=" + QUERY);
}
else {
feed = AtomFeed.newFromStream(openStream("search.atom"));
}
HashMap<String, Integer> uniqueAuthors = new HashMap<String, Integer>();
for (AtomEntry e : feed.entries) {
String name = e.author.name;
if (uniqueAuthors.containsKey(name)) {
int freq = uniqueAuthors.get(name);
uniqueAuthors.put(name, ++freq);
println(name + " = " + freq);
}
else {
println("new name: " + name);
uniqueAuthors.put(name, 1);
}
}
Collections.sort(feed.entries, new Comparator<AtomEntry>() {
public int compare(AtomEntry a, AtomEntry b) {
long atime = a.timePublished.toGregorianCalendar().getTimeInMillis();
long btime = b.timePublished.toGregorianCalendar().getTimeInMillis();
return (int)(atime - btime);
}
});
// find the minimum and maximum times of the tweets
long tmin = feed.entries.get(0).timePublished.toGregorianCalendar().getTimeInMillis();
long tmax = feed.entries.get(feed.entries.size() - 1).timePublished.toGregorianCalendar().getTimeInMillis();
// map entries on grid
List<String> names = new ArrayList<String>(uniqueAuthors.keySet());
for (AtomEntry e : feed.entries) {
// get the index of the current author
int a = names.indexOf(e.author.name);
// get the time of this tweet
long t = e.timePublished.toGregorianCalendar().getTimeInMillis();
// map to screen coordinates
float x = map(a, 0, uniqueAuthors.size() - 1, -RESX / 2, RESX / 2 - 1);
float y = map(t, tmin, tmax, RESY/2 - 1, -RESY / 2);
// make a TweetPoint
TweetPoint tp = new TweetPoint(new Vec2D(x, y), e);
tweets.add(tp);
}
}