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