OSDN Git Service

テーブルのツールチップにサムネイル情報を表示
[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 class Repository {
25
26     private static final Logger logger = LoggerFactory.getLogger(Repository.class);
27     private final ExecutorService executorService = Executors.newCachedThreadPool();
28     private final Map<String, Thumbnail> thumbnailMap = new HashMap<>();
29
30     public void request(ProxyProfile proxy, String videoId) {
31         synchronized (thumbnailMap) {
32             final Thumbnail value = thumbnailMap.get(videoId);
33             if (value != null) {
34                 return;
35             }
36         }
37
38         executorService.execute(new ThumnailFetchWorker(proxy, videoId));
39     }
40
41     public Thumbnail getThumnail(String videoId) {
42         synchronized (thumbnailMap) {
43             return thumbnailMap.get(videoId);
44         }
45     }
46
47     private class ThumnailFetchWorker implements Runnable {
48
49         private final ProxyProfile proxy;
50         private final String videoId;
51
52         private ThumnailFetchWorker(ProxyProfile proxy, String videoId) {
53             this.proxy = proxy;
54             this.videoId = videoId;
55         }
56
57         @Override
58         public void run() {
59             work();
60         }
61
62         private void work() {
63             final NicoHttpClient client = createClient(proxy);
64
65             try (InputStream is = client.getThumbInfo(videoId)) {
66                 final RssContent cont = new RssContentParser().parse(is);
67                 final URL thumbUrl = new URL(cont.getThumbnail_url());
68
69                 ImageIcon icon;
70                 try (InputStream imageStream = client.get(thumbUrl)) {
71                     final BufferedImage image = ImageIO.read(imageStream);
72                     icon = new ImageIcon(image);
73                 }
74                 final Thumbnail thumb = new Thumbnail(cont.getTitle(), icon);
75
76                 synchronized (thumbnailMap) {
77                     thumbnailMap.put(videoId, thumb);
78                 }
79             } catch (IOException ex) {
80                 logger.error(null, ex);
81             }
82         }
83
84         private NicoHttpClient createClient(ProxyProfile proxy) {
85             if (proxy.use()) {
86                 return new NicoHttpClient(proxy.getHost(), proxy.getPort());
87             } else {
88                 return new NicoHttpClient();
89             }
90         }
91     }
92 }