OSDN Git Service

過去ログ要求時に指定するwhen値を内部的にはlongで保持するよう変更.
[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.VideoInfo;
12 import saccubus.util.WayBackTimeParser;
13 import yukihane.inqubus.saccubus_adapter.NicoBrowserAdapter;
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         client = new NicoBrowserAdapter(li.getMail(), li.getPass(), stopFlag, host, port) {
49         };
50
51         if (!client.isLoggedIn()) {
52             throw new IOException("ログインに失敗");
53         }
54
55         try {
56             videoInfo = client.getVideoInfo(tag);
57             if (StringUtils.isNotBlank(time)) {
58                 System.out.print("Setting wayback time...");
59                 final long waybacktime = WayBackTimeParser.parse(time);
60                 String waybackkey = client.getWayBackKey(videoInfo);
61                 commentInfo = new CommentInfo(waybackkey, waybacktime);
62             }else{
63                 commentInfo = null;
64             }
65
66         } catch (IOException ex) {
67             throw new IOException(tag + "の情報の取得に失敗", ex);
68         }
69
70         if (videoType.isDoanload()) {
71             setVideoFileGetter(new VideoFileWebGetter(client, videoInfo));
72         }
73
74         if (commentType.isDoanload()) {
75             setCommentFileGetter(new CommentFileWebGetter(client, videoInfo, commentInfo, commentType.isAutoCommentNum(),
76                     commentType.getBackComment()));
77         }
78
79         if (tcommType.isDoanload()) {
80             setTcommFileGetter(new TcommFileWebGetter(client, videoInfo));
81         }
82     }
83
84     /**
85      * 動画のタイトルを取得する。
86      * 実際の動画タイトルはファイル名に用いることが出来ない文字を含んである場合があるため、ファイル名に適合するように編集した値が返る。
87      * @return
88      */
89     @Override
90     public String getVideoTitle() {
91         String name = videoInfo.getVideoTitle();
92         name = Normalizer.normalize(name, Normalizer.Form.NFKC);
93         name = name.replaceAll("[\\\\/:*?\"<>|.]", "_");
94         name = name.replace('\u2212', '\uff0d'); // - U+2212(MINUS SIGN) -> U+FF0D(FULLWIDTH HYPHEN-MINUS)
95         name = name.replace('\u301c', '\uff5e'); // ~ U+301C(WAVE DASH) -> U+FF5E(FULLWIDTH TILDE)
96         name = name.replace('\u223c', '\uff5e'); // ~ U+223C(TILDE OPERATOR) -> U+FF5E(FULLWIDTH TILDE)
97         name = name.replace('\u00a2', '\uffe0'); // ¢ U+00A2(CENT SIGN) -> U+FFE0(FULLWIDTH CENT SIGN)
98         name = name.replace('\u00a3', '\uffe1'); // £ U+00A3(POUND SIGN) -> U+FFE1(FULLWIDTH POUND SIGN)
99         name = name.replace('\u00ac', '\uffe2'); // ¬ U+00AC(NOT SIGN) -> U+FFE2(FULLWIDHT NOT SIGN)
100         return name;
101     }
102 }