OSDN Git Service

コメント取得数の手動設定を有効化
authoryukihane <yukihane.feather@gmail.com>
Mon, 15 Aug 2011 06:01:07 +0000 (15:01 +0900)
committeryukihane <yukihane.feather@gmail.com>
Mon, 15 Aug 2011 06:02:23 +0000 (15:02 +0900)
src/nicobrowser/NicoHttpClient.java
test/nicobrowser/NicoHttpClientTest.java

index 667a3e5..dfcfeb0 100644 (file)
@@ -791,19 +791,25 @@ public class NicoHttpClient {
         return getFlvFile(vi, vi.getRealVideoId(), Status.GET_INFO, true, ProgressListener.EMPTY_LISTENER);
     }
 
-    public File getCommentFile(VideoInfo vi, String fileName, WayBackInfo wayback) throws Exception {
-        return downloadComment(vi, fileName, false, wayback);
+    public File getCommentFile(VideoInfo vi, String fileName, WayBackInfo wayback, int commentNum) throws Exception {
+        return downloadComment(vi, fileName, false, wayback, Integer.valueOf(commentNum));
+    }
+
+    public File getCommentFile(VideoInfo vi, String fileName) throws Exception {
+        return downloadComment(vi, fileName, false, null, null);
     }
 
     public File getTCommentFile(VideoInfo vi, String fileName) throws Exception {
-        return downloadComment(vi, fileName, true, null, true);
+        return downloadComment(vi, fileName, true, null, Integer.valueOf(1000), true);
     }
 
-    private File downloadComment(VideoInfo vi, String fileName, boolean isTcomm, WayBackInfo wayback) throws Exception {
-        return downloadComment(vi, fileName, isTcomm, wayback, false);
+    private File downloadComment(VideoInfo vi, String fileName, boolean isTcomm, WayBackInfo wayback, Integer commentNum)
+            throws Exception {
+        return downloadComment(vi, fileName, isTcomm, wayback, commentNum, false);
     }
 
-    private File downloadComment(VideoInfo vi, String fileName, boolean isTcomm, WayBackInfo wayback, boolean oldVersion)
+    private File downloadComment(VideoInfo vi, String fileName, boolean isTcomm, WayBackInfo wayback, Integer commentNum,
+            boolean oldVersion)
             throws Exception {
         HttpResponse response = null;
         BufferedOutputStream bos = null;
@@ -812,9 +818,9 @@ public class NicoHttpClient {
             final HttpPost post = new HttpPost(vi.getMessageUrl().toString());
             final String param;
             if (oldVersion || isTcomm) {
-                param = createCommendDownloadParameter20101222(vi, isTcomm, wayback);
+                param = createCommendDownloadParameter20101222(vi, isTcomm, wayback, commentNum);
             } else {
-                param = createCommendDownloadParameter(vi, wayback);
+                param = createCommendDownloadParameter(vi, wayback, commentNum);
             }
             final StringEntity se = new StringEntity(param);
             post.setEntity(se);
@@ -844,7 +850,8 @@ public class NicoHttpClient {
         }
     }
 
-    private String createCommendDownloadParameter(VideoInfo vi, WayBackInfo wayback) {
+    private String createCommendDownloadParameter(VideoInfo vi, WayBackInfo wayback, Integer commentNum) {
+        // TODO 通常 wayback はnullのはずだが…要確認
         final String quote = "\"";
         final Map<String, String> th = new HashMap<String, String>();
         th.put("thread", vi.getThreadId());
@@ -856,7 +863,8 @@ public class NicoHttpClient {
         leaf.put("user_id", vi.getUserId());
 
         final int length = (int) Math.ceil(vi.getVideoLength() / 60.0);
-        final String element = "0-" + length + ":100," + vi.getResFrom();
+        final int resFrom = (commentNum != null) ? commentNum.intValue() : vi.getResFrom();
+        final String element = "0-" + length + ":100," + resFrom;
 
         final StringBuilder str = new StringBuilder();
         str.append("<packet>");
@@ -896,9 +904,10 @@ public class NicoHttpClient {
      * 2010/12/22 までのコメント表示仕様に基づいた取得パラメータ生成.
      * 「コメントの量を減らす」にチェックを入れた場合は現在でもこれが用いられているはず.
      */
-    private String createCommendDownloadParameter20101222(VideoInfo vi, boolean isTcomm, WayBackInfo wayback) {
+    private String createCommendDownloadParameter20101222(VideoInfo vi, boolean isTcomm, WayBackInfo wayback,
+            Integer commentNum) {
+        final int resFrom = (commentNum == null || commentNum <= 0) ? vi.getResFrom() : commentNum.intValue();
         final String tcommStr = (isTcomm) ? "fork=\"1\" " : "";
-        // TODO wayBackStr 使用するのを忘れている
         final String wayBackStr = (wayback != null) ? "when=" + "\"" + wayback.getTime() + "\"" + " waybackkey=" + "\""
                 + wayback.getKey() + " " : "";
         StringBuilder builder = new StringBuilder();
@@ -908,8 +917,9 @@ public class NicoHttpClient {
         }
         final String officialOption = builder.toString();
 
-        return "<thread " + VideoInfo.KEY_USER_ID + "=\"" + vi.getUserId() + "\" res_from=\"" + (-1 * vi.getResFrom())
-                + "\" version=\"20061206\" thread=\"" + vi.getThreadId() + "\" " + tcommStr + officialOption + "/>";
+        return "<thread " + VideoInfo.KEY_USER_ID + "=\"" + vi.getUserId() + "\" res_from=\"" + (-1 * resFrom)
+                + "\" version=\"20061206\" thread=\"" + vi.getThreadId() + "\" " + wayBackStr + tcommStr
+                + officialOption + "/>";
     }
 
     /**
index e268855..0bf1a05 100644 (file)
@@ -281,7 +281,7 @@ public class NicoHttpClientTest {
 
         instance.login(OK_MAIL, OK_PASS);
         VideoInfo vi = instance.getVideoInfo(NORMAL_VIDEO);
-        instance.getCommentFile(vi, comm, null);
+        instance.getCommentFile(vi, comm);
         instance.getTCommentFile(vi, tcomm);
 
         assertTrue(commFile.exists());
@@ -300,7 +300,7 @@ public class NicoHttpClientTest {
 
         instance.login(OK_MAIL, OK_PASS);
         VideoInfo vi = instance.getVideoInfo(OFFICIAL_VIDEO);
-        instance.getCommentFile(vi, comm, null);
+        instance.getCommentFile(vi, comm);
         instance.getTCommentFile(vi, tcomm);
 
         assertTrue(commFile.exists());