My favorites
▼
|
Sign in
lindenb
Pierre Lindenbaum's library
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
proj
/
tinytools
/
src
/
org
/
lindenb
/
tinytools
/
FriendFeedMap.java
r508
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package org.lindenb.tinytools;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.lindenb.me.Me;
import org.lindenb.treemap.TreeMap;
import org.lindenb.treemap.TreeMapModeler;
import org.lindenb.util.Compilation;
import org.lindenb.xml.XMLUtilities;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
/**
* Paint a treemap of the activity in FriendFeed.
*
*/
public class FriendFeedMap
{
/**
* A user in friendFeed
*
*/
private class User
implements TreeMap
{
String uri;
String name;
int post=0;
int like=0;
int comment=0;
Rectangle2D bounds=null;
@Override
public Rectangle2D getBounds() {
return this.bounds;
}
@Override
public void setBounds(Rectangle2D bounds) {
this.bounds=bounds;
}
@Override
public double getWeight()
{
switch(FriendFeedMap.this.observe)
{
case 0: return post;
case 1: return like;
case 2: return comment;
}
throw new IllegalStateException();
}
}
final static String DEFAULT_ROOM="the-life-scientists";
private String roomName=DEFAULT_ROOM;
private int observe=0;
private DocumentBuilder docBuilder;
private Map<String, User> uri2user=new HashMap<String, User>();
private XPathExpression xpathEntries;
private XPathExpression xpathUser;
private XPathExpression xpathUserProfileUrl;
private XPathExpression xpathUserName;
private XPathExpression xpathLike;
private XPathExpression xpathComment;
private Rectangle outputRect= new Rectangle(0,0,400,500);
private String outputFile="_treemap";
private FriendFeedMap() throws ParserConfigurationException, XPathExpressionException
{
DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
f.setCoalescing(true);
f.setNamespaceAware(true);
f.setValidating(false);
f.setExpandEntityReferences(true);
f.setIgnoringComments(false);
f.setIgnoringElementContentWhitespace(true);
docBuilder= f.newDocumentBuilder();
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
xpathEntries= xpath.compile("/feed/entry");
xpathUser= xpath.compile("user");
xpathUserProfileUrl= xpath.compile("profileUrl/text()");
xpathUserName= xpath.compile("name/text()");
xpathLike= xpath.compile("like");
xpathComment= xpath.compile("comment");
}
private User findUser(Node root) throws XPathExpressionException,
IOException,SAXException
{
Node node= (Node)xpathUser.evaluate(root,XPathConstants.NODE);
if(node==null) return null;
String userUri = (String)xpathUserProfileUrl.evaluate(node, XPathConstants.STRING);
if(userUri==null || userUri.trim().length()==0) return null;
User user= this.uri2user.get(userUri);
if(user==null)
{
user= new User();
user.uri= userUri;
user.name= (String)xpathUserName.evaluate(node,XPathConstants.STRING);
this.uri2user.put(userUri, user);
System.err.println("found new "+userUri+" "+this.uri2user.size());
}
return user;
}
private void parseFeeds() throws IOException,SAXException,XPathExpressionException
{
int start=0;
final int num=100;
while(true)
{
String uri="http://friendfeed.com/api/feed/room/"+roomName+"?format=xml&start="+start+"&num="+num;
System.err.println(uri);
Document dom= this.docBuilder.parse(uri);
NodeList entries=(NodeList)xpathEntries.evaluate(dom, XPathConstants.NODESET);
for(int i=0;i< entries.getLength();++i)
{
User user =findUser(entries.item(i));
if(user!=null) user.post++;
NodeList nodelist=(NodeList)xpathLike.evaluate(entries.item(i), XPathConstants.NODESET);
for(int j=0;j< nodelist.getLength();++j)
{
user =findUser(nodelist.item(j));
if(user!=null) user.like++;
}
nodelist=(NodeList)xpathComment.evaluate(entries.item(i), XPathConstants.NODESET);
for(int j=0;j< nodelist.getLength();++j)
{
user =findUser(nodelist.item(j));
if(user!=null) user.comment++;
}
}
if(entries.getLength()==0) break;
start+=entries.getLength();
}
List<User> users= new ArrayList<User>(this.uri2user.size());
for(String uri:this.uri2user.keySet())
{
User u= uri2user.get(uri);
if(u.getWeight()<=0) continue;
users.add(u);
}
TreeMapModeler modeler= new TreeMapModeler();
modeler.layout(users, new Rectangle2D.Double(0,0,
this.outputRect.width,
this.outputRect.height));
BufferedImage img= new BufferedImage(
this.outputRect.width,
this.outputRect.height,
BufferedImage.TYPE_INT_RGB
);
Graphics2D g= img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(User u:users)
{
URL url=new URL(u.uri+"/picture?size=large");
BufferedImage picture= ImageIO.read(url);
Rectangle2D rect = u.getBounds();
g.drawImage(picture,(int)rect.getX(), (int)rect.getY(),
(int)rect.getWidth(), (int)rect.getHeight(), null);
}
g.dispose();
ImageIO.write(img, "png", new File(this.outputFile+".png"));
PrintWriter out= new PrintWriter(new File(this.outputFile+".html"));
out.println("<html><body><div style='text-align:center;'>");
out.print("<map name='map1'>");
for(User u:users)
{
Rectangle2D rect = u.getBounds();
out.print("<area href='"+ u.uri+"' ");
out.print("alt='"+ XMLUtilities.escape(u.name)+"' ");
out.print("title='"+ XMLUtilities.escape(u.name)+"' ");
out.print("shape='rect' ");
out.print("coords='"+(int)rect.getX()+","+(int)rect.getY()+","+
(int)rect.getMaxX()+","+ (int)rect.getMaxY()+"' ");
out.print("/>");
}
out.print("</map>");
out.println("<img src='jeter.png' width='"+this.outputRect.width+"' height='"+this.outputRect.height+"' usemap='#map1'/>");
out.println("</div></body></html>");
out.flush();
out.close();
}
public static void main(String[] args)
{
try
{
FriendFeedMap app= new FriendFeedMap();
int optind=0;
while(optind< args.length)
{
if(args[optind].equals("-h"))
{
System.err.println("Pierre Lindenbaum PhD 2009. "+Me.WWW+" "+Me.MAIL);
System.err.println(Compilation.getLabel());
System.err.println("Creates a Treemap for a FriendFeed room.");
System.err.println("-room <name> default:"+app.roomName);
System.err.println("-t <integer> 0: observe posts. 1 observe likes. 2 observe comments. default:0");
System.err.println("-w <integer> width default:"+app.outputRect.width);
System.err.println("-h <integer> height default:"+app.outputRect.height);
System.err.println("-o <base-file> file default:"+app.outputFile);
}
else if(args[optind].equals("-room"))
{
app.roomName=args[++optind];
}
else if(args[optind].equals("-t"))
{
app.observe=Integer.parseInt(args[++optind]);
if(app.observe<0 || app.observe>2)
{
System.err.println("Bad value for -t");
return;
}
}
else if(args[optind].equals("-room"))
{
app.roomName=args[++optind];
}
else if(args[optind].equals("-o"))
{
app.outputFile=args[++optind];
}
else if(args[optind].equals("-w"))
{
app.outputRect.width=Integer.parseInt(args[++optind]);
}
else if(args[optind].equals("-h"))
{
app.outputRect.height=Integer.parseInt(args[++optind]);
}
else if(args[optind].equals("--"))
{
optind++;
break;
}
else if(args[optind].startsWith("-"))
{
System.err.println("Unknown option "+args[optind]);
}
else
{
break;
}
++optind;
}
app.parseFeeds();
}
catch(Throwable err)
{
err.printStackTrace();
}
}
}
Show details
Hide details
Change log
r356
by plindenbaum on Aug 4, 2009
Diff
[No log message]
Go to:
...enb/tinytools/FriendFeedMap.java
...denb/treemap/TreeMapModeler.java
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 8165 bytes, 289 lines
View raw file
Powered by
Google Project Hosting