OSDN Git Service

Thumnail取得用処理
authoryukihane <yukihane.feather@gmail.com>
Sun, 18 Sep 2011 06:08:47 +0000 (15:08 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sun, 18 Sep 2011 06:08:47 +0000 (15:08 +0900)
frontend/src/yukihane/inqubus/thumbnail/Repository.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/thumbnail/Thumbnail.java [new file with mode: 0644]

diff --git a/frontend/src/yukihane/inqubus/thumbnail/Repository.java b/frontend/src/yukihane/inqubus/thumbnail/Repository.java
new file mode 100644 (file)
index 0000000..cd2f545
--- /dev/null
@@ -0,0 +1,93 @@
+package yukihane.inqubus.thumbnail;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import javax.imageio.ImageIO;
+import javax.swing.ImageIcon;
+import nicobrowser.NicoHttpClient;
+import nicobrowser.util.RssContent;
+import nicobrowser.util.RssContentParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import saccubus.worker.profile.ProxyProfile;
+
+/**
+ *
+ * @author yuki
+ */
+public enum Repository {
+
+    INSTANCE;
+    private static final Logger logger = LoggerFactory.getLogger(Repository.class);
+    private final ExecutorService executorService = Executors.newCachedThreadPool();
+    private final Map<String, Thumbnail> thumbnailMap = new HashMap<>();
+
+    public void request(ProxyProfile proxy, String videoId) {
+        synchronized (thumbnailMap) {
+            final Thumbnail value = thumbnailMap.get(videoId);
+            if (value != null) {
+                return;
+            }
+        }
+
+        executorService.execute(new ThumnailFetchWorker(proxy, videoId));
+    }
+
+    public Thumbnail getThumnail(String videoId) {
+        synchronized (thumbnailMap) {
+            return thumbnailMap.get(videoId);
+        }
+    }
+
+    private class ThumnailFetchWorker implements Runnable {
+
+        private final ProxyProfile proxy;
+        private final String videoId;
+
+        private ThumnailFetchWorker(ProxyProfile proxy, String videoId) {
+            this.proxy = proxy;
+            this.videoId = videoId;
+        }
+
+        @Override
+        public void run() {
+            work();
+        }
+
+        private void work() {
+            final NicoHttpClient client = createClient(proxy);
+
+            try (InputStream is = client.getThumbInfo(videoId)) {
+                final RssContent cont = new RssContentParser().parse(is);
+                final URL thumbUrl = new URL(cont.getThumbnail_url());
+
+                ImageIcon icon;
+                try (InputStream imageStream = client.get(thumbUrl)) {
+                    final BufferedImage image = ImageIO.read(imageStream);
+                    icon = new ImageIcon(image);
+                }
+                final Thumbnail thumb = new Thumbnail(cont.getTitle(), icon);
+
+                synchronized (thumbnailMap) {
+                    thumbnailMap.put(videoId, thumb);
+                }
+            } catch (IOException ex) {
+                logger.error(null, ex);
+            }
+        }
+
+        private NicoHttpClient createClient(ProxyProfile proxy) {
+            if (proxy.use()) {
+                return new NicoHttpClient(proxy.getHost(), proxy.getPort());
+            } else {
+                return new NicoHttpClient();
+            }
+        }
+    }
+}
diff --git a/frontend/src/yukihane/inqubus/thumbnail/Thumbnail.java b/frontend/src/yukihane/inqubus/thumbnail/Thumbnail.java
new file mode 100644 (file)
index 0000000..707bdfd
--- /dev/null
@@ -0,0 +1,26 @@
+package yukihane.inqubus.thumbnail;
+
+import javax.swing.ImageIcon;
+
+/**
+ *
+ * @author yuki
+ */
+public class Thumbnail {
+
+    private final String title;
+    private final ImageIcon image;
+
+    public Thumbnail(String title, ImageIcon image) {
+        this.title = title;
+        this.image = image;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public ImageIcon getImage() {
+        return image;
+    }
+}