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