Skip to content
Browse files

Add mix method under /video/random/{count}

  • Loading branch information...
1 parent 47252a4 commit 0925e46e5eec21772ec6f23ec2346185c7bd6eda Rene Treffer committed Mar 23, 2012
View
9 src/main/java/com/hackanoon/youshake/backend/YoutubeTest.java
@@ -4,20 +4,21 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.codehaus.jettison.json.JSONArray;
import com.hackanoon.youshake.youtube.Top;
-@Path("/youtube")
+@Path("/video")
public class YoutubeTest {
@GET
- @Path("top")
+ @Path("random/{count}")
@Produces("application/json")
- public JSONArray top() {
- return new JSONArray(Arrays.asList(Top.getTopRated()));
+ public JSONArray mix(@PathParam("count") int count) {
+ return new JSONArray(Arrays.asList(Top.mix(count)));
}
}
View
90 src/main/java/com/hackanoon/youshake/youtube/Top.java
@@ -1,9 +1,9 @@
package com.hackanoon.youshake.youtube;
import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Random;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -70,7 +70,88 @@
return getRSSUrls(YOUTUBE_BASE_URL + "on_the_web");
}
+ public static String[] mix(int count) {
+ if (count > 250) {
+ count = 250;
+ }
+ Random rnd = new Random();
+ String[] result = new String[count];
+ int i = 0;
+ int round = 0;
+ while (i < count && round < count * 3) {
+ round++;
+ double d = rnd.nextDouble();
+ if (d < 0.1) {
+ int c = (int)Math.min(d * 80, 7);
+ String url = null;
+ switch(c) {
+ case 0: url = fromArray(getTopRated(), rnd, result); break;
+ case 1: url = fromArray(getTopFavorite(), rnd, result); break;
+ case 2: url = fromArray(getMostViewed(), rnd, result); break;
+ case 3: url = fromArray(getMostShared(), rnd, result); break;
+ case 4: url = fromArray(getMostPopular(), rnd, result); break;
+ case 5: url = fromArray(getMostDiscussed(), rnd, result); break;
+ case 6: url = fromArray(getMostResponded(), rnd, result); break;
+ case 7: url = fromArray(getMostFeatured(), rnd, result); break;
+ }
+ if (url != null) {
+ result[i++] = url;
+ }
+ continue;
+ }
+ if (d < 0.2) {
+ String url = fromArray(getMostFeatured(), rnd, result);
+ if (url != null) {
+ result[i++] = url;
+ }
+ continue;
+ }
+ String url = null;
+ if (rnd.nextBoolean()) {
+ url = fromArray(getMostRecent(), rnd, result);
+ } else {
+ url = fromArray(getOnTheWeb(), rnd, result);
+ }
+ if (url != null) {
+ result[i++] = url;
+ }
+ }
+ ArrayList<String> res = new ArrayList<String>();
+ for (String s: result) {
+ if (s != null) {
+ res.add(s);
+ }
+ }
+ return res.toArray(new String[res.size()]);
+ }
+
+ private static String fromArray(String src[], Random rnd, String[] blacklist) {
+ if (src.length == 0) {
+ return null;
+ }
+ String url = src[rnd.nextInt(src.length)];
+ for (String entry: blacklist) {
+ if (entry != null && entry.equals(url)) {
+ return null;
+ }
+ }
+ return url;
+ }
+
+ private static class TEntry {
+ public String[] result;
+ public long time = System.currentTimeMillis();
+ }
+
+ private static HashMap<String, TEntry> CALL_CACHE =
+ new HashMap<String, Top.TEntry>();
+
private final static String[] getRSSUrls(String url) {
+ long time = System.currentTimeMillis();
+ TEntry cached = CALL_CACHE.get(url);
+ if (cached != null && time - cached.time < 30 * 60 * 1000) {
+ return cached.result;
+ }
ArrayList<String> result = new ArrayList<String>();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -104,7 +185,10 @@
} catch (SAXException e) {
e.printStackTrace();
}
- return result.toArray(new String[result.size()]);
+ cached = new TEntry();
+ cached.result = result.toArray(new String[result.size()]);
+ CALL_CACHE.put(url, cached);
+ return cached.result;
}
}

0 comments on commit 0925e46

Please sign in to comment.
Something went wrong with that request. Please try again.