/[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 1268 - (show annotations) (download)
Wed Apr 6 05:10:35 2011 UTC (13 years, 1 month ago) by torben
File size: 4800 byte(s)
Upgrade to api 0.6
1 package dk.thoerup.marketstats;
2
3 import java.io.IOException;
4 import java.net.InetSocketAddress;
5 import java.util.ArrayList;
6 import java.util.Locale;
7 import java.util.concurrent.TimeUnit;
8 import java.util.logging.Logger;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import net.spy.memcached.MemcachedClient;
17
18 import com.gc.android.market.api.MarketSession;
19 import com.gc.android.market.api.model.Market.AppsRequest;
20 import com.gc.android.market.api.model.Market.CommentsRequest;
21
22
23 @WebServlet(urlPatterns={"/ShowStats"})
24 public class ShowStats extends HttpServlet {
25 private static final long serialVersionUID = 1L;
26
27 static final int MAXCOMMENTS = 30;
28 static final int APP_TIMEOUT = 30*60;
29 static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4;
30
31 static final Logger log = Logger.getLogger(ShowStats.class.getName());
32
33 String login;
34 String password;
35
36 MemcachedClient memcache = null;
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 try {
47 memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
48 } catch (IOException e) {
49 throw new ServletException(e);
50 }
51 }
52
53 @Override
54 public void destroy() {
55 super.destroy();
56
57 memcache.shutdown(3, TimeUnit.SECONDS);
58 memcache = null;
59 }
60
61 protected AppBean lookupApp(String query) throws IOException {
62
63
64 String key = "marketstats:" + query.replace(' ', '_');
65 //String response = (String) memcache.get(key);
66 AppBean response = (AppBean) memcache.get(key);
67
68 if (response == null) {
69 response = lookupAppWorker(query);
70 if (response != null) {
71 memcache.set(key, APP_TIMEOUT, response);
72 }
73 }
74 return response;
75 }
76
77 protected AppBean lookupAppWorker(String query) {
78
79 MarketSession session = new MarketSession();
80 session.login(login,password);
81 //session.getContext().setAndroidId("dead00beef");
82
83
84 AppsRequest appsRequest = AppsRequest.newBuilder()
85 .setQuery(query)
86 .setStartIndex(0)
87 .setEntriesCount(1)
88 .setWithExtendedInfo(true)
89 .build();
90
91 AppsCallback appsCb = new AppsCallback();
92 session.append(appsRequest, appsCb);
93 session.flush();
94
95 return appsCb.getResult().get(0);
96 }
97
98 CommentsRequest buildCommentRequest(String appId, int start, int count) {
99 CommentsRequest commentsRequest = CommentsRequest.newBuilder()
100 .setAppId(appId)
101 .setStartIndex(start)
102 .setEntriesCount(count)
103 .build();
104
105 return commentsRequest;
106 }
107
108 private ArrayList<CommentBean> loadComments(String appId) {
109
110 ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
111 MarketSession session = new MarketSession();
112 session.login(login,password);
113
114 CommentCallback commentsCb = new CommentCallback();
115 commentsCb.setList( commentBeans );
116
117 session.setLocale( Locale.ROOT );
118
119
120
121 int start = 0;
122 do {
123 int count = 10;
124 if (start > 0)
125 count = Math.min(10, commentsCb.getEntryCount() );
126
127 //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
128 for (int i=0; i<5;i++) {
129 CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);
130 session.append(commentsRequest, commentsCb);
131 start +=10;
132 }
133
134 session.flush();
135
136 if (start >= MAXCOMMENTS)
137 break; //emergency brake
138
139 } while ( start < commentsCb.getEntryCount() );
140
141 return commentBeans;
142 }
143
144
145
146 @SuppressWarnings("unchecked")
147 public ArrayList<CommentBean> getComments(AppBean app) {
148 String key = "marketstats:comments:" + app.getId();
149 ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key);
150
151 if (comments == null) {
152 comments = loadComments(app.getId());
153 memcache.set(key, COMMENT_TIMEOUT, comments);
154 }
155 return comments;
156 }
157
158 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
159 String query = "pname:" + request.getParameter("app");
160
161 request.setAttribute("extended", request.getParameter("extended"));
162
163 AppBean app = lookupApp(query);
164 if (app != null) {
165 request.setAttribute("app", app);
166
167 ArrayList<CommentBean> comments = getComments(app);
168 request.setAttribute("comments", comments);
169
170 getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response);
171 } else {
172 response.getWriter().print("No app found with query=" + query);
173 }
174 }
175
176 }

  ViewVC Help
Powered by ViewVC 1.1.20