Yahoo!ニュースのコメントのデータを、JavaScriptから取得する方法について解説します。
コメント機能の仕様変更にともない、2013/8/27以降この方法は使用できなくなる恐れがあります。
これはWeb APIとして提供されている機能ではありません。
次のURLにパラメータを付加してアクセスすると、JSONP形式でデータが返されます。
http://capi1.cpf.yahoo.co.jp/camp/v1/list/
パラメータ | 説明 |
---|---|
prop | |
uri | ニュース記事のURI (記事全文のページのURI) |
enc | 文字エンコード |
date | |
length | 取得数 |
offset | 取得開始のインデックス |
sort | 並べ替え方法
|
order | 並べ替え順
|
emj |
取得データはlisthandler()関数の引数として渡されますので、あらかじめlisthandlerという名前の関数を定義しておきます。
function listhandler( r ) { if( r.item ) { // データを処理する } else { // データの取得に失敗 } }
たとえば次のようにリクエストした場合、
http://capi1.cpf.yahoo.co.jp/camp/v1/list/ ?prop=1 &uri=http%3A%2F%2Fheadlines.yahoo.co.jp%2Fhl%3Fa%3D20110304-00000311-soccerk-socc &enc=euc-jp &date=21 &length=3 &offset=0 &sort=created_at &order=desc
レスポンスは以下のようになります。
listhandler( { "total": "963", "post_rate": "48.471121", "total_unique_yid": "879", "crm": "t=VGZcNB&sk=WPmOj3mIx59w1uNRQ4s0DSMUrkk-", "total_results_returned": 3, "item": [ { "id": "31160121", "seq_num": "1055", "masked_yid": "aco*****", "hashed_yid": "zfz5Mg5q5XicurGOj5joe5w-", "ptime": "2011年3月5日 10時12分", "user": "匿名", "points": "0", "minus_points": "0", "sum_points": "0", "lost_points": "0", "user_ext": "15000000", "user_ext2": "0", "device": "mobile", "body": "いい動きを見せてもなかなか納得しないんだよビッグクラブはさ" }, { "id": "31160042", "seq_num": "1054", "masked_yid": "red*****", "hashed_yid": "Q3FIlR1s7n4iQuERSetuePM-", "ptime": "2011年3月5日 10時8分", "user": "匿名", "points": "0", "minus_points": "0", "sum_points": "0", "lost_points": "0", "user_ext": "15000000", "user_ext2": "0", "device": "mobile", "body": "踏ん張れ!" }, { "id": "31159677", "seq_num": "1053", "masked_yid": "ken*****", "hashed_yid": "eprDMwRs5H4hZBPSKm5H_9T0nCs-", "ptime": "2011年3月5日 9時51分", "user": "匿名", "points": "1", "minus_points": "0", "sum_points": "1", "lost_points": "1", "user_ext": "15000000", "user_ext2": "0", "device": "pc", "body": "長友には期待しているし、見たいけど、BSのセリエAで毎週インテルっていうのは・・・。※整形済み
listhandler()関数の引数のオブジェクトは、次のようなプロパティを保ちます。
キー | 内容 | ||
---|---|---|---|
total | コメント数 | ||
total_unique_yid | 投稿人数 | ||
total_results_returned | 取得された結果数 | ||
post_rate | 平均投稿数 [件/時] | ||
error | エラーメッセージ (エラー発生時のみ) | ||
crm | |||
item [配列] |
body | コメント本文 | |
id | コメントID | ||
seq_num | 通し番号 | ||
ptime | 投稿日時 | ||
points | 「そう思う」の数 | 得点 | |
minus_points | 「そう思わない」の数 | ||
sum_points | 「そう思う」 + 「そう思わない」 | ||
lost_points | 「そう思う」 - 「そう思わない」 | ||
masked_yid | Yahoo! ID (最初の3文字のみ) | ユーザー | |
hashed_yid | Yahoo! IDのハッシュ | ||
user | ユーザーの種類 ('匿名') | ||
user_ext | |||
user_ext2 | |||
device | 使用端末 ('pc'または'mobile') |
レスポンスのhashed_yidから、ユーザーのニックネームを取得できます。
次のURLに取得対象のユーザーIDを渡すことで、それらのニックネームが返されます。
http://scache.news.yahoo.co.jp/comment/getNickname
具体的には次のように、hashed_yidsパラメータを必要数だけ連続して記述します。
http://scache.news.yahoo.co.jp/comment/getNickname ?hashed_yids=aaa &hashed_yids=bbb
レスポンスはnicknamehandler()関数の引数で渡されるため、あらかじめnicknamehandler()関数を定義しておきます。
レスポンスは、パラメータで渡したユーザーIDを名前とするオブジェクトで返されます。
キー | 内容 |
---|---|
truncated_yid | Yahoo! ID (最初の3文字のみ) |
nickname | ニックネーム |
次のようにリクエストした場合、
http://scache.news.yahoo.co.jp/comment/getNickname ?hashed_yids=zfz5Mg5q5XicurGOj5joe5w- &hashed_yids=Q3FIlR1s7n4iQuERSetuePM-
レスポンスは以下のようになります。
nicknamehandler( { "data": { "zfz5Mg5q5XicurGOj5joe5w-": { "truncated_yid":"aco...", "nickname":"DREAMIN" }, "Q3FIlR1s7n4iQuERSetuePM-": { "truncated_yid":"red...", "nickname":"red**x0128" } } } )※整形済み