* @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 "";
-
+ return Util.getTitle(content);
}
private static class GetRealVideoIdResult {
private static final Logger logger = LoggerFactory.getLogger(Util.class);
static ResultParse rp = new ResultParse();
+ static WatchParse wp = new WatchParse();
static UserInfo ui = new UserInfo();
public static String getExtention(String contentType) {
return rp.parse(is);
}
- public static TreeMap<Integer,String> getOtherPages(InputStream is) {
+ public static TreeMap<Integer, String> getOtherPages(InputStream is) {
return rp.getOtherPages(is);
}
}
return userName;
}
+
+ public static String getTitle(InputStream watchPage) {
+ return wp.getTitle(watchPage);
+ }
}
--- /dev/null
+package nicobrowser.util
+
+import org.cyberneko.html.parsers.SAXParser
+
+/**
+ * watchページのパースを実行するクラス.
+ * ニコニコ動画(原宿)より前のバージョンでは動作未検証.
+ * @author yuki
+ */
+class WatchParse {
+ def slurper = new XmlSlurper(new SAXParser())
+ def headerTitlePattern = /^(.+) ‐ ニコニコ動画\(.+\)$/
+
+ String getTitle(InputStream is){
+ def html = slurper.parse(is)
+ def res = html.HEAD.TITLE
+ def headerTitle = res.text()
+ def matcher = (headerTitle =~ headerTitlePattern)
+ if(matcher){
+ return matcher[0][1]
+ }
+
+ // ニコニコ動画(Zero)用
+ def innerTitle = html.'**'.find {
+ it.@id == 'videoHeaderDetail'
+ }
+ return innerTitle.DIV.H2.text()
+ }
+}