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