OSDN Git Service

watchページアクセス時に動画タイトルも取得しておく(inqubus用)
authoryukihane <yukihane.feather@gmail.com>
Wed, 10 Aug 2011 12:18:55 +0000 (21:18 +0900)
committeryukihane <yukihane.feather@gmail.com>
Wed, 10 Aug 2011 12:18:55 +0000 (21:18 +0900)
src/nicobrowser/NicoHttpClient.java [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index ba579fd..97f1ac8
@@ -513,7 +513,8 @@ public class NicoHttpClient {
      * @throws java.io.IOException ファイル取得失敗. 権限の無いファイルを取得しようとした場合も.
      */
     public VideoInfo getVideoInfo(String videoId) throws IOException {
-        final String realVideoId = getRealVideoId(videoId);
+        final GetRealVideoIdResult res = getRealVideoId(videoId);
+        final String realVideoId = res.videoId;
 
         String accessUrl = GET_FLV_INFO + realVideoId;
         if (realVideoId.startsWith("nm")) {
@@ -560,20 +561,51 @@ public class NicoHttpClient {
     }
 
     /**
+     * watchページコンテンツからタイトルを抽出する.
+     * @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 "";
+
+    }
+
+    private static class GetRealVideoIdResult {
+
+        private final String videoId;
+        private final String title;
+
+        private GetRealVideoIdResult(String videoId, String title) {
+            this.videoId = videoId;
+            this.title = title;
+        }
+    }
+
+    /**
      * watchページへアクセスし, ビデオIDを取得する.
      * soXXXXなど, 実際のビデオIDと異なる場合がある.
      * @param videoId 取得したいビデオのビデオID.
      * @return 実際のビデオID.
      * @throws IOException
      */
-    private String getRealVideoId(String videoId) throws IOException {
-        String realId = accessWatchPage(videoId);
+    private GetRealVideoIdResult getRealVideoId(String videoId) throws IOException {
+        GetRealVideoIdResult res = accessWatchPage(videoId);
+        final String realId = res.videoId;
 
         // ステータスコード302など、リダイレクトが必要な場合
         if (!videoId.equals(realId)) {
-            realId = getRealVideoId(realId);
+            res = getRealVideoId(realId);
         }
-        return realId;
+        return res;
     }
 
     /**
@@ -582,22 +614,27 @@ public class NicoHttpClient {
      * @return
      * @throws IOException
      */
-    private String accessWatchPage(String videoId) throws IOException {
+    private GetRealVideoIdResult accessWatchPage(String videoId) throws IOException {
         String realId = videoId;
+        String title;
         String watchUrl = WATCH_PAGE + videoId;
         log.debug("アクセス: " + watchUrl);
         http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
         try {
             HttpGet get = new HttpGet(watchUrl);
             HttpResponse response = http.execute(get);
-            if (response.containsHeader("Location")) {
-                realId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+            try {
+                if (response.containsHeader("Location")) {
+                    realId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+                }
+                title = getTitleInWatchPage(response.getEntity().getContent());
+            } finally {
+                response.getEntity().consumeContent();
             }
-            response.getEntity().consumeContent();
         } finally {
             http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
         }
-        return realId;
+        return new GetRealVideoIdResult(realId, title);
     }
 
     /**