OSDN Git Service

指定されたRSS URLからコンテンツを読み込むメソッドを追加.
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 23 May 2009 15:24:46 +0000 (15:24 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 23 May 2009 15:24:46 +0000 (15:24 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowserBranches/release_20090323/NicoBrowser@117 bdf3b611-c98c-6041-8292-703d9c9adbe7

src/nicobrowser/NicoHttpClient.java
test/nicobrowser/NicoHttpClientRssTest.java [new file with mode: 0644]

index f08d4d5..f113f7d 100644 (file)
@@ -199,6 +199,7 @@ public class NicoHttpClient extends DefaultHttpClient {
             // BOMを読み捨て
             // reader.skip(1);
             list = getNicoContents(reader);
+            deleteRankString(list);
             response.getEntity().consumeContent();
         } catch (FeedException ex) {
             log.error("", ex);
@@ -217,39 +218,43 @@ public class NicoHttpClient extends DefaultHttpClient {
     }
 
     /**
+     * ニコニコ動画のRSSからコンテンツリストを取得する.
+     * @param url 取得するrssのurl.
+     * @return コンテンツリスト.
+     */
+    public List<NicoContent> getContentsFromRss(String url) {
+        log.debug("アクセスURL: " + url);
+        List<NicoContent> list = accessRssUrl(url);
+        if (url.contains("ranking")) {
+            deleteRankString(list);
+        }
+        return list;
+    }
+
+    /**
+     * rankingの場合、本当のタイトルの前に"第XX位:"の文字列が
+     * 挿入されているため, それを削る.
+     * @param list 対象のリスト.
+     */
+    private void deleteRankString(List<NicoContent> list) {
+        for (NicoContent c : list) {
+            String title = c.getTitle();
+            int offset = title.indexOf(":") + 1;
+            c.setTitle(title.substring(offset));
+        }
+    }
+
+    /**
      * マイリストに登録した動画一覧の取得.
      * 「公開」設定にしていないリストからは取得できない.
      * ログインしていなくても取得可能.
      * @param listNo マイリストNo.
      * @return 動画一覧.
      */
-    public List<NicoContent> loadMyList(String listNo) throws URISyntaxException, HttpException, InterruptedException {
-        List<NicoContent> contList = new ArrayList<NicoContent>();
+    public List<NicoContent> loadMyList(String listNo) {
         String url = new String(MY_LIST_PAGE_HEADER + listNo + "?rss=atom");
         log.debug("マイリストURL: " + url);
-
-        HttpGet get = new HttpGet(url);
-
-        Reader reader = null;
-        try {
-            HttpResponse response = execute(get);
-            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
-            contList = getNicoContents(reader);
-        } catch (FeedException ex) {
-            log.warn("マイリストにアクセスできません: " + url);
-            log.debug("", ex);
-        } catch (IOException ex) {
-            log.error("", ex);
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (IOException ex) {
-                    log.error("", ex);
-                }
-            }
-        }
-        return contList;
+        return accessRssUrl(url);
     }
 
     /**
@@ -319,6 +324,42 @@ public class NicoHttpClient extends DefaultHttpClient {
         return cont;
     }
 
+    private List<NicoContent> accessRssUrl(String url) {
+        List<NicoContent> contList = new ArrayList<NicoContent>();
+        HttpGet get = new HttpGet(url);
+        BufferedReader reader = null;
+        try {
+            HttpResponse response = execute(get);
+            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
+            if (log.isTraceEnabled()) {
+                reader.mark(1024 * 1024);
+                while (true) {
+                    String str = reader.readLine();
+                    if (str == null) {
+                        break;
+                    }
+                    log.trace(str);
+                }
+                reader.reset();
+            }
+            contList = getNicoContents(reader);
+        } catch (FeedException ex) {
+            log.warn("アクセスできません: " + url);
+            log.debug("", ex);
+        } catch (IOException ex) {
+            log.error("", ex);
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException ex) {
+                    log.error("", ex);
+                }
+            }
+        }
+        return contList;
+    }
+
     private List<NicoContent> getNicoContents(Reader reader) throws FeedException {
         List<SyndEntryImpl> list = null;
         SyndFeedInput input = new SyndFeedInput();
@@ -344,8 +385,8 @@ public class NicoHttpClient extends DefaultHttpClient {
 
             @Override
             public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
-//                System.out.println("--------<" + t.toString() + ">--------");
-//                printAttributes(a);
+                log.debug("--------<" + t.toString() + ">--------");
+                log.debug(a);
                 if (HTML.Tag.IMG.equals(t)) {
                     imageLink = a.getAttribute(HTML.Attribute.SRC).toString();
                 }
@@ -359,8 +400,8 @@ public class NicoHttpClient extends DefaultHttpClient {
                         descFlag = true;
                     }
                 }
-//                System.out.println("--------<" + t.toString() + ">--------");
-//                printAttributes(a);
+                log.debug("--------<" + t.toString() + ">--------");
+                log.debug(a);
             }
 
             @Override
@@ -368,7 +409,7 @@ public class NicoHttpClient extends DefaultHttpClient {
                 if (HTML.Tag.P.equals(t)) {
                     descFlag = false;
                 }
-//                System.out.println("--------</" + t.toString() + ">--------");
+                log.debug("--------</" + t.toString() + ">--------");
             }
 
             @Override
@@ -376,9 +417,8 @@ public class NicoHttpClient extends DefaultHttpClient {
                 if (descFlag) {
                     description.append(data);
                 }
-
-//                System.out.println("--------TEXT--------");
-//                System.out.println(data);
+                log.debug("--------TEXT--------");
+                log.debug(data);
             }
 
             private void printAttributes(MutableAttributeSet a) {
@@ -404,9 +444,7 @@ public class NicoHttpClient extends DefaultHttpClient {
             NicoContent content = new NicoContent();
 
             String title = entry.getTitle();
-            // 本当のタイトルの前に「第XX位:」が付いてるので取り除く.
-            int offset = title.indexOf(":");
-            content.setTitle(title.substring(offset + 1));
+            content.setTitle(title);
             content.setPageLink(entry.getLink());
 
             // サムネイル画像リンクと説明文の取得
diff --git a/test/nicobrowser/NicoHttpClientRssTest.java b/test/nicobrowser/NicoHttpClientRssTest.java
new file mode 100644 (file)
index 0000000..b331b3f
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package nicobrowser;
+
+import java.net.MalformedURLException;
+import java.util.List;
+import nicobrowser.entity.NicoContent;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Test;
+
+/**
+ *
+ * @author yuki
+ */
+public class NicoHttpClientRssTest {
+
+    static final Log log = LogFactory.getLog(NicoHttpClientRssTest.class);
+    final NicoHttpClient client = NicoHttpClient.getInstance();
+
+    @Test
+    public void testNewArraivalCategory() throws MalformedURLException {
+        log.info("testNewArraivalCategory");
+
+        String page = "http://www.nicovideo.jp/tag/科学?sort=f&rss=2.0";
+        List<NicoContent> contents = client.getContentsFromRss(page);
+        for (NicoContent c : contents) {
+            System.out.println(c.getNicoId() + ": " + c.getTitle());
+        }
+    }
+
+    @Test
+    public void testNewArraival() {
+        log.info("testNewArraival");
+
+        String page = "http://www.nicovideo.jp/newarrival?rss=2.0";
+        List<NicoContent> contents = client.getContentsFromRss(page);
+        for (NicoContent c : contents) {
+            System.out.println(c.getNicoId() + ": " + c.getTitle());
+        }
+    }
+
+    @Test
+    public void testNewArraivalCommentMovie() {
+        log.info("testNewArraivalCommentMovie");
+
+        String page = "http://www.nicovideo.jp/recent?rss=2.0";
+        List<NicoContent> contents = client.getContentsFromRss(page);
+        for (NicoContent c : contents) {
+            System.out.println(c.getNicoId() + ": " + c.getTitle());
+        }
+    }
+
+    @Test
+    public void testRanking() {
+        log.info("testRanking");
+
+        String page = "http://www.nicovideo.jp/ranking/mylist/daily/science?rss=2.0";
+        List<NicoContent> contents = client.getContentsFromRss(page);
+        for (NicoContent c : contents) {
+            System.out.println(c.getNicoId() + ": " + c.getTitle());
+        }
+    }
+}