--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/04/23 14:13:39 662 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/05/27 09:45:09 759 @@ -6,7 +6,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.Formatter; import java.util.Locale; import java.util.logging.Logger; @@ -18,12 +17,9 @@ import net.spy.memcached.MemcachedClient; import com.gc.android.market.api.MarketSession; -import com.gc.android.market.api.MarketSession.Callback; -import com.gc.android.market.api.model.Market.App; 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.CommentsRequest; -import com.gc.android.market.api.model.Market.ResponseContext; + public class ShowStats extends HttpServlet { @@ -34,6 +30,8 @@ String login; String password; + + MemcachedClient memcache = null; @Override @@ -42,27 +40,63 @@ login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); + + try { + memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + } catch (IOException e) { + throw new ServletException(e); + } } - protected String doLookup(String appId) throws IOException { - MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + protected String doLookup(String query) throws IOException { + - - String key = "marketstats:" + appId; - String response = (String) c.get(key); + String key = "marketstats:" + query.replace(' ', '_'); + String response = (String) memcache.get(key); if (response == null) { - response = doLookupWorker(appId); - c.set(key, TIMEOUT, response); + response = doLookupWorker(query); + memcache.set(key, TIMEOUT, response); response += ""; } else { response += ""; } return response; } + + private void loadComments(String appId, MarketSession session, ArrayList commentBeans) { + CommentCallback commentsCb = new CommentCallback(); + commentsCb.setList( commentBeans ); + + session.setLocale( Locale.ROOT ); + + + + int start = 0; + do { + int count = 10; + if (start > 0) + count = Math.min(10, commentsCb.getEntryCount() ); + + //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); + CommentsRequest commentsRequest = CommentsRequest.newBuilder() + .setAppId(appId) + .setStartIndex(start) + .setEntriesCount(count) + .build(); + + session.append(commentsRequest, commentsCb); + session.flush(); + start +=10; + + if (start >= 200) + break; //emergency brake + + } while ( start < commentsCb.getEntryCount() ); + } - protected String doLookupWorker(String appId) { + protected String doLookupWorker(String query) { final StringBuilder sb = new StringBuilder(); @@ -71,83 +105,51 @@ AppsRequest appsRequest = AppsRequest.newBuilder() - .setAppId(appId) + .setQuery(query) .setStartIndex(0).setEntriesCount(10) .setWithExtendedInfo(true) .build(); - CommentsRequest commentsRequest = CommentsRequest.newBuilder() - .setAppId(appId) - .setStartIndex(0) - .setEntriesCount(10) - .build(); - - - - - Callback appsCb = new Callback() { - @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( "

" + app.getTitle() + "

"); - 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"); - } - }; + AppsCallback appsCb = new AppsCallback() ; + appsCb.setStringBuffer(sb); ArrayList commentBeans = new ArrayList(); - CommentCallback commentsCb = new CommentCallback(); - commentsCb.setList( commentBeans ); - commentsCb.setLocale("en"); - session.setLocale( Locale.ENGLISH ); session.append(appsRequest, appsCb); - session.append(commentsRequest, commentsCb); session.flush(); - - commentsCb.setLocale( "da" ); - session.setLocale( new Locale("da") ); - session.append(commentsRequest, commentsCb); - session.flush(); - - Collections.sort(commentBeans); - - sb.append("-----------------------------------------------------------------\n"); - for (CommentBean c : commentBeans) { - sb.append("User: " + c.author + " (" + c.locale + ")\n"); - sb.append("Rating: " + c.rating + "\n"); - sb.append("Time: " + new Date(c.time).toString() + "\n"); - sb.append( c.text + "\n"); - sb.append("\n"); - + + String appId = appsCb.getAppId(); + + if (appId != null) { + + loadComments(appId, session, commentBeans); + + sb.append("-----------------------------------------------------------------\n"); + for (CommentBean c : commentBeans) { + sb.append("User: " + c.author + "\n"); + sb.append("Rating: " + c.rating + "\n"); + sb.append("Time: " + new Date(c.time).toString() + "\n"); + sb.append( c.text + "\n"); + sb.append("\n"); + + } + sb.append("Comments: " + commentBeans.size() + "\n"); } + sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes"); return sb.toString(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String appId = request.getParameter("appId"); + String query = request.getParameter("query"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(appId) + "
" ); + out.print( "
" + doLookup(query) + "
" ); } }