OSDN Git Service

cd2f545c09e0724a6528c6d76e6660327c8e6cc3
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / thumbnail / Repository.java
1 package yukihane.inqubus.thumbnail;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 import javax.imageio.ImageIO;
12 import javax.swing.ImageIcon;
13 import nicobrowser.NicoHttpClient;
14 import nicobrowser.util.RssContent;
15 import nicobrowser.util.RssContentParser;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import saccubus.worker.profile.ProxyProfile;
19
20 /**
21  *
22  * @author yuki
23  */
24 public enum Repository {
25
26     INSTANCE;
27     private static final Logger logger = LoggerFactory.getLogger(Repository.class);
28     private final ExecutorService executorService = Executors.newCachedThreadPool();
29     private final Map<String, Thumbnail> thumbnailMap = new HashMap<>();
30
31     public void request(ProxyProfile proxy, String videoId) {
32         synchronized (thumbnailMap) {
33             final Thumbnail value = thumbnailMap.get(videoId);
34             if (value != null) {
35                 return;
36             }
37         }
38
39         executorService.execute(new ThumnailFetchWorker(proxy, videoId));
40     }
41
42     public Thumbnail getThumnail(String videoId) {
43         synchronized (thumbnailMap) {
44             return thumbnailMap.get(videoId);
45         }
46     }
47
48     private class ThumnailFetchWorker implements Runnable {
49
50         private final ProxyProfile proxy;
51         private final String videoId;
52
53         private ThumnailFetchWorker(ProxyProfile proxy, String videoId) {
54             this.proxy = proxy;
55             this.videoId = videoId;
56         }
57
58         @Override
59         public void run() {
60             work();
61         }
62
63         private void work() {
64             final NicoHttpClient client = createClient(proxy);
65
66             try (InputStream is = client.getThumbInfo(videoId)) {
67                 final RssContent cont = new RssContentParser().parse(is);
68                 final URL thumbUrl = new URL(cont.getThumbnail_url());
69
70                 ImageIcon icon;
71                 try (InputStream imageStream = client.get(thumbUrl)) {
72                     final BufferedImage image = ImageIO.read(imageStream);
73                     icon = new ImageIcon(image);
74                 }
75                 final Thumbnail thumb = new Thumbnail(cont.getTitle(), icon);
76
77                 synchronized (thumbnailMap) {
78                     thumbnailMap.put(videoId, thumb);
79                 }
80             } catch (IOException ex) {
81                 logger.error(null, ex);
82             }
83         }
84
85         private NicoHttpClient createClient(ProxyProfile proxy) {
86             if (proxy.use()) {
87                 return new NicoHttpClient(proxy.getHost(), proxy.getPort());
88             } else {
89                 return new NicoHttpClient();
90             }
91         }
92     }
93 }