import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
-import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.regex.Pattern;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
"https://secure.nicovideo.jp/secure/login?site=niconico";
private static final String LOGOUT_PAGE =
"https://secure.nicovideo.jp/secure/logout";
+ private static final String WATCH_PAGE = "http://www.nicovideo.jp/watch/";
private static final String MY_LIST_PAGE_HEADER =
"http://www.nicovideo.jp/mylist/";
private static final String MOVIE_THUMBNAIL_PAGE_HEADER =
* 一度http://www.nicovideo.jp/watch/ビデオIDに一度アクセスする必要があることに
* 注意.
* (参考: http://yusukebe.com/tech/archives/20070803/124356.html)
- * @param videoID ニコニコ動画のビデオID.
+ * @param videoId ニコニコ動画のビデオID.
* @return FLVファイル実体があるURL.
* @throws java.io.IOException ファイル取得失敗. 権限の無いファイルを取得しようとした場合も.
*/
- public URL getFlvUrl(String videoID) throws IOException {
- String accessUrl = GET_FLV_INFO + videoID;
- if (videoID.startsWith("nm")) {
+ public VideoInfo getVideoInfo(String videoId) throws IOException {
+ final String realVideoId = getRealVideoId(videoId);
+
+ String accessUrl = GET_FLV_INFO + realVideoId;
+ if (realVideoId.startsWith("nm")) {
accessUrl += "?as3=1";
}
log.debug("アクセス: " + accessUrl);
}
}
- String[] urls = resultString.split("&");
- final String marker = "url=";
- for (String url : urls) {
- if (url.contains(marker)) {
- String result = url.substring(marker.length());
- result = URLDecoder.decode(result, "UTF-8");
+ String[] params = resultString.split("&");
+// final String marker = "url=";
+ Map<String, String> map = new HashMap<String, String>();
+ for (String param : params) {
+ String[] elm = param.split("=");
+ map.put(elm[0], elm[1]);
+ // if (url.contains(marker)) {
+ // String result = url.substring(marker.length());
+ // result = URLDecoder.decode(result, "UTF-8");
+ //
+ // return new URL(result);
+ // }
+ }
+// throw new IOException("ダウンロードに失敗しました(削除済みの可能性もあります)。 ID: " + videoID + ", パラメータ:" + resultString);
+ return new VideoInfo(realVideoId, map);
+ }
- return new URL(result);
+ private String getRealVideoId(String videoId) throws IOException {
+ String watchUrl = WATCH_PAGE + videoId;
+ log.debug("アクセス: " + watchUrl);
+ http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
+ try {
+ HttpGet get = new HttpGet(watchUrl);
+ HttpResponse response = http.execute(get);
+ String realID = videoId;
+
+ // ステータスコード302など、リダイレクトが必要な場合
+ if (response.containsHeader("Location")) {
+ realID = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+ response.getEntity().consumeContent();
+ watchUrl = WATCH_PAGE + realID;
+ log.debug("アクセス: " + watchUrl);
+ HttpGet watchGet = new HttpGet(watchUrl);
+ HttpResponse watchResponse = http.execute(get);
+ watchResponse.getEntity().consumeContent();
+ } else {
+ response.getEntity().consumeContent();
}
+ return realID;
+ } finally {
+ http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
}
- throw new IOException("ダウンロードに失敗しました(削除済みの可能性もあります)。 ID: " + videoID + ", パラメータ:" + resultString);
}
/**
* ニコニコ動画から動画ファイルをダウンロードする.
- * @param videoID smxxxx形式のビデオID.
+ * @param videoId smxxxx形式のビデオID.
* @param fileName ダウンロード後のファイル名. 拡張子は別途付与されるため不要.
* @param nowStatus ダウンロードしようとしている動画ファイルの, 現在のステータス.
* @param needLowFile エコノミー動画をダウンロードするのであればtrue.
public GetFlvResult getFlvFile(String videoID, String fileName, Status nowStatus, boolean needLowFile) throws
IOException,
URISyntaxException, HttpException, InterruptedException {
- String watchUrl = "http://www.nicovideo.jp/watch/" + videoID;
- log.debug("アクセス: " + watchUrl);
- http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
- HttpGet get = new HttpGet(watchUrl);
- HttpResponse response = http.execute(get);
- String realID = videoID;
-
- // ステータスコード302など、リダイレクトが必要な場合
- if (response.containsHeader("Location")) {
- realID = response.getFirstHeader("Location").getValue().replace("/watch/", "");
- response.getEntity().consumeContent();
- watchUrl = "http://www.nicovideo.jp/watch/" + realID;
- log.debug("アクセス: " + watchUrl);
- get = new HttpGet(watchUrl);
- response = http.execute(get);
- }
- http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
+ VideoInfo vi = getVideoInfo(videoID);
- final URL notifierUrl = Util.getNotifierUrl(response.getEntity().getContent());
- log.debug("違反通報ページ: " + notifierUrl);
- response.getEntity().consumeContent();
+ final URL notifierUrl = vi.getSmileUrl();
String userName = null;
if (notifierUrl != null) {
- get = new HttpGet(notifierUrl.toString());
- response = http.execute(get);
+ HttpGet get = new HttpGet(notifierUrl.toString());
+ HttpResponse response = http.execute(get);
userName = Util.getUserName(response.getEntity().getContent());
response.getEntity().consumeContent();
}
- URL url = getFlvUrl(realID);
+ final URL url = vi.getVideoUrl();
if (nowStatus == Status.GET_LOW || !needLowFile) {
if (url.toString().contains("low")) {
log.info("エコノミー動画のためスキップ: " + videoID);
}
}
- get = new HttpGet(url.toURI());
- response = http.execute(get);
+ HttpGet get = new HttpGet(url.toURI());
+ HttpResponse response = http.execute(get);
String contentType = response.getEntity().getContentType().getValue();
log.debug(contentType);
log.debug(fileName);
/**
* ニコニコ動画から動画ファイルをダウンロードする.
- * @param videoID smxxxx形式のビデオID.
+ * @param videoId smxxxx形式のビデオID.
* @param fileName ダウンロード後のファイル名. 拡張子は別途付与されるため不要.
* @return この処理を行った後の, 対象ファイルのステータス.
* @throws java.io.IOException ファイル取得失敗. 権限の無いファイルを取得しようとした場合も.
/**
* ニコニコ動画から動画ファイルをダウンロードする.
* ファイル名はビデオID名となる.
- * @param videoID smxxxx形式のビデオID.
+ * @param videoId smxxxx形式のビデオID.
* @return この処理を行った後の, 対象ファイルのステータス.
* @throws java.io.IOException ファイル取得失敗. 権限の無いファイルを取得しようとした場合も.
*/
--- /dev/null
+/* $Id$ */
+package nicobrowser;
+
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.Map;
+
+/**
+ *
+ * @author yuki
+ */
+public class VideoInfo {
+
+ private final String realVideoId;
+ private final String threadId;
+ private final URL videoUrl;
+ private final URL messageUrl;
+ private final URL smileUrl;
+// private final String userId;
+ private final int videoLength;
+ private static final String KEY_THREAD_ID = "thread_id";
+ private static final String KEY_URL = "url";
+ private static final String KEY_MESSAGE_URL = "ms";
+ private static final String KEY_SMILE_LINK = "link";
+ private static final String KEY_VIDEO_LENGTH = "l";
+
+ public VideoInfo(String realVideoId, Map<String, String> getflvReceivedMap) {
+ try {
+ this.realVideoId = realVideoId;
+ threadId = getflvReceivedMap.get(KEY_THREAD_ID);
+ videoUrl = new URL(URLDecoder.decode(getflvReceivedMap.get(KEY_URL), "UTF-8"));
+ messageUrl = new URL(URLDecoder.decode(getflvReceivedMap.get(KEY_MESSAGE_URL), "UTF-8"));
+ smileUrl = new URL(URLDecoder.decode(getflvReceivedMap.get(KEY_SMILE_LINK), "UTF-8"));
+ videoLength = Integer.parseInt(getflvReceivedMap.get(KEY_VIDEO_LENGTH));
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public String getRealVideoId() {
+ return realVideoId;
+ }
+
+ public URL getMessageUrl() {
+ return messageUrl;
+ }
+
+ public URL getSmileUrl() {
+ return smileUrl;
+ }
+
+ public String getThreadId() {
+ return threadId;
+ }
+
+ public int getVideoLength() {
+ return videoLength;
+ }
+
+ public URL getVideoUrl() {
+ return videoUrl;
+ }
+}
/**
* Test of login method, of class NicoHttpClient.
*/
+ @Test
public void login() throws HttpException, URISyntaxException, InterruptedException {
System.out.println("login");
assertEquals(true, result);
}
+ @Test
public void loadMyList() throws URISyntaxException, HttpException, InterruptedException {
System.out.println("loadMyList");
}
- public void getFlvUrl() throws URISyntaxException, HttpException, InterruptedException, IOException {
+ @Test
+ public void getVideoInfo() throws URISyntaxException, HttpException, InterruptedException, IOException {
System.out.println("getFlv");
instance.login(OK_MAIL, OK_PASS);
try {
- URL str = instance.getFlvUrl("sm1359820");
+ URL str = instance.getVideoInfo("sm1359820").getVideoUrl();
System.out.println(str);
} catch (IOException ex) {
fail();
}
@Test
+ public void getVideoInfo_Official() throws URISyntaxException, HttpException, InterruptedException, IOException {
+ System.out.println("getVideoInfo_Official");
+ instance.login(OK_MAIL, OK_MAIL);
+ final String OFFICIAL_VIDEO = "so8799877";
+ VideoInfo vi = instance.getVideoInfo(OFFICIAL_VIDEO);
+ assertNotSame(OFFICIAL_VIDEO, vi.getRealVideoId());
+ }
+
+ @Test
public void downLoad() throws URISyntaxException, IOException, HttpException, InterruptedException {
System.out.println("downLoad");