4 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
5 |
import java.net.InetSocketAddress; |
import java.net.InetSocketAddress; |
6 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.Collections; |
|
7 |
import java.util.Date; |
import java.util.Date; |
|
import java.util.Formatter; |
|
8 |
import java.util.Locale; |
import java.util.Locale; |
9 |
|
import java.util.concurrent.TimeUnit; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
12 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
17 |
import net.spy.memcached.MemcachedClient; |
import net.spy.memcached.MemcachedClient; |
18 |
|
|
19 |
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; |
|
20 |
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; |
|
21 |
import com.gc.android.market.api.model.Market.CommentsRequest; |
import com.gc.android.market.api.model.Market.CommentsRequest; |
22 |
import com.gc.android.market.api.model.Market.ResponseContext; |
|
23 |
|
|
24 |
|
|
25 |
public class ShowStats extends HttpServlet { |
public class ShowStats extends HttpServlet { |
30 |
|
|
31 |
String login; |
String login; |
32 |
String password; |
String password; |
33 |
|
|
34 |
|
MemcachedClient memcache = null; |
35 |
|
|
36 |
|
|
37 |
@Override |
@Override |
40 |
|
|
41 |
login = getServletContext().getInitParameter("login"); |
login = getServletContext().getInitParameter("login"); |
42 |
password = getServletContext().getInitParameter("password"); |
password = getServletContext().getInitParameter("password"); |
43 |
|
|
44 |
|
try { |
45 |
|
memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
46 |
|
} catch (IOException e) { |
47 |
|
throw new ServletException(e); |
48 |
|
} |
49 |
} |
} |
50 |
|
|
51 |
protected String doLookup(String appId) throws IOException { |
@Override |
52 |
MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
public void destroy() { |
53 |
|
super.destroy(); |
54 |
|
|
55 |
|
memcache.shutdown(3, TimeUnit.SECONDS); |
56 |
|
memcache = null; |
57 |
|
} |
58 |
|
|
59 |
|
protected String doLookup(String query) throws IOException { |
60 |
|
|
61 |
|
|
62 |
String key = "marketstats:" + appId; |
String key = "marketstats:" + query.replace(' ', '_'); |
63 |
String response = (String) c.get(key); |
String response = (String) memcache.get(key); |
64 |
|
|
65 |
if (response == null) { |
if (response == null) { |
66 |
response = doLookupWorker(appId); |
response = doLookupWorker(query); |
67 |
c.set(key, TIMEOUT, response); |
memcache.set(key, TIMEOUT, response); |
68 |
response += "<!-- new lookup -->"; |
response += "<!-- new lookup -->"; |
69 |
} else { |
} else { |
70 |
response += "<!-- from memcached -->"; |
response += "<!-- from memcached -->"; |
71 |
} |
} |
72 |
return response; |
return response; |
73 |
} |
} |
74 |
|
|
75 |
|
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) { |
76 |
|
CommentCallback commentsCb = new CommentCallback(); |
77 |
|
commentsCb.setList( commentBeans ); |
78 |
|
|
79 |
|
session.setLocale( Locale.ROOT ); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
int start = 0; |
84 |
|
do { |
85 |
|
int count = 10; |
86 |
|
if (start > 0) |
87 |
|
count = Math.min(10, commentsCb.getEntryCount() ); |
88 |
|
|
89 |
|
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
90 |
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
91 |
|
.setAppId(appId) |
92 |
|
.setStartIndex(start) |
93 |
|
.setEntriesCount(count) |
94 |
|
.build(); |
95 |
|
|
96 |
|
session.append(commentsRequest, commentsCb); |
97 |
|
session.flush(); |
98 |
|
start +=10; |
99 |
|
|
100 |
|
if (start >= 200) |
101 |
|
break; //emergency brake |
102 |
|
|
103 |
|
} while ( start < commentsCb.getEntryCount() ); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
protected String doLookupWorker(String appId) { |
protected String doLookupWorker(String query) { |
108 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
109 |
|
|
110 |
|
|
113 |
|
|
114 |
|
|
115 |
AppsRequest appsRequest = AppsRequest.newBuilder() |
AppsRequest appsRequest = AppsRequest.newBuilder() |
116 |
.setAppId(appId) |
.setQuery(query) |
117 |
.setStartIndex(0).setEntriesCount(10) |
.setStartIndex(0).setEntriesCount(10) |
118 |
.setWithExtendedInfo(true) |
.setWithExtendedInfo(true) |
119 |
.build(); |
.build(); |
120 |
|
|
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
|
|
.setAppId(appId) |
|
|
.setStartIndex(0) |
|
|
.setEntriesCount(10) |
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"); |
|
121 |
|
|
122 |
sb.append("Downloads: " + app.getExtendedInfo().getDownloadsCountText() + " (" + app.getExtendedInfo().getDownloadsCount() + ")\n" ); |
AppsCallback appsCb = new AppsCallback() ; |
123 |
|
appsCb.setStringBuffer(sb); |
|
sb.append("\n"); |
|
|
|
|
|
} |
|
|
}; |
|
124 |
|
|
125 |
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
126 |
|
|
|
CommentCallback commentsCb = new CommentCallback(); |
|
|
commentsCb.setList( commentBeans ); |
|
127 |
|
|
|
commentsCb.setLocale("en"); |
|
|
session.setLocale( Locale.ENGLISH ); |
|
128 |
|
|
129 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
|
session.append(commentsRequest, commentsCb); |
|
130 |
session.flush(); |
session.flush(); |
131 |
|
|
132 |
commentsCb.setLocale( "da" ); |
String appId = appsCb.getAppId(); |
133 |
session.setLocale( new Locale("da") ); |
|
134 |
session.append(commentsRequest, commentsCb); |
if (appId != null) { |
135 |
session.flush(); |
|
136 |
|
loadComments(appId, session, commentBeans); |
137 |
Collections.sort(commentBeans); |
|
138 |
|
sb.append("-----------------------------------------------------------------\n"); |
139 |
sb.append("-----------------------------------------------------------------\n"); |
for (CommentBean c : commentBeans) { |
140 |
for (CommentBean c : commentBeans) { |
sb.append("User: " + c.author + "\n"); |
141 |
sb.append("User: " + c.author + " (" + c.locale + ")\n"); |
sb.append("Rating: " + c.rating + "\n"); |
142 |
sb.append("Rating: " + c.rating + "\n"); |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
143 |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
sb.append( c.text + "\n"); |
144 |
sb.append( c.text + "\n"); |
sb.append("\n"); |
145 |
sb.append("\n"); |
|
146 |
|
} |
147 |
|
sb.append("Comments: " + commentBeans.size() + "\n"); |
148 |
} |
} |
149 |
|
sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n"); |
150 |
|
|
151 |
|
|
152 |
return sb.toString(); |
return sb.toString(); |
153 |
} |
} |
154 |
|
|
155 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
156 |
String appId = request.getParameter("appId"); |
String query = request.getParameter("query"); |
157 |
|
|
158 |
response.setContentType("text/html"); |
response.setContentType("text/html"); |
159 |
PrintWriter out = response.getWriter(); |
PrintWriter out = response.getWriter(); |
160 |
out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" ); |
out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" ); |
161 |
} |
} |
162 |
|
|
163 |
} |
} |