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;
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;
* @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;
}
/**
--- /dev/null
+/* $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;
+ }
+}
--- /dev/null
+/* $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;
+ }
+}
--- /dev/null
+/* $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;
+ }
+}
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
}
}
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;
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);
}
/**
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;
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;
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 {
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 {