* @throws java.io.IOException ファイル取得失敗. 権限の無いファイルを取得しようとした場合も.
*/
public VideoInfo getVideoInfo(String videoId) throws IOException {
- final String realVideoId = getRealVideoId(videoId);
+ final GetRealVideoIdResult res = getRealVideoId(videoId);
+ final String realVideoId = res.videoId;
String accessUrl = GET_FLV_INFO + realVideoId;
if (realVideoId.startsWith("nm")) {
}
/**
+ * watchページコンテンツからタイトルを抽出する.
+ * @param content watchページコンテンツのストリーム.
+ */
+ private String getTitleInWatchPage(InputStream content) throws IOException {
+ final String TITLE_PARSE_STR_START = "<title>";
+ BufferedReader br = new BufferedReader(new InputStreamReader(content, "UTF-8"));
+ String ret;
+ while ((ret = br.readLine()) != null) {
+ final int index = ret.indexOf(TITLE_PARSE_STR_START);
+ if (index >= 0) {
+ String videoTitle = ret.substring(index + TITLE_PARSE_STR_START.length(), ret.indexOf("‐", index));
+ return videoTitle;
+ }
+ }
+ return "";
+
+ }
+
+ private static class GetRealVideoIdResult {
+
+ private final String videoId;
+ private final String title;
+
+ private GetRealVideoIdResult(String videoId, String title) {
+ this.videoId = videoId;
+ this.title = title;
+ }
+ }
+
+ /**
* watchページへアクセスし, ビデオIDを取得する.
* soXXXXなど, 実際のビデオIDと異なる場合がある.
* @param videoId 取得したいビデオのビデオID.
* @return 実際のビデオID.
* @throws IOException
*/
- private String getRealVideoId(String videoId) throws IOException {
- String realId = accessWatchPage(videoId);
+ private GetRealVideoIdResult getRealVideoId(String videoId) throws IOException {
+ GetRealVideoIdResult res = accessWatchPage(videoId);
+ final String realId = res.videoId;
// ステータスコード302など、リダイレクトが必要な場合
if (!videoId.equals(realId)) {
- realId = getRealVideoId(realId);
+ res = getRealVideoId(realId);
}
- return realId;
+ return res;
}
/**
* @return
* @throws IOException
*/
- private String accessWatchPage(String videoId) throws IOException {
+ private GetRealVideoIdResult accessWatchPage(String videoId) throws IOException {
String realId = videoId;
+ String title;
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);
- if (response.containsHeader("Location")) {
- realId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+ try {
+ if (response.containsHeader("Location")) {
+ realId = response.getFirstHeader("Location").getValue().replace("/watch/", "");
+ }
+ title = getTitleInWatchPage(response.getEntity().getContent());
+ } finally {
+ response.getEntity().consumeContent();
}
- response.getEntity().consumeContent();
} finally {
http.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
}
- return realId;
+ return new GetRealVideoIdResult(realId, title);
}
/**