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