From: yukihane Date: Wed, 10 Aug 2011 12:18:55 +0000 (+0900) Subject: watchページアクセス時に動画タイトルも取得しておく(inqubus用) X-Git-Tag: rel20110815_ver0.9.2~1^2~10 X-Git-Url: http://git.osdn.net/view?p=coroid%2FNicoBrowser.git;a=commitdiff_plain;h=4817d88b6f40b9e7761055be32e0dd2606b09c55 watchページアクセス時に動画タイトルも取得しておく(inqubus用) --- diff --git a/src/nicobrowser/NicoHttpClient.java b/src/nicobrowser/NicoHttpClient.java old mode 100644 new mode 100755 index ba579fd..97f1ac8 --- a/src/nicobrowser/NicoHttpClient.java +++ b/src/nicobrowser/NicoHttpClient.java @@ -513,7 +513,8 @@ public class NicoHttpClient { * @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")) { @@ -560,20 +561,51 @@ public class NicoHttpClient { } /** + * watchページコンテンツからタイトルを抽出する. + * @param content watchページコンテンツのストリーム. + */ + private String getTitleInWatchPage(InputStream content) throws IOException { + final String TITLE_PARSE_STR_START = ""; + 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; } /** @@ -582,22 +614,27 @@ public class NicoHttpClient { * @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); } /**