From 91ddda31f587bb422d96fdcd9d9ce3e14c0fdfe9 Mon Sep 17 00:00:00 2001 From: yuki Date: Mon, 1 Feb 2010 14:39:16 +0000 Subject: [PATCH] =?utf8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= =?utf8?q?=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 54 +++++++++++++++++++------------- test/nicobrowser/NicoHttpClientTest.java | 1 - 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/nicobrowser/NicoHttpClient.java b/src/nicobrowser/NicoHttpClient.java index 6267c92..56f57e1 100644 --- a/src/nicobrowser/NicoHttpClient.java +++ b/src/nicobrowser/NicoHttpClient.java @@ -502,47 +502,59 @@ public class NicoHttpClient { } String[] params = resultString.split("&"); -// final String marker = "url="; Map map = new HashMap(); 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; } /** diff --git a/test/nicobrowser/NicoHttpClientTest.java b/test/nicobrowser/NicoHttpClientTest.java index ab5db0b..173dbed 100644 --- a/test/nicobrowser/NicoHttpClientTest.java +++ b/test/nicobrowser/NicoHttpClientTest.java @@ -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"); -- 2.11.0