OSDN Git Service

動画ID抽出メソッドをバックポート
authoryukihane <yukihane.feather@gmail.com>
Sun, 21 Aug 2011 02:43:55 +0000 (11:43 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sun, 21 Aug 2011 02:43:55 +0000 (11:43 +0900)
frontend/src/yukihane/Util.java

index 6c8175c..237b90b 100644 (file)
@@ -1,6 +1,9 @@
 /* $Id$ */
 package yukihane;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  * nicobrowser.util.Utilより, 必要なものをピックアップしたクラス.
  * @author yuki
@@ -22,4 +25,16 @@ public class Util {
         }
         return contentType.split("/")[1];
     }
+
+    public static String getVideoId(final String alterId) {
+        final int startIdIdx = alterId.lastIndexOf("/") + 1;
+        final String altId = alterId.substring(startIdIdx);
+        final Pattern idPattern = Pattern.compile("([a-z]*\\d+)");
+        final Matcher idMatcher = idPattern.matcher(altId);
+        if (!idMatcher.find()) {
+            throw new IllegalArgumentException("URL/IDの指定が不正です: " + alterId);
+        }
+
+        return idMatcher.group(1);
+    }
 }