OSDN Git Service

e53a5b9a1c9e31a65aab224838fe6cf89b166077
[coroid/inqubus.git] / frontend / src / saccubus / converter / filegetter / WebFileInstanciator.java
1 /* $Id$ */
2 package saccubus.converter.filegetter;
3
4 import java.io.IOException;
5 import java.text.Normalizer;
6 import org.apache.commons.lang.StringUtils;
7 import saccubus.ConvertStopFlag;
8 import saccubus.converter.profile.Proxy;
9 import saccubus.net.CommentInfo;
10 import saccubus.net.NicoClient;
11 import saccubus.net.NicoClientImpl;
12 import saccubus.net.VideoInfo;
13 import saccubus.util.WayBackTimeParser;
14
15 /**
16  * 動画ファイル, コメントファイルなど必要なファイルのうち, 1つでもダウンロード処理を必要とする場合のインスタンス化クラス.
17  * @author yuki
18  */
19 public class WebFileInstanciator extends FileInstanciator {
20
21     private final NicoClient client;
22     private final VideoInfo videoInfo;
23     private final CommentInfo commentInfo;
24
25     WebFileInstanciator(
26             ConvertStopFlag stopFlag,
27             InstanciationType videoType,
28             CommentInstanciationType commentType,
29             InstanciationType tcommType,
30             LoginInfo li,
31             String tag,
32             String time) throws IOException {
33         super(videoType, commentType, tcommType, tag);
34
35         if (li.getMail() == null || li.getPass() == null || li.getMail().equals("") || li.getPass().equals("")) {
36             throw new IllegalArgumentException("メールアドレスかパスワードが空白です。");
37         }
38
39         String host;
40         int port;
41         if (li.getProxy() != Proxy.NO_PROXY) {
42             host = li.getProxy().getHost();
43             port = li.getProxy().getPort();
44         } else {
45             host = null;
46             port = -1;
47         }
48         // TODO Implを直接newしている
49         client = new NicoClientImpl(li.getMail(), li.getPass(), stopFlag, host, port) {
50         };
51
52         if (!client.isLoggedIn()) {
53             throw new IOException("ログインに失敗");
54         }
55
56         try {
57             videoInfo = client.getVideoInfo(tag);
58             if (StringUtils.isNotBlank(time)) {
59                 System.out.print("Setting wayback time...");
60                 final String waybacktime = WayBackTimeParser.parse(time);
61                 String waybackkey = client.getWayBackKey(videoInfo);
62                 commentInfo = new CommentInfo(waybackkey, waybacktime);
63             }else{
64                 commentInfo = CommentInfo.DEFAULT;
65             }
66
67         } catch (IOException ex) {
68             throw new IOException(tag + "の情報の取得に失敗", ex);
69         }
70
71         if (videoType.isDoanload()) {
72             setVideoFileGetter(new VideoFileWebGetter(client, videoInfo));
73         }
74
75         if (commentType.isDoanload()) {
76             setCommentFileGetter(new CommentFileWebGetter(client, videoInfo, commentInfo, commentType.isAutoCommentNum(),
77                     commentType.getBackComment()));
78         }
79
80         if (tcommType.isDoanload()) {
81             setTcommFileGetter(new TcommFileWebGetter(client, videoInfo));
82         }
83     }
84
85     /**
86      * 動画のタイトルを取得する。
87      * 実際の動画タイトルはファイル名に用いることが出来ない文字を含んである場合があるため、ファイル名に適合するように編集した値が返る。
88      * @return
89      */
90     @Override
91     public String getVideoTitle() {
92         String name = videoInfo.getVideoTitle();
93         name = Normalizer.normalize(name, Normalizer.Form.NFKC);
94         name = name.replaceAll("[\\\\/:*?\"<>|.]", "_");
95         name = name.replace('\u2212', '\uff0d'); // - U+2212(MINUS SIGN) -> U+FF0D(FULLWIDTH HYPHEN-MINUS)
96         name = name.replace('\u301c', '\uff5e'); // ~ U+301C(WAVE DASH) -> U+FF5E(FULLWIDTH TILDE)
97         name = name.replace('\u223c', '\uff5e'); // ~ U+223C(TILDE OPERATOR) -> U+FF5E(FULLWIDTH TILDE)
98         name = name.replace('\u00a2', '\uffe0'); // ¢ U+00A2(CENT SIGN) -> U+FFE0(FULLWIDTH CENT SIGN)
99         name = name.replace('\u00a3', '\uffe1'); // £ U+00A3(POUND SIGN) -> U+FFE1(FULLWIDTH POUND SIGN)
100         name = name.replace('\u00ac', '\uffe2'); // ¬ U+00AC(NOT SIGN) -> U+FFE2(FULLWIDHT NOT SIGN)
101         return name;
102     }
103 }