OSDN Git Service

ProfileインタフェースからneedsConvertメソッドを削除
[coroid/inqubus.git] / frontend / src / saccubus / converter / Converter.java
index d80a9f9..2f7506e 100644 (file)
@@ -1,13 +1,15 @@
 package saccubus.converter;
 
-import saccubus.converter.profile.Profile;
-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.classic.profile.FfmpegOption;
+import saccubus.converter.filegetter.FileInstanciator;
 import saccubus.net.TextProgressListener;
+import yukihane.saccubus.converter.profile.FfmpegOption;
+import yukihane.saccubus.converter.profile.Profile;
 
 /**
  * <p>タイトル: さきゅばす</p>
@@ -21,9 +23,8 @@ 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 static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
     private final Profile profile;
     private final String movieId;
     private final String time;
@@ -39,21 +40,23 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
     public Converter(String url, String time, Profile profile,
             TextProgressListener listener, ConvertStopFlag flag) {
         super(listener, flag);
-        url = url.trim();
-        if (url.startsWith(VIDEO_URL_PARSER)) {
-            int index = url.indexOf('?', VIDEO_URL_PARSER.length());
-            if (index >= 0) {
-                movieId = url.substring(VIDEO_URL_PARSER.length(), index);
-            } else {
-                movieId = url.substring(VIDEO_URL_PARSER.length());
-            }
+
+        // TODO 入力欄の値から動画IDの切り出しはGUI側でやるべきだろう
+        final int startIdIdx = url.lastIndexOf("/") + 1;
+        final String altId = url.substring(startIdIdx);
+        final Pattern idPattern = Pattern.compile("([a-z]*\\d+)");
+        final Matcher idMatcher = idPattern.matcher(altId);
+        if (idMatcher.find()) {
+            this.movieId = idMatcher.group(1);
         } else {
-            movieId = url;
+            throw new IllegalArgumentException("URL/IDの指定が不正です: " + url);
         }
+
         this.time = time;
         this.profile = profile;
     }
 
+    @Override
     public Boolean call() throws Exception {
         boolean result = false;
         try {
@@ -64,6 +67,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
         return Boolean.valueOf(result);
     }
 
+    // TODO Runnableを実装しなくなったので削除する
     public void run() {
         try {
             call();
@@ -99,7 +103,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
 
         File tcommFile = fi.getTcommFile(getListener());
 
-        if (!profile.needsConvert()) {
+        if (!profile.getOutputFileSetting().isConvert()) {
             sendText("動画・コメントを保存し、変換は行いませんでした。");
             return true;
         }
@@ -145,17 +149,6 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
 
         boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
                 convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
-        if (res) {
-            if (profile.getCommentSetting().isDelete()) {
-                commentFile.delete();
-            }
-            if (profile.getVideoSetting().isDelete()) {
-                videoFile.delete();
-            }
-            if (profile.getTcommentSetting().isDelete()) {
-                tcommFile.delete();
-            }
-        }
         return res;
     }
 
@@ -165,13 +158,14 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
         FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(profile.getVideoSetting());
 
         FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(profile.
-                getCommentSetting(), profile.getCommentGetInfo().isselfAdjustCommentNum(), profile.getCommentGetInfo().
-                getBackComment());
+                getCommentSetting(), profile.getCommentSetting().isSelfAdjustCommentNum(), profile.getCommentSetting().
+                getBackComment(), profile.getCommentSetting().isReduceComment());
 
         FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
-                profile.getTcommentSetting());
+                profile.getCommentSetting());
 
-        fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, profile.getLoginInfo(), movieId, time);
+        fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, profile.getLoginInfo(), profile.
+                getProxySetting(), movieId, time);
         return fi;
     }
 
@@ -180,7 +174,7 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
      * @throws IllegalArgumentException 設定に不備がある場合.
      */
     private void validSetting() {
-        if (profile.needsConvert()) {
+        if (profile.getOutputFileSetting().isConvert()) {
             File a = profile.getFfmpeg().getFfmpeg();
             if (!a.canRead()) {
                 throw new IllegalArgumentException("FFmpegが見つかりません。");
@@ -196,16 +190,6 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
             if (!a.canRead()) {
                 throw new IllegalArgumentException("フォントが見つかりません。");
             }
-        } else {
-            if (profile.getVideoSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
-            }
-            if (profile.getCommentSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
-            }
-            if (profile.getTcommentSetting().isDelete()) {
-                throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
-            }
         }
     }
 
@@ -224,6 +208,11 @@ public class Converter extends AbstractCommand implements Runnable, Callable<Boo
 
     /** @return 何か実行すべき処理があればtrue. */
     private static boolean shouldRun(Profile profile) {
-        return profile.getOutputFileSetting().isConvert() || profile.needsDownload();
+        return profile.getOutputFileSetting().isConvert() || needsDownload(profile);
+    }
+
+    /** @return 何かダウンロードするものがあればtrue. */
+    private static boolean needsDownload(Profile profile) {
+        return (profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload());
     }
 }