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

  ViewVC Help
Powered by ViewVC 1.1.20