// BOMを読み捨て
// reader.skip(1);
list = getNicoContents(reader);
+ deleteRankString(list);
response.getEntity().consumeContent();
} catch (FeedException ex) {
log.error("", ex);
}
/**
+ * ニコニコ動画の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);
}
/**
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();
@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();
}
descFlag = true;
}
}
-// System.out.println("--------<" + t.toString() + ">--------");
-// printAttributes(a);
+ log.debug("--------<" + t.toString() + ">--------");
+ log.debug(a);
}
@Override
if (HTML.Tag.P.equals(t)) {
descFlag = false;
}
-// System.out.println("--------</" + t.toString() + ">--------");
+ log.debug("--------</" + t.toString() + ">--------");
}
@Override
if (descFlag) {
description.append(data);
}
-
-// System.out.println("--------TEXT--------");
-// System.out.println(data);
+ log.debug("--------TEXT--------");
+ log.debug(data);
}
private void printAttributes(MutableAttributeSet a) {
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());
// サムネイル画像リンクと説明文の取得
--- /dev/null
+/*
+ * 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());
+ }
+ }
+}