OSDN Git Service

ZeroでWatchページからタイトルを取得する
authoryukihane <yukihane.feather@gmail.com>
Tue, 3 Jul 2012 11:07:44 +0000 (20:07 +0900)
committeryukihane <yukihane.feather@gmail.com>
Tue, 3 Jul 2012 11:07:44 +0000 (20:07 +0900)
src/nicobrowser/NicoHttpClient.java
src/nicobrowser/util/Util.java
src/nicobrowser/util/WatchParse.groovy [new file with mode: 0644]

index adf435b..0e40763 100644 (file)
@@ -660,18 +660,7 @@ public class NicoHttpClient {
      * @param content watchページコンテンツのストリーム.
      */
     private String getTitleInWatchPage(InputStream content) throws IOException {
-        final String TITLE_PARSE_STR_START = "<title>";
-        BufferedReader br = new BufferedReader(new InputStreamReader(content, "UTF-8"));
-        String ret;
-        while ((ret = br.readLine()) != null) {
-            final int index = ret.indexOf(TITLE_PARSE_STR_START);
-            if (index >= 0) {
-                String videoTitle = ret.substring(index + TITLE_PARSE_STR_START.length(), ret.indexOf(" ‐", index));
-                return videoTitle;
-            }
-        }
-        return "";
-
+        return Util.getTitle(content);
     }
 
     private static class GetRealVideoIdResult {
index 7438c8e..bc9408d 100644 (file)
@@ -13,6 +13,7 @@ public class Util {
 
     private static final Logger logger = LoggerFactory.getLogger(Util.class);
     static ResultParse rp = new ResultParse();
+    static WatchParse wp = new WatchParse();
     static UserInfo ui = new UserInfo();
 
     public static String getExtention(String contentType) {
@@ -30,7 +31,7 @@ public class Util {
         return rp.parse(is);
     }
 
-    public static TreeMap<Integer,String> getOtherPages(InputStream is) {
+    public static TreeMap<Integer, String> getOtherPages(InputStream is) {
         return rp.getOtherPages(is);
     }
 
@@ -74,4 +75,8 @@ public class Util {
         }
         return userName;
     }
+
+    public static String getTitle(InputStream watchPage) {
+        return wp.getTitle(watchPage);
+    }
 }
diff --git a/src/nicobrowser/util/WatchParse.groovy b/src/nicobrowser/util/WatchParse.groovy
new file mode 100644 (file)
index 0000000..f73e761
--- /dev/null
@@ -0,0 +1,29 @@
+package nicobrowser.util
+
+import org.cyberneko.html.parsers.SAXParser
+
+/**
+ * watchページのパースを実行するクラス.
+ * ニコニコ動画(原宿)より前のバージョンでは動作未検証.
+ * @author yuki
+ */
+class WatchParse {
+    def slurper = new XmlSlurper(new SAXParser())
+    def headerTitlePattern = /^(.+) ‐ ニコニコ動画\(.+\)$/
+
+    String getTitle(InputStream is){
+        def html = slurper.parse(is)
+        def res = html.HEAD.TITLE
+        def headerTitle = res.text()
+        def matcher = (headerTitle =~ headerTitlePattern)
+        if(matcher){
+            return matcher[0][1]
+        }
+
+        // ニコニコ動画(Zero)用
+        def innerTitle = html.'**'.find {
+            it.@id == 'videoHeaderDetail'
+        }
+        return innerTitle.DIV.H2.text()
+    }
+}