/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Diff of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 659 by torben, Fri Apr 23 12:31:41 2010 UTC revision 667 by torben, Sun Apr 25 20:53:06 2010 UTC
# Line 3  package dk.thoerup.marketstats; Line 3  package dk.thoerup.marketstats;
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.Collections;
8  import java.util.Date;  import java.util.Date;
 import java.util.Formatter;  
 import java.util.List;  
9  import java.util.Locale;  import java.util.Locale;
10  import java.util.logging.Logger;  import java.util.logging.Logger;
11    
# Line 17  import javax.servlet.http.HttpServletRes Line 17  import javax.servlet.http.HttpServletRes
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;          final int TIMEOUT = 30*60;
29          static final Logger log = Logger.getLogger(ShowStats.class.getName());          static final Logger log = Logger.getLogger(ShowStats.class.getName());
30    
31          String login;          String login;
32          String password;          String password;
33                    
34                    MemcachedClient memcache = null;
35    
36    
37          @Override          @Override
# Line 45  public class ShowStats extends HttpServl Line 40  public class ShowStats extends HttpServl
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 appId) throws IOException {
                 MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));  
                 c.flush();  
52                                    
53    
54    
55                  String key = "marketstats:" + appId;                  String key = "marketstats:" + appId;
56                  String response = (String) c.get(key);                  String response = (String) memcache.get(key);
57    
58                  if (response == null) {                  if (response == null) {
59                          response = doLookupWorker(appId);                          response = doLookupWorker(appId);
60                          c.set(key, TIMEOUT, response);                          memcache.set(key, TIMEOUT, response);
61                          response += "<!-- new lookup -->";                          response += "<!-- new lookup -->";
62                  } else {                  } else {
63                          response += "<!-- from memcached -->";                          response += "<!-- from memcached -->";
64                  }                  }
65                  return response;                  return response;
66          }          }
67            
68            private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) {            
69                    CommentCallback commentsCb = new CommentCallback();
70                    commentsCb.setList( commentBeans );
71    
72                    commentsCb.setLocale( locale );
73                    session.setLocale( new Locale(locale) );
74                    
75                    
76                    int start = 0;
77                    do {
78                            int count = 10;
79                            if (start > 0)                          
80                                    count = Math.min(10, commentsCb.getEntryCount() );
81                            
82                            //log.warning("count=" + count + " start=" + start + " " + locale);
83                            CommentsRequest commentsRequest = CommentsRequest.newBuilder()
84                            .setAppId(appId)
85                            .setStartIndex(start)
86                            .setEntriesCount(count)
87                            .build();
88                            
89                            session.append(commentsRequest, commentsCb);
90                            session.flush();
91                            start +=10;
92                            
93                            if (start >= 20)
94                                    break; //emergency brake
95                            
96                    } while ( start < commentsCb.getEntryCount() );
97            }
98    
99    
100          protected String doLookupWorker(String appId) {          protected String doLookupWorker(String appId) {
# Line 72  public class ShowStats extends HttpServl Line 103  public class ShowStats extends HttpServl
103    
104                  MarketSession session = new MarketSession();                  MarketSession session = new MarketSession();
105                  session.login(login,password);                  session.login(login,password);
106                  session.setLocale( Locale.ENGLISH );  
107    
108                  AppsRequest appsRequest = AppsRequest.newBuilder()                  AppsRequest appsRequest = AppsRequest.newBuilder()
109                  .setAppId(appId)                  .setAppId(appId)
# Line 80  public class ShowStats extends HttpServl Line 111  public class ShowStats extends HttpServl
111                  .setWithExtendedInfo(true)                  .setWithExtendedInfo(true)
112                  .build();                  .build();
113    
                 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");  
114    
115                                  sb.append("Rating: "  );                  AppsCallback appsCb = new AppsCallback() ;
116                                  double rating = Double.parseDouble( app.getRating() );                  appsCb.setStringBuffer(sb);
                                 form.format("%.4f", rating);  
                                 sb.append("\n");  
117    
118                                  sb.append("Downloads: " + app.getExtendedInfo().getDownloadsCountText() + "(" + app.getExtendedInfo().getDownloadsCount() + ")\n" );                  ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
119    
                                 sb.append("\n");  
120    
                         }  
                 };  
   
                 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");  
121    
122                    session.append(appsRequest, appsCb);
123                    session.flush();
124    
125                                  List<Comment> cl = response.getCommentsList();                  loadComments(appId, session, "da", commentBeans);
126                                  for (Comment c : cl) {                  loadComments(appId, session, "en", commentBeans);
                                         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() );  
127    
128                    Collections.sort(commentBeans);
129    
130                    sb.append("-----------------------------------------------------------------\n");
131                    for (CommentBean c : commentBeans) {
132                            sb.append("User: " + c.author + " ("  + c.locale + ")\n");
133                            sb.append("Rating: " + c.rating + "\n");
134                            sb.append("Time: " + new Date(c.time).toString() + "\n");
135                            sb.append( c.text + "\n");
136                            sb.append("\n");
137    
138                          }                  }
139                    sb.append("Comments: " + commentBeans.size() );
140    
                 };  
                 session.append(appsRequest, appsCb);  
                 session.append(commentsRequest, commentsCb);  
                 session.flush();  
                 session.setLocale( new Locale("da") );  
                 session.append(commentsRequest, commentsCb);  
                 session.flush();  
141    
142                  return sb.toString();                  return sb.toString();
143          }          }
144    
145          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
146                  String appId = request.getParameter("appId");                  String appId = request.getParameter("appId");
                 PrintWriter out = response.getWriter();  
147    
148                  out.print( "<pre>" + doLookup(appId) + "</pre>" );                  response.setContentType("text/html");
149                    PrintWriter out = response.getWriter();
150                    out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" );
151          }          }
152    
153  }  }

Legend:
Removed from v.659  
changed lines
  Added in v.667

  ViewVC Help
Powered by ViewVC 1.1.20