OSDN Git Service

リダイレクトが必要な動画に対応
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Mon, 30 Nov 2009 20:20:01 +0000 (20:20 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Mon, 30 Nov 2009 20:20:01 +0000 (20:20 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser/trunk@257 bdf3b611-c98c-6041-8292-703d9c9adbe7

src/nicobrowser/NicoHttpClient.java

index ccb3648..33394df 100644 (file)
@@ -36,6 +36,7 @@ import nicobrowser.util.Result;
 import nicobrowser.util.Util;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpResponse;
@@ -527,10 +528,23 @@ public class NicoHttpClient extends DefaultHttpClient {
             IOException,
             URISyntaxException, HttpException, InterruptedException {
         byte[] buffer = new byte[1024 * 32];
-        final String watchUrl = "http://www.nicovideo.jp/watch/" + videoID;
+
+        String watchUrl = "http://www.nicovideo.jp/watch/" + videoID;
         log.debug("アクセス: " + watchUrl);
+        getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
         HttpGet get = new HttpGet(watchUrl);
         HttpResponse response = execute(get);
+        String redirectId = videoID;
+        if (response.containsHeader("Location")) {
+            redirectId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+            response.getEntity().consumeContent();
+            watchUrl = "http://www.nicovideo.jp/watch/" + redirectId;
+            log.debug("アクセス: " + watchUrl);
+            get = new HttpGet(watchUrl);
+            response = execute(get);
+        }
+        getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
+
         final String userId = Util.getUserId(response.getEntity().getContent());
         log.debug("userId: " + userId);
         response.getEntity().consumeContent();
@@ -545,7 +559,7 @@ public class NicoHttpClient extends DefaultHttpClient {
             response.getEntity().consumeContent();
         }
 
-        URL url = getFlvUrl(videoID);
+        URL url = getFlvUrl(redirectId);
         if (nowStatus == Status.GET_LOW && url.toString().contains("low")) {
             log.info("lowファイル取得済みのためスキップ" + videoID + ":" + fileName);
             return new GetFlvResult(nowStatus, userName);