6 |
import java.util.ArrayList; |
import java.util.ArrayList; |
7 |
import java.util.Collections; |
import java.util.Collections; |
8 |
import java.util.Date; |
import java.util.Date; |
|
import java.util.Formatter; |
|
9 |
import java.util.Locale; |
import java.util.Locale; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
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 { |
protected String doLookup(String query) throws IOException { |
52 |
MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
|
53 |
|
|
54 |
|
String key = "marketstats:" + query; |
55 |
String key = "marketstats:" + appId; |
String response = (String) memcache.get(key); |
|
String response = (String) c.get(key); |
|
56 |
|
|
57 |
if (response == null) { |
if (response == null) { |
58 |
response = doLookupWorker(appId); |
response = doLookupWorker(query); |
59 |
c.set(key, TIMEOUT, response); |
memcache.set(key, TIMEOUT, response); |
60 |
response += "<!-- new lookup -->"; |
response += "<!-- new lookup -->"; |
61 |
} else { |
} else { |
62 |
response += "<!-- from memcached -->"; |
response += "<!-- from memcached -->"; |
63 |
} |
} |
64 |
return response; |
return response; |
65 |
} |
} |
66 |
|
|
67 |
|
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) { |
68 |
|
CommentCallback commentsCb = new CommentCallback(); |
69 |
|
commentsCb.setList( commentBeans ); |
70 |
|
|
71 |
|
session.setLocale( Locale.ROOT ); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
int start = 0; |
76 |
|
do { |
77 |
|
int count = 10; |
78 |
|
if (start > 0) |
79 |
|
count = Math.min(10, commentsCb.getEntryCount() ); |
80 |
|
|
81 |
|
//log.warning("count=" + count + " start=" + start ); |
82 |
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
83 |
|
.setAppId(appId) |
84 |
|
.setStartIndex(start) |
85 |
|
.setEntriesCount(count) |
86 |
|
.build(); |
87 |
|
|
88 |
|
session.append(commentsRequest, commentsCb); |
89 |
|
session.flush(); |
90 |
|
start +=10; |
91 |
|
|
92 |
|
if (start >= 50) |
93 |
|
break; //emergency brake |
94 |
|
|
95 |
|
} while ( start < commentsCb.getEntryCount() ); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
protected String doLookupWorker(String appId) { |
protected String doLookupWorker(String query) { |
100 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
101 |
|
|
102 |
|
|
105 |
|
|
106 |
|
|
107 |
AppsRequest appsRequest = AppsRequest.newBuilder() |
AppsRequest appsRequest = AppsRequest.newBuilder() |
108 |
.setAppId(appId) |
.setQuery(query) |
109 |
.setStartIndex(0).setEntriesCount(10) |
.setStartIndex(0).setEntriesCount(10) |
110 |
.setWithExtendedInfo(true) |
.setWithExtendedInfo(true) |
111 |
.build(); |
.build(); |
112 |
|
|
|
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"); |
|
|
|
|
|
sb.append("Downloads: " + app.getExtendedInfo().getDownloadsCountText() + " (" + app.getExtendedInfo().getDownloadsCount() + ")\n" ); |
|
|
|
|
|
sb.append("\n"); |
|
113 |
|
|
114 |
} |
AppsCallback appsCb = new AppsCallback() ; |
115 |
}; |
appsCb.setStringBuffer(sb); |
116 |
|
|
117 |
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
118 |
|
|
|
CommentCallback commentsCb = new CommentCallback(); |
|
|
commentsCb.setList( commentBeans ); |
|
119 |
|
|
|
commentsCb.setLocale("en"); |
|
|
session.setLocale( Locale.ENGLISH ); |
|
120 |
|
|
121 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
|
session.append(commentsRequest, commentsCb); |
|
122 |
session.flush(); |
session.flush(); |
123 |
|
|
124 |
commentsCb.setLocale( "da" ); |
String appId = appsCb.getAppId(); |
125 |
session.setLocale( new Locale("da") ); |
|
126 |
session.append(commentsRequest, commentsCb); |
if (appId != null) { |
127 |
session.flush(); |
|
128 |
|
loadComments(appId, session, commentBeans); |
129 |
Collections.sort(commentBeans); |
|
130 |
|
sb.append("-----------------------------------------------------------------\n"); |
131 |
sb.append("-----------------------------------------------------------------\n"); |
for (CommentBean c : commentBeans) { |
132 |
for (CommentBean c : commentBeans) { |
sb.append("User: " + c.author + "\n"); |
133 |
sb.append("User: " + c.author + " (" + c.locale + ")\n"); |
sb.append("Rating: " + c.rating + "\n"); |
134 |
sb.append("Rating: " + c.rating + "\n"); |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
135 |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
sb.append( c.text + "\n"); |
136 |
sb.append( c.text + "\n"); |
sb.append("\n"); |
137 |
sb.append("\n"); |
|
138 |
|
} |
139 |
|
sb.append("Comments: " + commentBeans.size() + "\n"); |
140 |
} |
} |
141 |
|
sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes"); |
142 |
|
|
143 |
|
|
144 |
return sb.toString(); |
return sb.toString(); |
145 |
} |
} |
146 |
|
|
147 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
148 |
String appId = request.getParameter("appId"); |
String query = request.getParameter("query"); |
149 |
|
|
150 |
response.setContentType("text/html"); |
response.setContentType("text/html"); |
151 |
PrintWriter out = response.getWriter(); |
PrintWriter out = response.getWriter(); |
152 |
out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" ); |
out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" ); |
153 |
} |
} |
154 |
|
|
155 |
} |
} |