From: yuki Date: Tue, 1 Dec 2009 05:51:43 +0000 (+0000) Subject: ユーザ名を"投稿者プロフィールへ"からではなく、"この動画を違反通報"から取得するようにする。 X-Git-Tag: rel20091201_ver0.4.0~10 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=188d666c579cf89c2fac49a60720ea9e76c236b2;p=coroid%2FNicoBrowser.git ユーザ名を"投稿者プロフィールへ"からではなく、"この動画を違反通報"から取得するようにする。 投稿者プロフィールを公開していないと、前者からでは取得できないため。 git-svn-id: http://192.168.11.7/svn/repository/NicoBrowser/trunk@259 bdf3b611-c98c-6041-8292-703d9c9adbe7 --- diff --git a/src/nicobrowser/NicoHttpClient.java b/src/nicobrowser/NicoHttpClient.java index aa027a0..f4fc746 100644 --- a/src/nicobrowser/NicoHttpClient.java +++ b/src/nicobrowser/NicoHttpClient.java @@ -547,15 +547,13 @@ public class NicoHttpClient extends DefaultHttpClient { } getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); - final String userId = Util.getUserId(response.getEntity().getContent()); - log.debug("userId: " + userId); + final URL notifierUrl = Util.getNotifierUrl(response.getEntity().getContent()); + log.debug("違反通報ページ: " + notifierUrl); response.getEntity().consumeContent(); String userName = null; - if (userId != null) { - final String userUrl = "http://www.nicovideo.jp/user/" + userId; - log.debug("アクセス: " + watchUrl); - get = new HttpGet(userUrl); + if (notifierUrl != null) { + get = new HttpGet(notifierUrl.toString()); response = execute(get); userName = Util.getUserName(response.getEntity().getContent()); response.getEntity().consumeContent(); diff --git a/src/nicobrowser/util/UserInfo.groovy b/src/nicobrowser/util/UserInfo.groovy index 19c98cc..76cefe7 100644 --- a/src/nicobrowser/util/UserInfo.groovy +++ b/src/nicobrowser/util/UserInfo.groovy @@ -1,16 +1,13 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - +/* $Id$ */ package nicobrowser.util import org.cyberneko.html.parsers.SAXParser -/** - * - * @author yuki - */ class UserInfo { + /** + * 動画閲覧ページからユーザIDを取得する. + * @params is 動画閲覧ページ. + * @return ユーザID. 取得できなければnull. + */ String getUserId(InputStream is){ def html = new XmlSlurper(new SAXParser()).parse(is) def res = html.'**'.find{it.attributes()['href']?.startsWith '/user/'} @@ -21,12 +18,20 @@ class UserInfo { return res.attributes()['href'].replaceAll('/user/','').toString() } + String getNotifierUrl(InputStream is){ + def html = new XmlSlurper(new SAXParser()).parse(is) + def res = html.'**'.find{it.text() == 'この動画を違反通報'} + + // 公式動画(IDがsoで始まる動画)のように投稿者が無い場合がある + if(res==null) return null + + return res.attributes()['href'].toString() + } + String getUserName(InputStream is){ def html = new XmlSlurper(new SAXParser()).parse(is) - def title = html.'HEAD'.'TITLE'.text() - def i = title.indexOf('さんのユーザーページ') - if(i<=0) return null - return title.substring(0,i) + def statement = html.'BODY'.'DIV'.'P'.find{it.text().contains('が投稿した動画を、違反動画として通報します。')}//'STRONG'.text().toString() + statement.'STRONG'.toString() } } diff --git a/src/nicobrowser/util/Util.java b/src/nicobrowser/util/Util.java index d74edba..c313202 100644 --- a/src/nicobrowser/util/Util.java +++ b/src/nicobrowser/util/Util.java @@ -2,7 +2,11 @@ package nicobrowser.util; import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; public class Util { @@ -31,16 +35,35 @@ public class Util { /** * ユーザIDを取得する * @param is ニコニコ動画のウォッチページストリーム. - * @return ユーザID. + * @return ユーザID. 取得できなければnull. */ public static String getUserId(InputStream is) { return ui.getUserId(is); } /** + * 違反通報ページURLを取得する. + * @param is ニコニコ動画のウォッチページストリーム. + * @return 違反通報ページURL. 取得できなければnull. + */ + public static URL getNotifierUrl(InputStream is) { + String res = ui.getNotifierUrl(is); + if (res == null) { + return null; + } + + try { + return new URL(res); + } catch (MalformedURLException ex) { + Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + } + + /** * ユーザ名を取得する. - * @param is ユーザページのストリーム. - * @return ユーザ名. + * @param is 違反通報ページストリーム. + * @return ユーザ名. 取得できなければnull. */ public static String getUserName(InputStream is) { return ui.getUserName(is); diff --git a/test/nicobrowser/util/UtilTest.java b/test/nicobrowser/util/UtilTest.java index 59438d0..5faf326 100644 --- a/test/nicobrowser/util/UtilTest.java +++ b/test/nicobrowser/util/UtilTest.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URL; import org.junit.Test; import static org.junit.Assert.*; @@ -66,8 +67,18 @@ public class UtilTest { @Test public void testGetUserName() throws FileNotFoundException { - InputStream is = new FileInputStream("testdata/user_336619.html"); + InputStream is = new FileInputStream("testdata/notifier.html"); String name = Util.getUserName(is); - assertEquals("ZAKO2012", name); + assertEquals("nakano", name); + } + + /** + * Test of getNotifierUrl method, of class Util. + */ + @Test + public void testGetNotifierUrl() throws FileNotFoundException { + InputStream is = new FileInputStream("testdata/sm8769630.html"); + URL url = Util.getNotifierUrl(is); + assertEquals("http://www.smilevideo.jp/view/8769630/335912", url.toString()); } } diff --git a/testdata/notifier.html b/testdata/notifier.html new file mode 100644 index 0000000..5758a1a --- /dev/null +++ b/testdata/notifier.html @@ -0,0 +1,98 @@ + + + + + + + +SMILEVIDEO | 違反動画の通報 + + + + + + + + + + + + + +

SMILEVIDEO

Version

+
+

違反動画の通報

+
+ +
+

nakano が投稿した動画を、違反動画として通報します。

+
+ +
+
+ +

動画ID:9

+

+

新・豪血寺一族 -煩悩解放 - レッツゴー!陰陽師

+

00:05:20

+

レッツゴー!陰陽師(フルコーラスバージョン)

+ +
+
+ +
+

通報する場合は、ニコニコ動画アカウントでログインしてください。

+
+ +
+
+
+ + + + + + + + + + + + + + +
メールアドレス:
パスワード:
+
+
+
+
+ + + +
+

+無料ツールで動画を作成動画の素材を検索

+
+ +
+

ヘルプ 利用規約 ライツコントロールプログラムのご案内 動画投稿ハンドブック

+
+ + + + + + + + + + +
worldwide:
+ +
(C) niwango, inc. All rights reserved.
+ + + + + \ No newline at end of file