package dk.thoerup.marketstats; import java.io.IOException; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Locale; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.spy.memcached.MemcachedClient; import com.gc.android.market.api.MarketSession; import com.gc.android.market.api.model.Market.AppsRequest; import com.gc.android.market.api.model.Market.CommentsRequest; public class ShowStats extends HttpServlet { private static final long serialVersionUID = 1L; final int TIMEOUT = 30*60; static final Logger log = Logger.getLogger(ShowStats.class.getName()); String login; String password; @Override public void init() throws ServletException { super.init(); login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); } protected String doLookup(String appId) throws IOException { MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); String key = "marketstats:" + appId; String response = (String) c.get(key); if (response == null) { response = doLookupWorker(appId); c.set(key, TIMEOUT, response); response += ""; } else { response += ""; } return response; } private void loadComments(String appId, MarketSession session, String locale, ArrayList commentBeans) { CommentCallback commentsCb = new CommentCallback(); commentsCb.setList( commentBeans ); commentsCb.setLocale( locale ); session.setLocale( new Locale(locale) ); int start = 0; do { int count = 10; if (start > 0) count = Math.min(10, commentsCb.getEntryCount() ); //log.warning("count=" + count + " start=" + start + " " + locale); CommentsRequest commentsRequest = CommentsRequest.newBuilder() .setAppId(appId) .setStartIndex(start) .setEntriesCount(count) .build(); session.append(commentsRequest, commentsCb); session.flush(); start +=10; if (start >= 20) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); } protected String doLookupWorker(String appId) { final StringBuilder sb = new StringBuilder(); MarketSession session = new MarketSession(); session.login(login,password); AppsRequest appsRequest = AppsRequest.newBuilder() .setAppId(appId) .setStartIndex(0).setEntriesCount(10) .setWithExtendedInfo(true) .build(); AppsCallback appsCb = new AppsCallback() ; appsCb.setStringBuffer(sb); ArrayList commentBeans = new ArrayList(); session.append(appsRequest, appsCb); session.flush(); loadComments(appId, session, "da", commentBeans); loadComments(appId, session, "en", commentBeans); 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"); } sb.append("Comments: " + commentBeans.size() ); return sb.toString(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String appId = request.getParameter("appId"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print( "
" + doLookup(appId) + "
" ); } }