OSDN Git Service

検索メソッドの実装
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Tue, 1 Jun 2010 08:15:40 +0000 (08:15 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Tue, 1 Jun 2010 08:15:40 +0000 (08:15 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser/branches/dev20100601_search@331 bdf3b611-c98c-6041-8292-703d9c9adbe7

src/nicobrowser/NicoHttpClient.java
src/nicobrowser/search/SearchKind.java [new file with mode: 0644]
src/nicobrowser/search/SearchOrder.java [new file with mode: 0644]
src/nicobrowser/search/SearchResult.java [new file with mode: 0644]
src/nicobrowser/util/ResultParse.groovy
src/nicobrowser/util/Util.java
test/nicobrowser/NicoHttpClientTest.java
test/nicobrowser/util/UtilTest.java

index 7c1ba30..89b0517 100644 (file)
@@ -3,8 +3,13 @@ package nicobrowser;
 
 import java.net.URISyntaxException;
 import java.util.Set;
+import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import nicobrowser.entity.NicoContent;
+import nicobrowser.search.SearchKind;
+import nicobrowser.search.SearchOrder;
 import com.sun.syndication.feed.synd.SyndContentImpl;
 import com.sun.syndication.feed.synd.SyndEntryImpl;
 import com.sun.syndication.feed.synd.SyndFeed;
@@ -36,6 +41,7 @@ import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import nicobrowser.entity.NicoContent.Status;
+import nicobrowser.search.SearchResult;
 import nicobrowser.util.Result;
 import nicobrowser.util.Util;
 import org.apache.commons.io.FilenameUtils;
@@ -150,36 +156,42 @@ public class NicoHttpClient {
      * @param word 検索キーワード
      * @return 検索結果.
      */
-    public List<NicoContent> search(String word) {
+    public SearchResult search(String word, SearchKind kind, SearchOrder order, int page) throws IOException {
         log.debug("検索:" + word);
 
         InputStream is = null;
-        List<NicoContent> conts = new ArrayList<NicoContent>();
-        String url = new String(SEARCH_HEAD + word + SEARCH_TAIL);
+        ArrayList<NicoContent> conts = new ArrayList<NicoContent>();
+        String url = SEARCH_HEAD + word + "?page=" + Integer.toString(page) + "&sort=" + kind.getKey() + "&order=" + order.
+                getKey();
 
         try {
-            while (url != null) {
-                HttpGet get = new HttpGet(url);
-                HttpResponse response;
-                response = http.execute(get);
-                is = new BufferedInputStream(response.getEntity().getContent());
-                assert is.markSupported();
-                is.mark(1024 * 1024);
-                List<Result> results = Util.parseSerchResult(is);
-                for (Result r : results) {
-                    NicoContent c = loadMyMovie(r.getId());
-                    if (c != null) {
-                        conts.add(c);
-                    }
+            HttpGet get = new HttpGet(url);
+            HttpResponse response;
+            response = http.execute(get);
+            is = new BufferedInputStream(response.getEntity().getContent());
+            assert is.markSupported();
+            is.mark(1024 * 1024);
+            List<Result> results = Util.parseSearchResult(is);
+            for (Result r : results) {
+                NicoContent c = loadMyMovie(r.getId());
+                if (c != null) {
+                    conts.add(c);
                 }
-                is.reset();
-                url = Util.getNextPage(is);
-                is.close();
             }
+            is.reset();
+            TreeMap<Integer, String> otherPages = Util.getOtherPages(is);
+            return new SearchResult(conts, otherPages);
         } catch (IOException ex) {
             log.error("検索結果処理時に例外発生", ex);
+            throw ex;
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException ex) {
+                }
+            }
         }
-        return conts;
     }
 
     /**
diff --git a/src/nicobrowser/search/SearchKind.java b/src/nicobrowser/search/SearchKind.java
new file mode 100644 (file)
index 0000000..9ee40e6
--- /dev/null
@@ -0,0 +1,16 @@
+/* $Id$ */
+package nicobrowser.search;
+
+public enum SearchKind {
+
+    LastCommentDate("n"), PostDate("f"), PlayTimes("v"), MyListRegistration("m");
+    private final String key;
+
+    private SearchKind(String key) {
+        this.key = key;
+    }
+
+    public String getKey() {
+        return key;
+    }
+}
diff --git a/src/nicobrowser/search/SearchOrder.java b/src/nicobrowser/search/SearchOrder.java
new file mode 100644 (file)
index 0000000..cf71adf
--- /dev/null
@@ -0,0 +1,16 @@
+/* $Id$ */
+package nicobrowser.search;
+
+public enum SearchOrder {
+
+    Descending("d"), Ascending("a");
+    private final String key;
+
+    private SearchOrder(String key) {
+        this.key = key;
+    }
+
+    public String getKey() {
+        return key;
+    }
+}
diff --git a/src/nicobrowser/search/SearchResult.java b/src/nicobrowser/search/SearchResult.java
new file mode 100644 (file)
index 0000000..d807a35
--- /dev/null
@@ -0,0 +1,25 @@
+/* $Id */
+package nicobrowser.search;
+
+import java.util.ArrayList;
+import java.util.TreeMap;
+import nicobrowser.entity.NicoContent;
+
+public class SearchResult {
+
+    private final ArrayList<NicoContent> contents;
+    private final TreeMap<Integer, String> pages;
+
+    public SearchResult(ArrayList<NicoContent> contents, TreeMap<Integer, String> pages) {
+        this.contents = contents;
+        this.pages = pages;
+    }
+
+    public ArrayList<NicoContent> getContents() {
+        return contents;
+    }
+
+    public TreeMap<Integer, String> getPages() {
+        return pages;
+    }
+}
index e018301..79b3d03 100644 (file)
@@ -17,11 +17,18 @@ class ResultParse {
         return list
     }
 
-    String getNextPage(InputStream is){
+    TreeMap<Integer, String> getOtherPages(InputStream is){
         def html = new XmlSlurper(new SAXParser()).parse(is)
-        def res = html.'**'.find{it.attributes()['rel'] == 'next'}
-        if(res==null) return null
-        return res.attributes()['href']
+        def res = html.'**'.find{it.@class == 'pager'}
+        def map =[:]
+        res.TBODY.TR.TD.A.each{
+            try{
+                def page = Integer.parseInt(it.text())
+                def url = it.@href.text()
+                map.put(page,url)
+            }catch(e){}
+        }
+        return map
     }
 }
 
index ddf6e77..1d8c44f 100644 (file)
@@ -5,6 +5,7 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.List;
+import java.util.TreeMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -24,12 +25,12 @@ public class Util {
         return contentType.split("/")[1];
     }
 
-    public static List<Result> parseSerchResult(InputStream is) {
+    public static List<Result> parseSearchResult(InputStream is) {
         return rp.parse(is);
     }
 
-    public static String getNextPage(InputStream is) {
-        return rp.getNextPage(is);
+    public static TreeMap<Integer,String> getOtherPages(InputStream is) {
+        return rp.getOtherPages(is);
     }
 
     /**
index a7ca587..f028235 100644 (file)
@@ -5,6 +5,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import nicobrowser.entity.NicoContent;
@@ -14,6 +15,9 @@ import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
 import nicobrowser.entity.NicoContent.Status;
+import nicobrowser.search.SearchKind;
+import nicobrowser.search.SearchOrder;
+import nicobrowser.search.SearchResult;
 import org.apache.http.HttpException;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -218,15 +222,16 @@ public class NicoHttpClientTest {
         assertNull("公式動画からは著者は取得できない", result.getAuthor());
     }
 
+    @Test
     public void search() throws URISyntaxException, InterruptedException, HttpException, IOException {
         System.out.println("search");
         instance.login(OK_MAIL, OK_PASS);
 
-        List<NicoContent> conts = instance.search("初音ミク");
+        SearchResult result = instance.search("初音ミク", SearchKind.PlayTimes, SearchOrder.Descending, 1);
+        ArrayList<NicoContent> conts = result.getContents();
         System.out.println("検索結果件数: " + conts.size());
-        assertTrue(conts.size() > 0);
-        assertTrue(conts.size() > 100);
-
+        assertEquals("1ページの上限20件がヒット", 20, conts.size());
+        assertTrue(result.getPages().size() > 0);
     }
 
     public void loadMyMovie() throws URISyntaxException, InterruptedException, HttpException, IOException {
index 5faf326..65b7a52 100644 (file)
@@ -37,26 +37,26 @@ public class UtilTest {
         assertEquals("txt", result);
     }
 
-    /**
-     * Test of getNextPage method, of class Util.
-     */
-    @Test
-    public void testGetNextPage() throws FileNotFoundException, MalformedURLException {
-        System.out.println("getNextPage");
-
-        InputStream is;
-        String result;
-
-        // 次のページがある場合
-        is = new FileInputStream("test_data/momiji.html");
-        result = Util.getNextPage(is);
-        assertEquals("http://www.nicovideo.jp/search/%E7%B4%85%E8%91%89?page=2", result);
-
-        // 最終ページ
-        is = new FileInputStream("test_data/momiji_lastpage.html");
-        result = Util.getNextPage(is);
-        assertNull(result);
-    }
+//    /**
+//     * Test of getNextPage method, of class Util.
+//     */
+//    @Test
+//    public void testGetNextPage() throws FileNotFoundException, MalformedURLException {
+//        System.out.println("getNextPage");
+//
+//        InputStream is;
+//        String result;
+//
+//        // 次のページがある場合
+//        is = new FileInputStream("test_data/momiji.html");
+//        result = Util.getOtherPages(is);
+//        assertEquals("http://www.nicovideo.jp/search/%E7%B4%85%E8%91%89?page=2", result);
+//
+//        // 最終ページ
+//        is = new FileInputStream("test_data/momiji_lastpage.html");
+//        result = Util.getOtherPages(is);
+//        assertNull(result);
+//    }
 
     @Test
     public void testGetUserId() throws FileNotFoundException {