1 |
package dk.thoerup.marketstats;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.io.PrintWriter;
|
5 |
import java.net.InetSocketAddress;
|
6 |
import java.util.ArrayList;
|
7 |
import java.util.Collections;
|
8 |
import java.util.Date;
|
9 |
import java.util.Locale;
|
10 |
import java.util.logging.Logger;
|
11 |
|
12 |
import javax.servlet.ServletException;
|
13 |
import javax.servlet.http.HttpServlet;
|
14 |
import javax.servlet.http.HttpServletRequest;
|
15 |
import javax.servlet.http.HttpServletResponse;
|
16 |
|
17 |
import net.spy.memcached.MemcachedClient;
|
18 |
|
19 |
import com.gc.android.market.api.MarketSession;
|
20 |
import com.gc.android.market.api.model.Market.AppsRequest;
|
21 |
import com.gc.android.market.api.model.Market.CommentsRequest;
|
22 |
|
23 |
|
24 |
|
25 |
public class ShowStats extends HttpServlet {
|
26 |
private static final long serialVersionUID = 1L;
|
27 |
|
28 |
final int TIMEOUT = 30*60;
|
29 |
static final Logger log = Logger.getLogger(ShowStats.class.getName());
|
30 |
|
31 |
String login;
|
32 |
String password;
|
33 |
|
34 |
MemcachedClient memcache = null;
|
35 |
|
36 |
|
37 |
@Override
|
38 |
public void init() throws ServletException {
|
39 |
super.init();
|
40 |
|
41 |
login = getServletContext().getInitParameter("login");
|
42 |
password = getServletContext().getInitParameter("password");
|
43 |
|
44 |
try {
|
45 |
memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
|
46 |
} catch (IOException e) {
|
47 |
throw new ServletException(e);
|
48 |
}
|
49 |
}
|
50 |
|
51 |
protected String doLookup(String query) throws IOException {
|
52 |
|
53 |
|
54 |
String key = "marketstats:" + query;
|
55 |
String response = (String) memcache.get(key);
|
56 |
|
57 |
if (response == null) {
|
58 |
response = doLookupWorker(query);
|
59 |
memcache.set(key, TIMEOUT, response);
|
60 |
response += "<!-- new lookup -->";
|
61 |
} else {
|
62 |
response += "<!-- from memcached -->";
|
63 |
}
|
64 |
return response;
|
65 |
}
|
66 |
|
67 |
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {
|
68 |
CommentCallback commentsCb = new CommentCallback();
|
69 |
commentsCb.setList( commentBeans );
|
70 |
|
71 |
session.setLocale( Locale.ROOT );
|
72 |
|
73 |
|
74 |
|
75 |
int start = 0;
|
76 |
do {
|
77 |
int count = 10;
|
78 |
if (start > 0)
|
79 |
count = Math.min(10, commentsCb.getEntryCount() );
|
80 |
|
81 |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
|
82 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder()
|
83 |
.setAppId(appId)
|
84 |
.setStartIndex(start)
|
85 |
.setEntriesCount(count)
|
86 |
.build();
|
87 |
|
88 |
session.append(commentsRequest, commentsCb);
|
89 |
session.flush();
|
90 |
start +=10;
|
91 |
|
92 |
if (start >= 200)
|
93 |
break; //emergency brake
|
94 |
|
95 |
} while ( start < commentsCb.getEntryCount() );
|
96 |
}
|
97 |
|
98 |
|
99 |
protected String doLookupWorker(String query) {
|
100 |
final StringBuilder sb = new StringBuilder();
|
101 |
|
102 |
|
103 |
MarketSession session = new MarketSession();
|
104 |
session.login(login,password);
|
105 |
|
106 |
|
107 |
AppsRequest appsRequest = AppsRequest.newBuilder()
|
108 |
.setQuery(query)
|
109 |
.setStartIndex(0).setEntriesCount(10)
|
110 |
.setWithExtendedInfo(true)
|
111 |
.build();
|
112 |
|
113 |
|
114 |
AppsCallback appsCb = new AppsCallback() ;
|
115 |
appsCb.setStringBuffer(sb);
|
116 |
|
117 |
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
|
118 |
|
119 |
|
120 |
|
121 |
session.append(appsRequest, appsCb);
|
122 |
session.flush();
|
123 |
|
124 |
String appId = appsCb.getAppId();
|
125 |
|
126 |
if (appId != null) {
|
127 |
|
128 |
loadComments(appId, session, commentBeans);
|
129 |
|
130 |
sb.append("-----------------------------------------------------------------\n");
|
131 |
for (CommentBean c : commentBeans) {
|
132 |
sb.append("User: " + c.author + "\n");
|
133 |
sb.append("Rating: " + c.rating + "\n");
|
134 |
sb.append("Time: " + new Date(c.time).toString() + "\n");
|
135 |
sb.append( c.text + "\n");
|
136 |
sb.append("\n");
|
137 |
|
138 |
}
|
139 |
sb.append("Comments: " + commentBeans.size() + "\n");
|
140 |
}
|
141 |
sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes");
|
142 |
|
143 |
|
144 |
return sb.toString();
|
145 |
}
|
146 |
|
147 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
148 |
String query = request.getParameter("query");
|
149 |
|
150 |
response.setContentType("text/html");
|
151 |
PrintWriter out = response.getWriter();
|
152 |
out.print( "<html><body><pre>" + doLookupWorker(query) + "</pre></body></html>" );
|
153 |
}
|
154 |
|
155 |
}
|