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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20