OSDN Git Service

ProfileインタフェースからneedsConvertメソッドを削除
[coroid/inqubus.git] / frontend / src / saccubus / converter / Converter.java
index 524d547..2f7506e 100644 (file)
@@ -1,73 +1,93 @@
 package saccubus.converter;
 
-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>\83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·</p>
+ * <p>タイトル: さきゅばす</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>説明: ニコニコ動画の動画をコメントつきで保存</p>
  *
- * <p>\92\98\8dì\8c : Copyright (c) 2007 PSI</p>
+ * <p>著作権: Copyright (c) 2007 PSI</p>
  *
- * <p>\89ï\8eÐ\96¼: </p>
+ * <p>会社名: </p>
  *
- * @author \96¢\93ü\97Í
+ * @author 未入力
  * @version 1.0
  */
-public class Converter extends AbstractCommand implements Runnable {
+public class Converter extends AbstractCommand implements Callable<Boolean> {
 
-    private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
-    private final Profile Setting;
-    private final String Tag;
-    private final String Time;
+    private final Profile profile;
+    private final String movieId;
+    private final String time;
 
-    public Converter(String url, String time, Profile setting,
+    /**
+     * コンバータを構築します.
+     * @param url 対象となる動画のURL.
+     * @param time
+     * @param profile
+     * @param listener
+     * @param flag
+     */
+    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) {
-                Tag = url.substring(VIDEO_URL_PARSER.length(), index);
-            } else {
-                Tag = 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 {
-            Tag = url;
+            throw new IllegalArgumentException("URL/IDの指定が不正です: " + url);
         }
-        Time = time;
-        Setting = setting;
+
+        this.time = time;
+        this.profile = profile;
     }
 
     @Override
+    public Boolean call() throws Exception {
+        boolean result = false;
+        try {
+            result = runConvert();
+        } finally {
+            getStopFlag().finished();
+        }
+        return Boolean.valueOf(result);
+    }
+
+    // TODO Runnableを実装しなくなったので削除する
     public void run() {
         try {
-            runConvert();
+            call();
         } catch (Exception ex) {
-            String text = (ex.getMessage() != null) ? ex.getMessage() : "\97\\8aú\82µ\82È\82¢\83G\83\89\81[\94­\90\82Ì\82½\82ß\92\86\92f\82µ\82Ü\82µ\82½\81B";
+            String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
             sendText(text);
             ex.printStackTrace();
-        } finally {
-            getStopFlag().finished();
         }
     }
 
-    private void runConvert() throws IOException, InterruptedException {
-        if (!Setting.shouldRun()) {
-            sendText("\89½\82à\82·\82é\82±\82Æ\82ª\82 \82è\82Ü\82¹\82ñ");
-            return;
+    private boolean runConvert() throws IOException, InterruptedException {
+        if (!shouldRun(profile)) {
+            sendText("何もすることがありません");
+            return true;
         }
 
         validSetting();
-        final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
+        final FfmpegOption ov = profile.getFfmpeg().getFfmpegOption();
 
-        sendText("\83\8d\83O\83C\83\93\92\86");
+        sendText("ログイン中");
 
         final FileInstanciator fi = createInstanciator();
 
@@ -83,121 +103,98 @@ public class Converter extends AbstractCommand implements Runnable {
 
         File tcommFile = fi.getTcommFile(getListener());
 
-        if (!Setting.needsConvert()) {
-            sendText("\93®\89æ\81E\83R\83\81\83\93\83g\82ð\95Û\91\82µ\81A\95Ï\8a·\82Í\8ds\82¢\82Ü\82¹\82ñ\82Å\82µ\82½\81B");
-            return;
+        if (!profile.getOutputFileSetting().isConvert()) {
+            sendText("動画・コメントを保存し、変換は行いませんでした。");
+            return true;
         }
-        // TODO \96{\97\88videoFile\82Ì\8eæ\93¾\8e¸\94s\8e\9e\82Ínull\82Å\82Í\82È\82­IOException\82É\82µ\82½\82¢\82ª...
-        if (videoFile == null || !videoFile.isFile()) {
-            throw new IOException("\93ü\97Í\93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
+
+        if (!videoFile.isFile()) {
+            throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
         }
 
-        if (Setting.getOutputFileSetting().isAddComment()) {
-            if (commentFile == null || !commentFile.isFile()) {
-                throw new IOException("\93ü\97Í\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
+        if (profile.getOutputFileSetting().isAddComment()) {
+            if (!commentFile.isFile()) {
+                throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
             }
         } else {
             commentFile = null;
         }
 
-        if (Setting.getOutputFileSetting().isAddTcomment()) {
-            if (tcommFile == null || !tcommFile.isFile()) {
-                throw new IOException("\93ü\97Í\93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
+        if (profile.getOutputFileSetting().isAddTcomment()) {
+            if (!tcommFile.isFile()) {
+                throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
             }
         } else {
             tcommFile = null;
         }
 
-        /*\83r\83f\83I\96¼\82Ì\8am\92è*/
+        /*ビデオ名の確定*/
         File convertedVideoFile;
-        if (!Setting.getOutputFileSetting().getFile().isFile()) {
-            if (fi.getVideoTitle() == null) {
-                sendText("\95Ï\8a·\8cã\82Ì\83r\83f\83I\83t\83@\83C\83\8b\96¼\82ª\8am\92è\82Å\82«\82Ü\82¹\82ñ\81B");
-                return;
-            }
+        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()).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());
+        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, time);
         return fi;
     }
 
     /**
-     * (\83l\83b\83g\83\8f\81[\83N\90Ý\92è\88È\8aO\82Ì)\90Ý\92è\82ð\8c\9f\8fØ\82·\82é.
-     * @throws IllegalArgumentException \90Ý\92è\82É\95s\94õ\82ª\82 \82é\8fê\8d\87.
+     * (ネットワーク設定以外の)設定を検証する.
+     * @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\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
+                throw new IllegalArgumentException("FFmpegが見つかりません。");
             }
-            if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
-                throw new IllegalArgumentException("\82·\82¢\82Ü\82¹\82ñ\81B\8c»\8dÝvhook\83\89\83C\83u\83\89\83\8a\82É\82Í\94¼\8ap\8bó\94\92\82Í\8eg\82¦\82Ü\82¹\82ñ\81B");
+            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\83\89\83C\83u\83\89\83\8a\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
+                throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
             }
-            a = Setting.getFfmpeg().getFont();
+            a = profile.getFfmpeg().getFont();
             if (!a.canRead()) {
-                throw new IllegalArgumentException("\83t\83H\83\93\83g\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
-            }
-        } else {
-            if (Setting.getVideoSetting().isDelete()) {
-                throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93®\89æ\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
-            }
-            if (Setting.getCommentSetting().isDelete()) {
-                throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
-            }
-            if (Setting.getTcommentSetting().isDelete()) {
-                throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93\8a\8de\8eÒ\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
+                throw new IllegalArgumentException("フォントが見つかりません。");
             }
         }
     }
 
     private String getVideoIDWithBracket() {
-        return "[" + Tag + "]";
+        return "[" + movieId + "]";
     }
 
     public boolean isConverted() {
@@ -208,4 +205,14 @@ public class Converter extends AbstractCommand implements Runnable {
     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());
+    }
 }