OSDN Git Service

リファクタリング
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Mon, 1 Feb 2010 14:39:16 +0000 (14:39 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Mon, 1 Feb 2010 14:39:16 +0000 (14:39 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser/branches/dev20100201_comment_download@275 bdf3b611-c98c-6041-8292-703d9c9adbe7

src/nicobrowser/NicoHttpClient.java
test/nicobrowser/NicoHttpClientTest.java

index 6267c92..56f57e1 100644 (file)
@@ -502,47 +502,59 @@ public class NicoHttpClient {
         }
 
         String[] params = resultString.split("&");
-//        final String marker = "url=";
         Map<String, String> map = new HashMap<String, String>();
         for (String param : params) {
             String[] elm = param.split("=");
             map.put(elm[0], elm[1]);
-            //            if (url.contains(marker)) {
-            //                String result = url.substring(marker.length());
-            //                result = URLDecoder.decode(result, "UTF-8");
-            //
-            //                return new URL(result);
-            //            }
         }
-//        throw new IOException("ダウンロードに失敗しました(削除済みの可能性もあります)。 ID: " + videoID + ", パラメータ:" + resultString);
         return new VideoInfo(realVideoId, map);
     }
 
+    /**
+     * watchページへアクセスし, ビデオIDを取得する.
+     * soXXXXなど, 実際のビデオIDと異なる場合がある.
+     * @param videoId 取得したいビデオのビデオID.
+     * @return 実際のビデオID.
+     * @throws IOException
+     */
     private String getRealVideoId(String videoId) throws IOException {
+        String realId = accessWatchPage(videoId);
+
+        // ステータスコード302など、リダイレクトが必要な場合
+        if (!videoId.equals(realId)) {
+            realId = getRealVideoId(realId);
+        }
+        return realId;
+    }
+
+    /**
+     * WATCHページへアクセスする. getflvを行うためには, 必ず事前にWATCHページへアクセスしておく必要があるため.
+     * @param videoId ビデオID.
+     * @return
+     * @throws IOException
+     */
+    private String accessWatchPage(String videoId) throws IOException {
+        String realId = videoId;
         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);
-            String realID = videoId;
-
-            // ステータスコード302など、リダイレクトが必要な場合
             if (response.containsHeader("Location")) {
-                realID = response.getFirstHeader("Location").getValue().replace("/watch/", "");
-                response.getEntity().consumeContent();
-                watchUrl = WATCH_PAGE + realID;
-                log.debug("アクセス: " + watchUrl);
-                HttpGet watchGet = new HttpGet(watchUrl);
-                HttpResponse watchResponse = http.execute(get);
-                watchResponse.getEntity().consumeContent();
-            } else {
-                response.getEntity().consumeContent();
+                realId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
             }
-            return realID;
+            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+            String line;
+            while((line = reader.readLine())!=null){
+                System.out.println(line);
+            }
+            reader.close();
+            response.getEntity().consumeContent();
         } finally {
             http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
         }
+        return realId;
     }
 
     /**
index ab5db0b..173dbed 100644 (file)
@@ -164,7 +164,6 @@ public class NicoHttpClientTest {
         assertNotSame(OFFICIAL_VIDEO, vi.getRealVideoId());
     }
 
-    @Test
     public void downLoad() throws URISyntaxException, IOException, HttpException, InterruptedException {
         System.out.println("downLoad");