8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
10 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
11 |
|
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
20 |
import com.gc.android.market.api.model.Market.CommentsRequest; |
import com.gc.android.market.api.model.Market.CommentsRequest; |
21 |
|
|
22 |
|
|
23 |
|
@WebServlet(urlPatterns={"/ShowStats"}) |
24 |
public class ShowStats extends HttpServlet { |
public class ShowStats extends HttpServlet { |
25 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
26 |
|
|
58 |
memcache = null; |
memcache = null; |
59 |
} |
} |
60 |
|
|
61 |
protected AppBean doLookup(String query) throws IOException { |
protected AppBean lookupApp(String query) throws IOException { |
62 |
|
|
63 |
|
|
64 |
String key = "marketstats:" + query.replace(' ', '_'); |
String key = "marketstats:" + query.replace(' ', '_'); |
66 |
AppBean response = (AppBean) memcache.get(key); |
AppBean response = (AppBean) memcache.get(key); |
67 |
|
|
68 |
if (response == null) { |
if (response == null) { |
69 |
response = doLookupWorker(query); |
response = lookupAppWorker(query); |
70 |
if (response != null) { |
if (response != null) { |
71 |
memcache.set(key, APP_TIMEOUT, response); |
memcache.set(key, APP_TIMEOUT, response); |
72 |
} |
} |
73 |
} |
} |
74 |
return response; |
return response; |
75 |
} |
} |
76 |
|
|
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 |
|
return appsCb.getResult().get(0); |
95 |
|
} |
96 |
|
|
97 |
CommentsRequest buildCommentRequest(String appId, int start, int count) { |
CommentsRequest buildCommentRequest(String appId, int start, int count) { |
98 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
124 |
count = Math.min(10, commentsCb.getEntryCount() ); |
count = Math.min(10, commentsCb.getEntryCount() ); |
125 |
|
|
126 |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
127 |
CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); |
for (int i=0; i<5;i++) { |
128 |
session.append(commentsRequest, commentsCb); |
CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); |
129 |
start +=10; |
session.append(commentsRequest, commentsCb); |
130 |
|
start +=10; |
131 |
|
} |
132 |
|
|
|
CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count); |
|
|
session.append(commentsRequest2, commentsCb); |
|
133 |
session.flush(); |
session.flush(); |
134 |
|
|
|
start +=10; |
|
|
|
|
135 |
if (start >= MAXCOMMENTS) |
if (start >= MAXCOMMENTS) |
136 |
break; //emergency brake |
break; //emergency brake |
137 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
|
|
|
|
|
protected AppBean doLookupWorker(String query) { |
|
|
final StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
|
MarketSession session = new MarketSession(); |
|
|
session.login(login,password); |
|
|
|
|
|
|
|
|
AppsRequest appsRequest = AppsRequest.newBuilder() |
|
|
.setQuery(query) |
|
|
.setStartIndex(0).setEntriesCount(10) |
|
|
.setWithExtendedInfo(true) |
|
|
.build(); |
|
|
|
|
|
AppsCallback appsCb = new AppsCallback(); |
|
|
session.append(appsRequest, appsCb); |
|
|
session.flush(); |
|
|
|
|
|
return appsCb.getResult(); |
|
|
} |
|
144 |
|
|
145 |
|
@SuppressWarnings("unchecked") |
146 |
public ArrayList<CommentBean> getComments(AppBean app) { |
public ArrayList<CommentBean> getComments(AppBean app) { |
147 |
String key = "marketstats:comments:" + app.getId(); |
String key = "marketstats:comments:" + app.getId(); |
148 |
ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key); |
ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key); |
155 |
} |
} |
156 |
|
|
157 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
158 |
String query = request.getParameter("query"); |
String query = "pname:" + request.getParameter("app"); |
159 |
|
|
160 |
|
request.setAttribute("extended", request.getParameter("extended")); |
161 |
|
|
162 |
AppBean app = doLookup(query); |
AppBean app = lookupApp(query); |
163 |
if (app != null) { |
if (app != null) { |
164 |
request.setAttribute("app", app); |
request.setAttribute("app", app); |
165 |
|
|