GASでNiconicoのランキング取得


GASでNiconicoのランキングを取得してみた。

公式サイトは以下。

https://site.nicovideo.jp/search-api-docs/snapshot

作成したコード

function test(){
console.log(niconicoRankig("VOCALOID"));
}
const Webhook_URL = "https://snapshot.search.nicovideo.jp/api/v2/snapshot/video/contents/search";
function niconicoRankig(Search_Word) {
let params =
{
q: Search_Word,
targets: "tagsExact",
//タグで完全一致検索
fields: "contentId",
_sort: "-viewCounter",
//視聴回数でソート
_limit: 1,
_context: "アプリ名、メールアドレス、NiconicoのID",
};
let queryString = Object.keys(params)
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(params[key]))
.join("&");
let requestUrl = Webhook_URL + "?" + queryString;
let options = {
"method": "get",
"headers": {
"User-Agent": "アプリ名、メールアドレス、NiconicoのID"
},
};
var response = UrlFetchApp.fetch(requestUrl, options);
var json=JSON.parse(response.getContentText());
const video_URL="https://www.nicovideo.jp/watch/"+json.data[0].contentId;
return video_URL;
}

実行すると、ちゃんとhttps://www.nicovideo.jp/watch/sm15630734と出力された。

本当はデイリーの人気度ランキングも取得したかったのだが人気度は取得オプションになく、ランキングのRSS配信や過去ログファイルはサービス終了しているため苦戦中…。