OSDN Git Service

動画ダウンロード中のキャンセル処理を実装
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / saccubus_adapter / NicoBrowserAdapter.java
1 package yukihane.inqubus.saccubus_adapter;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
7 import nicobrowser.GetFlvResult;
8 import nicobrowser.NicoHttpClient;
9 import nicobrowser.ProgressListener;
10 import saccubus.ConvertStopFlag;
11 import saccubus.net.CommentInfo;
12 import saccubus.net.NicoClient;
13 import saccubus.net.TextProgressListener;
14 import saccubus.net.VideoInfo;
15 import saccubus.util.FileUtil;
16
17 /**
18  *
19  * @author yuki
20  */
21 public class NicoBrowserAdapter implements NicoClient {
22
23     private static final Logger logger = Logger.getLogger(NicoBrowserAdapter.class.getName());
24     private final NicoHttpClient client;
25     private final boolean hasLogin;
26     private final ConvertStopFlag stopFlag;
27     private nicobrowser.VideoInfo nicoBrowserVi;
28
29     public NicoBrowserAdapter(final String mail, final String pass,
30             final ConvertStopFlag flag, final String proxyHost, final int proxyPort) {
31
32         if (proxyHost != null && proxyHost.length() > 0 && proxyPort >= 0 && proxyPort <= 65535) {
33             this.client = new NicoHttpClient(proxyHost, proxyPort);
34         } else {
35             this.client = new NicoHttpClient();
36         }
37
38         // ログイン
39         boolean loginned = false;
40         try {
41             loginned = client.login(mail, pass);
42         } catch (Exception ex) {
43             logger.log(Level.SEVERE, "ログイン処理時に例外発生", ex);
44         }
45         this.hasLogin = loginned;
46
47         this.stopFlag = flag;
48
49     }
50
51     @Override
52     public VideoInfo getVideoInfo(String videoId) throws IOException {
53         nicoBrowserVi = client.getVideoInfo(videoId);
54         final nicobrowser.VideoInfo vi = nicoBrowserVi;
55
56         final VideoInfo.OfficialOption oo =
57                 new VideoInfo.OfficialOption(vi.getKeyMap().get("threadkey"), vi.getKeyMap().get("force_184"));
58
59         return new VideoInfo(FileUtil.safeFileName(vi.getTitleInWatchPage()), vi.getThreadId(), vi.getVideoUrl().
60                 toString(), vi.getMessageUrl().toString(), vi.getUserId(), vi.getVideoLength(), oo);
61     }
62
63     @Override
64     public String getWayBackKey(VideoInfo vi) throws IOException {
65         return client.getWayBackKey(nicoBrowserVi);
66     }
67
68     @Override
69     public File getComment(VideoInfo videoInfo, CommentInfo commentInfo, File file, TextProgressListener listener,
70             String com) {
71         try {
72             // TODO CommentInfo を使用するようにしなければ
73             return client.getCommentFile(nicoBrowserVi, file.getPath());
74         } catch (Exception ex) {
75             logger.log(Level.SEVERE, "コメント取得失敗", ex);
76         }
77         // TODO 失敗した場合何を返す?
78         return null;
79     }
80
81     @Override
82     public File getTcomment(VideoInfo videoInfo, File file, TextProgressListener listener) {
83         try {
84             return client.getTCommentFile(nicoBrowserVi, file.getParent());
85         } catch (Exception ex) {
86             logger.log(Level.SEVERE, "投稿者コメント取得失敗", ex);
87         }
88         // TODO 失敗した場合何を返す?
89         return null;
90     }
91
92     @Override
93     public File getVideo(final VideoInfo videoInfo, File file, final TextProgressListener listener) {
94         try {
95             GetFlvResult res = client.getFlvFile(nicoBrowserVi, file.getPath(), new ProgressListener() {
96
97                 @Override
98                 public void progress(long fileSize, long downloadSize) {
99                     final double p = ((double) downloadSize / fileSize) * 100.0;
100                     final String msg = "動画ダウンロード:" + p + "パーセント完了";
101                     listener.setText(msg);
102                 }
103
104                 @Override
105                 public boolean getCancel() {
106                     return stopFlag.needStop();
107                 }
108             });
109
110             return res.getFile();
111         } catch (Exception ex) {
112             logger.log(Level.SEVERE, null, ex);
113         }
114         // TODO 失敗した場合何を返す?
115         return null;
116     }
117
118     @Override
119     public boolean isLoggedIn() {
120         return hasLogin;
121     }
122 }