OSDN Git Service

過去ログ取得時点をConverterコンストラクタ引数からProfileに移動
[coroid/inqubus.git] / frontend / src / saccubus / converter / Converter.java
index dc657d0..84c934f 100644 (file)
@@ -1,15 +1,15 @@
 package saccubus.converter;
 
-import java.util.regex.Matcher;
-import saccubus.converter.profile.Profile;
-import saccubus.converter.profile.FfmpegOption;
-import saccubus.converter.filegetter.FileInstanciator;
 import java.io.File;
 import java.io.IOException;
 import java.util.concurrent.Callable;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import saccubus.ConvertStopFlag;
+import saccubus.converter.filegetter.FileInstanciator;
 import saccubus.net.TextProgressListener;
+import yukihane.saccubus.converter.profile.FfmpegOption;
+import yukihane.saccubus.converter.profile.Profile;
 
 /**
  * <p>タイトル: さきゅばす</p>
@@ -23,13 +23,20 @@ import saccubus.net.TextProgressListener;
  * @author 未入力
  * @version 1.0
  */
-public class Converter extends AbstractCommand implements Runnable, Callable<Boolean> {
+public class Converter extends AbstractCommand implements Callable<Boolean> {
 
-    private final Profile Setting;
-    private final String Tag;
-    private final String Time;
+    private final Profile profile;
+    private final String movieId;
 
-    public Converter(String url, String time, Profile setting,
+    /**
+     * コンバータを構築します.
+     * @param url 対象となる動画のURL.
+     * @param time
+     * @param profile
+     * @param listener
+     * @param flag
+     */
+    public Converter(String url, Profile profile,
             TextProgressListener listener, ConvertStopFlag flag) {
         super(listener, flag);
 
@@ -39,15 +46,15 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
         final Pattern idPattern = Pattern.compile("([a-z]*\\d+)");
         final Matcher idMatcher = idPattern.matcher(altId);
         if (idMatcher.find()) {
-            Tag = idMatcher.group(1);
+            this.movieId = idMatcher.group(1);
         } else {
             throw new IllegalArgumentException("URL/IDの指定が不正です: " + url);
         }
 
-        Time = time;
-        Setting = setting;
+        this.profile = profile;
     }
 
+    @Override
     public Boolean call() throws Exception {
         boolean result = false;
         try {
@@ -58,6 +65,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
         return Boolean.valueOf(result);
     }
 
+    // TODO Runnableを実装しなくなったので削除する
     public void run() {
         try {
             call();
@@ -69,13 +77,13 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
     }
 
     private boolean runConvert() throws IOException, InterruptedException {
-        if (!Setting.shouldRun()) {
+        if (!shouldRun(profile)) {
             sendText("何もすることがありません");
             return true;
         }
 
         validSetting();
-        final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
+        final FfmpegOption ov = profile.getFfmpeg().getFfmpegOption();
 
         sendText("ログイン中");
 
@@ -93,7 +101,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
 
         File tcommFile = fi.getTcommFile(getListener());
 
-        if (!Setting.needsConvert()) {
+        if (!profile.getOutputFileSetting().isConvert()) {
             sendText("動画・コメントを保存し、変換は行いませんでした。");
             return true;
         }
@@ -102,7 +110,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
             throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
         }
 
-        if (Setting.getOutputFileSetting().isAddComment()) {
+        if (profile.getOutputFileSetting().isAddComment()) {
             if (!commentFile.isFile()) {
                 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
             }
@@ -110,7 +118,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
             commentFile = null;
         }
 
-        if (Setting.getOutputFileSetting().isAddTcomment()) {
+        if (profile.getOutputFileSetting().isAddTcomment()) {
             if (!tcommFile.isFile()) {
                 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
             }
@@ -120,52 +128,42 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
 
         /*ビデオ名の確定*/
         File convertedVideoFile;
-        if (!Setting.getOutputFileSetting().getFile().isFile()) {
+        if (!profile.getOutputFileSetting().getFile().isFile()) {
             String conv_name = fi.getVideoTitle();
-            if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
+            if (profile.getOutputFileSetting().isAppendPrefixVideoId()) {
                 conv_name = getVideoIDWithBracket() + conv_name;
             }
-            convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
+            convertedVideoFile = new File(profile.getOutputFileSetting().getFile().getFile(),
                     conv_name + ov.getExtOption());
         } else {
-            String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
+            String filename = profile.getOutputFileSetting().getFile().getFile().getPath();
             if (!filename.endsWith(ov.getExtOption())) {
                 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
                 convertedVideoFile = new File(filename);
             } else {
-                convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
+                convertedVideoFile = profile.getOutputFileSetting().getFile().getFile();
             }
         }
 
         boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
-                convertedVideoFile, Setting.getFfmpeg(), Setting.getGeneralSetting()).execute();
-        if (res) {
-            if (Setting.getCommentSetting().isDelete()) {
-                commentFile.delete();
-            }
-            if (Setting.getVideoSetting().isDelete()) {
-                videoFile.delete();
-            }
-            if (Setting.getTcommentSetting().isDelete()) {
-                tcommFile.delete();
-            }
-        }
+                convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
         return res;
     }
 
     private FileInstanciator createInstanciator() throws IOException {
         FileInstanciator fi;
 
-        FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
+        FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(profile.getVideoSetting());
 
-        FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
-                getCommentSetting(), Setting.getCommentGetInfo().isSelfAdjustCommentNum(), Setting.getCommentGetInfo().
-                getBackComment(), Setting.getCommentGetInfo().isReduceComment());
+        FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(profile.
+                getCommentSetting(), profile.getCommentSetting().isSelfAdjustCommentNum(), profile.getCommentSetting().
+                getBackComment(), profile.getCommentSetting().isReduceComment());
 
         FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
-                Setting.getTcommentSetting());
+                profile.getCommentSetting());
 
-        fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
+        fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, profile.getLoginInfo(), profile.
+                getProxySetting(), movieId, profile.getCommentSetting().getBackLogPoint());
         return fi;
     }
 
@@ -174,37 +172,27 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
      * @throws IllegalArgumentException 設定に不備がある場合.
      */
     private void validSetting() {
-        if (Setting.needsConvert()) {
-            File a = Setting.getFfmpeg().getFfmpeg();
+        if (profile.getOutputFileSetting().isConvert()) {
+            File a = profile.getFfmpeg().getFfmpeg();
             if (!a.canRead()) {
                 throw new IllegalArgumentException("FFmpegが見つかりません。");
             }
-            if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
+            if (profile.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
                 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
             }
-            a = Setting.getFfmpeg().getVhook();
+            a = profile.getFfmpeg().getVhook();
             if (!a.canRead()) {
                 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
             }
-            a = Setting.getFfmpeg().getFont();
+            a = profile.getFfmpeg().getFont();
             if (!a.canRead()) {
                 throw new IllegalArgumentException("フォントが見つかりません。");
             }
-        } else {
-            if (Setting.getVideoSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
-            }
-            if (Setting.getCommentSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
-            }
-            if (Setting.getTcommentSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
-            }
         }
     }
 
     private String getVideoIDWithBracket() {
-        return "[" + Tag + "]";
+        return "[" + movieId + "]";
     }
 
     public boolean isConverted() {
@@ -215,4 +203,14 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
     public ConvertStopFlag getStopFlag() {
         return super.getStopFlag();
     }
+
+    /** @return 何か実行すべき処理があればtrue. */
+    private static boolean shouldRun(Profile profile) {
+        return profile.getOutputFileSetting().isConvert() || needsDownload(profile);
+    }
+
+    /** @return 何かダウンロードするものがあればtrue. */
+    private static boolean needsDownload(Profile profile) {
+        return (profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload());
+    }
 }