OSDN Git Service

LoginInfoのinterface化
[coroid/inqubus.git] / frontend / src / saccubus / prompt / Prompt.java
index 052c0d6..2aec63a 100644 (file)
 package saccubus.prompt;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
 import saccubus.ConvertStopFlag;
 import saccubus.ConvertStopFlag.State;
 import saccubus.converter.Converter;
+import saccubus.converter.classic.profile.LoginInfo;
+import saccubus.converter.classic.profile.CommentGetInfo;
+import saccubus.converter.classic.profile.Ffmpeg;
+import saccubus.converter.classic.profile.FfmpegOption;
+import saccubus.converter.classic.profile.GeneralSetting;
+import saccubus.converter.classic.profile.InputFileSetting;
+import saccubus.converter.classic.profile.OutputFileSetting;
+import saccubus.converter.classic.profile.Profile;
+import saccubus.converter.classic.profile.SFile;
 import saccubus.properties.SProperties;
 import saccubus.net.TextProgressListener;
 
 /**
  * <p>
- * \83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·
+ * タイトル: さきゅばす
  * </p>
- * 
+ *
  * <p>
- * \90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91
+ * 説明: ニコニコ動画の動画をコメントつきで保存
  * </p>
- * 
+ *
  * <p>
- * \92\98\8dì\8c : Copyright (c) 2007 PSI
+ * 著作権: Copyright (c) 2007 PSI
  * </p>
- * 
+ *
  * <p>
- * \89ï\8eÐ\96¼:
+ * 会社名:
  * </p>
- * 
- * @author \96¢\93ü\97Í
+ *
+ * @author 未入力
  * @version 1.0
  */
 public class Prompt {
-       public static void main(String[] args) {
-        TextProgressListener sl = new TextProgressListener() {
 
-            public void setText(String text) {
-            }
-        };
-        ConvertStopFlag.StateChangeListener scl = new ConvertStopFlag.StateChangeListener() {
+    public static void main(String[] args) throws IOException {
+        // 第1引数がメールアドレスと思しき時は昔の引数であるとみなしてパース、実行する.
+        if (args.length > 0 && args[0].contains("@")) {
+            doWithOldArguments(args);
+            return;
+        }
+
+        new Prompt().execute(args);
+    }
+
+    public void execute(String[] args) throws IOException {
+        execute(args, TextProgressListener.EMPTY_LISTENER, ConvertStopFlag.StateChangeListener.EMPTY_LISTENER);
+    }
 
-            public void changeState(State s) {
+    public void execute(String[] args, TextProgressListener tpl, ConvertStopFlag.StateChangeListener scl) throws
+            IOException {
+        Options options = createOptions(args);
+        Profile profile;
+        try {
+            CommandLineParser parser = new BasicParser();
+            CommandLine cmd = parser.parse(options, args);
+            profile = createExecuteOption(cmd);
+        } catch (Exception e) {
+            HelpFormatter hf = new HelpFormatter();
+            hf.printHelp("java -jar Saccubus.jar [opts]", options);
+            System.out.flush();
+            throw new IOException(e);
+        }
+
+        Converter conv = new Converter("dummy", "", profile, tpl, new ConvertStopFlag(scl));
+        ExecutorService es = Executors.newSingleThreadExecutor();
+        Future<Boolean> future = es.submit((Callable<Boolean>) conv);
+        try {
+            boolean res = future.get().booleanValue();
+            if (!res) {
+                throw new IOException("ffmpeg変換処理が正常に終了しませんでした。");
             }
-        };
-               String mail = args[0];
-               String pass = args[1];
-               String tag = args[2];
-               String time = args.length < 4 ? "" : args[3];
-               SProperties setting = SProperties.loadSetting(mail, pass);
-        Converter conv = new Converter(tag, time, setting.toProfile(), sl, new ConvertStopFlag(scl));
-               System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
-               System.out.println("Saccubus on CUI");
-               System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
-               System.out.println("Mailaddr: " + mail);
-               System.out.println("Password: hidden");
-               System.out.println("VideoID: " + tag);
-               System.out.println("WaybackTime: " + time);
-               System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
-               conv.run();
-               System.out.println("Finished.");
-       }
+        } catch (Exception ex) {
+            throw new IOException("取得失敗", ex);
+        }
+        System.out.println("Finished.");
+    }
+
+    private Options createOptions(String[] args) {
+        Options options = new Options();
+//        Option id = OptionBuilder.withArgName("mail").hasArg().withDescription("ニコニコ動画ログインID(メールアドレス)").create("id");
+//        Option password = OptionBuilder.withArgName("password").hasArg().withDescription("ニコニコ動画ログインパスワード").
+//                create("password");
+//        Option video = OptionBuilder.withArgName("id").hasArg().withDescription("ニコニコ動画ビデオID").create("video");
+        Option videoFile = OptionBuilder.withArgName("file").hasArg().withDescription("動画ファイル").create("file_video");
+        Option commentFile = OptionBuilder.withArgName("file").hasArg().withDescription("コメントファイル").
+                create("file_comment");
+        Option tcommentFile = OptionBuilder.withArgName("file").hasArg().withDescription("投稿者コメントファイル").
+                create("file_tcomment");
+        Option outputFile = OptionBuilder.withArgName("file").hasArg().withDescription("出力ファイル").create("file_output");
+        Option ffmpeg = OptionBuilder.withArgName("option").hasArg().withDescription("ffmpeg変換オプション").create("ffmpeg");
+//        options.addOption(id);
+//        options.addOption(password);
+//        options.addOption(video);
+        options.addOption(videoFile);
+        options.addOption(commentFile);
+        options.addOption(tcommentFile);
+        options.addOption(outputFile);
+        options.addOption(ffmpeg);
+        return options;
+    }
+
+    private Profile createExecuteOption(CommandLine cmd) throws IOException {
+        final String mail = "dmy";
+        final String pass = "dmy";
+        final String video = cmd.getOptionValue("file_video");
+        final String comm = cmd.getOptionValue("file_comment");
+        final String tcomm = cmd.getOptionValue("file_tcomment");
+        final String output = cmd.getOptionValue("file_output");
+        final String ff = cmd.getOptionValue("ffmpeg");
+
+        SProperties setting = SProperties.loadSetting(mail, pass);
+        Profile p = setting.toProfile();
+        GeneralSetting general = p.getGeneralSetting();
+        LoginInfo loginInfo = p.getLoginInfo();
+//        InputFileSetting videoSetting = p.getVideoSetting();
+//        InputFileSetting commentSetting = p.getCommentSetting();
+        CommentGetInfo commentGetInfo = p.getCommentGetInfo();
+//        InputFileSetting tcommentSetting = p.getTcommentSetting();
+//        OutputFileSetting outputFileSetting = p.getOutputFileSetting();
+        Ffmpeg ffmpeg = p.getFfmpeg();
+
+        FfmpegOption newFfmpegOption = new FfmpegOption(ffmpeg.getFfmpegOption().getExtOption(), "", "", ff);
+        InputFileSetting newVideoSetting = new InputFileSetting(new SFile(true, new File(video)), false, false);
+        InputFileSetting newCommentSetting = new InputFileSetting(new SFile(true, new File(comm)), false, false);
+        InputFileSetting newTcommentSetting = new InputFileSetting(new SFile(true, new File(tcomm)), false, false);
+        OutputFileSetting newOutputFileSetting = new OutputFileSetting(new SFile(true, new File(output)), true, false,
+                true, true);
+        Ffmpeg newFfmpeg = new Ffmpeg(ffmpeg.getFfmpeg(), ffmpeg.getVhook(), newFfmpegOption, ffmpeg.isVhookDisabled(), ffmpeg.
+                getMaxNumOfComment(), ffmpeg.getFont(), ffmpeg.getFontIndex(), ffmpeg.getShadowIndex(), ffmpeg.
+                isShowConverting(), ffmpeg.isSelfAdjustFontSize(), ffmpeg.isCommentOpaque(), ffmpeg.getNgSetting());
+
+        p = new Profile(general, loginInfo, newVideoSetting, newCommentSetting, commentGetInfo, newTcommentSetting,
+                newOutputFileSetting, newFfmpeg);
+
+        return p;
+    }
+
+    /**
+     * 昔の引数形式でプログラムを実行する.
+     */
+    private static void doWithOldArguments(String[] args) throws IOException {
+        String mail = args[0];
+        String pass = args[1];
+        String tag = args[2];
+        String time = args.length < 4 ? "" : args[3];
+        SProperties setting = SProperties.loadSetting(mail, pass);
+        Converter conv = new Converter(tag, time, setting.toProfile(), TextProgressListener.EMPTY_LISTENER, new ConvertStopFlag(
+                ConvertStopFlag.StateChangeListener.EMPTY_LISTENER));
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println("Saccubus on CUI");
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println("Mailaddr: " + mail);
+        System.out.println("Password: hidden");
+        System.out.println("VideoID: " + tag);
+        System.out.println("WaybackTime: " + time);
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        conv.run();
+        System.out.println("Finished.");
+    }
 }