1 |
package dk.thoerup.marketstats; |
package dk.thoerup.marketstats; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.io.PrintWriter; |
|
4 |
import java.net.InetSocketAddress; |
import java.net.InetSocketAddress; |
5 |
import java.util.Date; |
import java.util.ArrayList; |
|
import java.util.Formatter; |
|
|
import java.util.List; |
|
6 |
import java.util.Locale; |
import java.util.Locale; |
7 |
|
import java.util.concurrent.TimeUnit; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
10 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
15 |
import net.spy.memcached.MemcachedClient; |
import net.spy.memcached.MemcachedClient; |
16 |
|
|
17 |
import com.gc.android.market.api.MarketSession; |
import com.gc.android.market.api.MarketSession; |
|
import com.gc.android.market.api.MarketSession.Callback; |
|
|
import com.gc.android.market.api.model.Market.App; |
|
18 |
import com.gc.android.market.api.model.Market.AppsRequest; |
import com.gc.android.market.api.model.Market.AppsRequest; |
|
import com.gc.android.market.api.model.Market.AppsResponse; |
|
|
import com.gc.android.market.api.model.Market.Comment; |
|
19 |
import com.gc.android.market.api.model.Market.CommentsRequest; |
import com.gc.android.market.api.model.Market.CommentsRequest; |
20 |
import com.gc.android.market.api.model.Market.CommentsResponse; |
|
|
import com.gc.android.market.api.model.Market.ResponseContext; |
|
21 |
|
|
22 |
|
|
23 |
public class ShowStats extends HttpServlet { |
public class ShowStats extends HttpServlet { |
24 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
25 |
|
|
26 |
final int TIMEOUT = 15*60; |
static final int MAXCOMMENTS = 30; |
27 |
|
static final int APP_TIMEOUT = 30*60; |
28 |
|
static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4; |
29 |
|
|
30 |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
31 |
|
|
32 |
String login; |
String login; |
33 |
String password; |
String password; |
34 |
|
|
35 |
|
MemcachedClient memcache = null; |
36 |
|
|
37 |
|
|
38 |
@Override |
@Override |
41 |
|
|
42 |
login = getServletContext().getInitParameter("login"); |
login = getServletContext().getInitParameter("login"); |
43 |
password = getServletContext().getInitParameter("password"); |
password = getServletContext().getInitParameter("password"); |
44 |
|
|
45 |
|
try { |
46 |
|
memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
47 |
|
} catch (IOException e) { |
48 |
|
throw new ServletException(e); |
49 |
|
} |
50 |
|
} |
51 |
|
|
52 |
|
@Override |
53 |
|
public void destroy() { |
54 |
|
super.destroy(); |
55 |
|
|
56 |
|
memcache.shutdown(3, TimeUnit.SECONDS); |
57 |
|
memcache = null; |
58 |
} |
} |
59 |
|
|
60 |
protected String doLookup(String appId) throws IOException { |
protected AppBean doLookup(String query) throws IOException { |
|
MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
|
|
c.flush(); |
|
61 |
|
|
62 |
|
|
63 |
String key = "marketstats:" + appId; |
String key = "marketstats:" + query.replace(' ', '_'); |
64 |
String response = (String) c.get(key); |
//String response = (String) memcache.get(key); |
65 |
|
AppBean response = (AppBean) memcache.get(key); |
66 |
|
|
67 |
if (response == null) { |
if (response == null) { |
68 |
response = doLookupWorker(appId); |
response = doLookupWorker(query); |
69 |
c.set(key, TIMEOUT, response); |
if (response != null) { |
70 |
response += "<!-- new lookup -->"; |
memcache.set(key, APP_TIMEOUT, response); |
71 |
} else { |
} |
72 |
response += "<!-- from memcached -->"; |
} |
|
} |
|
73 |
return response; |
return response; |
74 |
} |
} |
75 |
|
|
76 |
|
CommentsRequest buildCommentRequest(String appId, int start, int count) { |
77 |
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
78 |
|
.setAppId(appId) |
79 |
|
.setStartIndex(start) |
80 |
|
.setEntriesCount(count) |
81 |
|
.build(); |
82 |
|
|
83 |
|
return commentsRequest; |
84 |
|
} |
85 |
|
|
86 |
|
private ArrayList<CommentBean> loadComments(String appId) { |
87 |
|
|
88 |
|
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
89 |
|
MarketSession session = new MarketSession(); |
90 |
|
session.login(login,password); |
91 |
|
|
92 |
|
CommentCallback commentsCb = new CommentCallback(); |
93 |
|
commentsCb.setList( commentBeans ); |
94 |
|
|
95 |
|
session.setLocale( Locale.ROOT ); |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
int start = 0; |
100 |
|
do { |
101 |
|
int count = 10; |
102 |
|
if (start > 0) |
103 |
|
count = Math.min(10, commentsCb.getEntryCount() ); |
104 |
|
|
105 |
|
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
106 |
|
CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); |
107 |
|
session.append(commentsRequest, commentsCb); |
108 |
|
start +=10; |
109 |
|
|
110 |
|
CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count); |
111 |
|
session.append(commentsRequest2, commentsCb); |
112 |
|
session.flush(); |
113 |
|
|
114 |
|
start +=10; |
115 |
|
|
116 |
|
if (start >= MAXCOMMENTS) |
117 |
|
break; //emergency brake |
118 |
|
|
119 |
|
} while ( start < commentsCb.getEntryCount() ); |
120 |
|
|
121 |
|
return commentBeans; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
protected String doLookupWorker(String appId) { |
protected AppBean doLookupWorker(String query) { |
127 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
128 |
|
|
129 |
|
|
130 |
MarketSession session = new MarketSession(); |
MarketSession session = new MarketSession(); |
131 |
session.login(login,password); |
session.login(login,password); |
|
session.setLocale( Locale.ENGLISH ); |
|
132 |
|
|
|
AppsRequest appsRequest = AppsRequest.newBuilder() |
|
|
.setAppId(appId) |
|
|
.setStartIndex(0).setEntriesCount(10) |
|
|
.setWithExtendedInfo(true) |
|
|
.build(); |
|
133 |
|
|
134 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
AppsRequest appsRequest = AppsRequest.newBuilder() |
135 |
.setAppId(appId) |
.setQuery(query) |
136 |
.setStartIndex(0) |
.setStartIndex(0) |
137 |
.setEntriesCount(10) |
.setEntriesCount(1) |
138 |
|
.setWithExtendedInfo(true) |
139 |
.build(); |
.build(); |
140 |
|
|
141 |
|
AppsCallback appsCb = new AppsCallback(); |
|
|
|
|
|
|
|
Callback<AppsResponse> appsCb = new Callback<AppsResponse>() { |
|
|
@Override |
|
|
public void onResult(ResponseContext context, AppsResponse response) { |
|
|
//System.out.println("Response : " + response); |
|
|
//sb.append("Response: " + response + "\n"); |
|
|
|
|
|
Formatter form = new Formatter(sb); |
|
|
App app = response.getApp(0); |
|
|
sb.append( "<h2>" + app.getTitle() + "</h2>"); |
|
|
sb.append("Ver: " + app.getVersion() + "(" + app.getVersionCode() + ")\n" ); |
|
|
sb.append("Ratingcount: " + app.getRatingsCount() + "\n"); |
|
|
|
|
|
sb.append("Rating: " ); |
|
|
double rating = Double.parseDouble( app.getRating() ); |
|
|
form.format("%.4f", rating); |
|
|
sb.append("\n"); |
|
|
|
|
|
sb.append("Downloads: " + app.getExtendedInfo().getDownloadsCountText() + "(" + app.getExtendedInfo().getDownloadsCount() + ")\n" ); |
|
|
|
|
|
sb.append("\n"); |
|
|
|
|
|
} |
|
|
}; |
|
|
|
|
|
Callback<CommentsResponse> commentsCb = new Callback<CommentsResponse>() { |
|
|
|
|
|
@Override |
|
|
public void onResult(ResponseContext context, CommentsResponse response) { |
|
|
//System.out.println("Response : " + response); |
|
|
//sb.append("Response: " + response + "\n"); |
|
|
sb.append("--------------------------------------------------------------\n"); |
|
|
sb.append("Total comments: " + response.getEntriesCount() +"\n\n"); |
|
|
|
|
|
|
|
|
List<Comment> cl = response.getCommentsList(); |
|
|
for (Comment c : cl) { |
|
|
sb.append("User: " + c.getAuthorName() + "\n"); |
|
|
sb.append("Rating: " + c.getRating() + "\n"); |
|
|
sb.append("Time: " + new Date(c.getCreationTime()).toString() + "\n"); |
|
|
sb.append( c.getText() + "\n"); |
|
|
sb.append("\n"); |
|
|
sb.append(c.getUnknownFields().toString() ); |
|
|
|
|
|
} |
|
|
sb.append( response.getUnknownFields().toString() ); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
142 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
|
session.append(commentsRequest, commentsCb); |
|
143 |
session.flush(); |
session.flush(); |
144 |
session.setLocale( new Locale("da") ); |
|
145 |
session.append(commentsRequest, commentsCb); |
return appsCb.getResult(); |
146 |
session.flush(); |
} |
147 |
|
|
148 |
return sb.toString(); |
public ArrayList<CommentBean> getComments(AppBean app) { |
149 |
|
String key = "marketstats:comments:" + app.getId(); |
150 |
|
ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key); |
151 |
|
|
152 |
|
if (comments == null) { |
153 |
|
comments = loadComments(app.getId()); |
154 |
|
memcache.set(key, COMMENT_TIMEOUT, comments); |
155 |
|
} |
156 |
|
return comments; |
157 |
} |
} |
158 |
|
|
159 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
160 |
String appId = request.getParameter("appId"); |
String query = request.getParameter("query"); |
161 |
PrintWriter out = response.getWriter(); |
|
162 |
|
AppBean app = doLookup(query); |
163 |
out.print( "<pre>" + doLookup(appId) + "</pre>" ); |
if (app != null) { |
164 |
|
request.setAttribute("app", app); |
165 |
|
|
166 |
|
ArrayList<CommentBean> comments = getComments(app); |
167 |
|
request.setAttribute("comments", comments); |
168 |
|
|
169 |
|
getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response); |
170 |
|
} else { |
171 |
|
response.getWriter().print("No app found with query=" + query); |
172 |
|
} |
173 |
} |
} |
174 |
|
|
175 |
} |
} |