/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Contents of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 666 - (show annotations) (download)
Fri Apr 23 15:28:34 2010 UTC (14 years ago) by torben
File size: 3961 byte(s)
Limit comments to 20 per language
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
35 @Override
36 public void init() throws ServletException {
37 super.init();
38
39 login = getServletContext().getInitParameter("login");
40 password = getServletContext().getInitParameter("password");
41 }
42
43 protected String doLookup(String appId) throws IOException {
44 MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));
45
46
47 String key = "marketstats:" + appId;
48 String response = (String) c.get(key);
49
50 if (response == null) {
51 response = doLookupWorker(appId);
52 c.set(key, TIMEOUT, response);
53 response += "<!-- new lookup -->";
54 } else {
55 response += "<!-- from memcached -->";
56 }
57 return response;
58 }
59
60 private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) {
61 CommentCallback commentsCb = new CommentCallback();
62 commentsCb.setList( commentBeans );
63
64 commentsCb.setLocale( locale );
65 session.setLocale( new Locale(locale) );
66
67
68 int start = 0;
69 do {
70 int count = 10;
71 if (start > 0)
72 count = Math.min(10, commentsCb.getEntryCount() );
73
74 //log.warning("count=" + count + " start=" + start + " " + locale);
75 CommentsRequest commentsRequest = CommentsRequest.newBuilder()
76 .setAppId(appId)
77 .setStartIndex(start)
78 .setEntriesCount(count)
79 .build();
80
81 session.append(commentsRequest, commentsCb);
82 session.flush();
83 start +=10;
84
85 if (start >= 20)
86 break; //emergency brake
87
88 } while ( start < commentsCb.getEntryCount() );
89 }
90
91
92 protected String doLookupWorker(String appId) {
93 final StringBuilder sb = new StringBuilder();
94
95
96 MarketSession session = new MarketSession();
97 session.login(login,password);
98
99
100 AppsRequest appsRequest = AppsRequest.newBuilder()
101 .setAppId(appId)
102 .setStartIndex(0).setEntriesCount(10)
103 .setWithExtendedInfo(true)
104 .build();
105
106
107 AppsCallback appsCb = new AppsCallback() ;
108 appsCb.setStringBuffer(sb);
109
110 ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
111
112
113
114 session.append(appsRequest, appsCb);
115 session.flush();
116
117 loadComments(appId, session, "da", commentBeans);
118 loadComments(appId, session, "en", commentBeans);
119
120 Collections.sort(commentBeans);
121
122 sb.append("-----------------------------------------------------------------\n");
123 for (CommentBean c : commentBeans) {
124 sb.append("User: " + c.author + " (" + c.locale + ")\n");
125 sb.append("Rating: " + c.rating + "\n");
126 sb.append("Time: " + new Date(c.time).toString() + "\n");
127 sb.append( c.text + "\n");
128 sb.append("\n");
129
130 }
131 sb.append("Comments: " + commentBeans.size() );
132
133
134 return sb.toString();
135 }
136
137 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
138 String appId = request.getParameter("appId");
139
140 response.setContentType("text/html");
141 PrintWriter out = response.getWriter();
142 out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" );
143 }
144
145 }

  ViewVC Help
Powered by ViewVC 1.1.20