/*$Id$*/
package nicobrowser;
+import nicobrowser.entity.NicoContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
-import com.sun.syndication.io.ParsingFeedException;
import com.sun.syndication.io.SyndFeedInput;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
+import java.net.URL;
+import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
"http://www.nicovideo.jp/mylist/";
private static final String MOVIE_THUMBNAIL_PAGE_HEADER =
"http://www.nicovideo.jp/api/getthumbinfo/";
+ private static final String GET_FLV_INFO = "http://www.nicovideo.jp/api/getflv?v=";
private NicoHttpClient() {
super();
}
return contList;
}
+
+ /**
+ * FLVファイルのURLを取得する. ログインが必要.
+ * また, 実際にFLVファイルの実態をダウンロードするには
+ * 一度http://www.nicovideo.jp/watch/ビデオIDに一度アクセスする必要があることに
+ * 注意.
+ * (参考: http://yusukebe.com/tech/archives/20070803/124356.html)
+ * @param videoID ニコニコ動画のビデオID.
+ * @return FLVファイル実体があるURL.
+ * @throws java.io.IOException
+ */
+ public URL getFlvUrl(String videoID) throws IOException {
+ String accessUrl = GET_FLV_INFO + videoID;
+ GetMethod get = new GetMethod(accessUrl);
+ String resultString;
+ try {
+ executeMethod(get);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), "UTF-8"));
+
+ String str;
+ StringBuilder strBuilder = new StringBuilder();
+ while ((str = reader.readLine()) != null) {
+ strBuilder.append(str);
+ }
+ resultString = strBuilder.toString();
+ } finally {
+ get.releaseConnection();
+ }
+
+ String[] urls = resultString.split("&");
+ final String marker = "url=";
+ for (String url : urls) {
+ System.out.println(url);
+ if (url.contains(marker)) {
+ String result = url.substring(marker.length());
+ result = URLDecoder.decode(result, "UTF-8");
+ return new URL(result);
+ }
+ }
+ throw new IOException("フォーマット仕様変更?");
+ }
+
+ void getFlvFile(String videoID) throws IOException {
+ byte[] buffer = new byte[1024 * 32];
+ final String watchUrl = "http://www.nicovideo.jp/watch/" + videoID;
+ GetMethod get = new GetMethod(watchUrl);
+ executeMethod(get);
+ get.releaseConnection();
+
+ URL url = getFlvUrl(videoID);
+
+ get = new GetMethod(url.toString());
+ executeMethod(get);
+ BufferedInputStream in = new BufferedInputStream(get.getResponseBodyAsStream());
+
+ //TODO ファイル保存場所を正しく.
+ FileOutputStream file = new FileOutputStream(new File("d:\\aaaccc.flv"));
+ BufferedOutputStream out = new BufferedOutputStream(file);
+
+ int i;
+ while ((i = in.read(buffer)) != -1) {
+ out.write(buffer, 0, i);
+ System.out.println(i);
+ }
+
+ out.close();
+ in.close();
+ }
}
/*$Id$*/
package nicobrowser;
+import java.io.IOException;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import nicobrowser.entity.NicoContent;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
}
transaction.commit();
- System.out.println("2つのPersonオブジェクトを永続化しました.");
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
}
}
+
+ @Test
+ public void getFlvUrl() {
+ System.out.println("getFlv");
+
+ NicoHttpClient instance = NicoHttpClient.getInstance();
+ instance.login(OK_MAIL, OK_PASS);
+ try {
+ URL str = instance.getFlvUrl("sm9");
+ System.out.println(str);
+ } catch (IOException ex) {
+ fail();
+ Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+ }
+
+ @Test
+ public void downLoad() {
+ System.out.println("downLoad");
+
+ NicoHttpClient instance = NicoHttpClient.getInstance();
+ instance.login(OK_MAIL, OK_PASS);
+
+ try {
+ instance.getFlvFile("sm9");
+ } catch (IOException ex) {
+ Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
+ fail();
+ }
+ }
}