0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【GAS】ニコニコ動画の広告者を取得する方法【ニコメア】

Last updated at Posted at 2023-02-01

はじめに

前回の記事

ではニコニコ動画のコメントを取得する方法を紹介しましたが、今回はニコニコ動画で自分が投稿した動画に対して広告者に感謝するための広告者を取得する方法を説明します。

なお筆者はGoogle Apps Script(以降GAS)初心者です。

広告取得処理

広告を取得する処理はシートのクリア、動画IDの取得、スレッドIDの取得、広告の取得の4種類で構成しています。
スレッドは使っていませんが動画情報表示のため呼び出しています。

getThanks.js
function getThanks(){
  sheetInfoClear()
  sheetThanksClear();
  let id = getId();
  let threadId= HttpInfo(id);
  HttpThanks(id);
  thanksToFormat();
}

シートのクリア

前回の記事と同様です、消去範囲が異なります。

sheetThanksClear.js
function sheetThanksClear(){
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('広告');
  let y=5;
  const lastRow = sheet.getLastRow();
  sheet.getRange(y, 2, lastRow, 8).clearContent();  
  sheet.getRange('B2').clearContent();
}

広告情報の取得

使用するシート、呼び出すurl、取得したデータの処理が異なります。
広告者情報は情報数が返ってきますので**let cnt = data["count"];**で取得してループ回数に使用します。

HttpThanks.js
// 指定したIDから広告を取得し表示 
function HttpThanks(id){
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('広告');
  url = "https://api.nicoad.nicovideo.jp/v1/contents/video/";
  url = url + id;
  url = url + "/histories?limit=1000";
  const docText = UrlFetchApp.fetch(url).getContentText();
  let json = JSON.parse(docText);
  let data = json["data"];
  let cnt = data["count"];
  let hists = data["histories"];
  let oValues = [[]];
  oValues.length = 0;
  let k = 0;
  for(let j=0;j<cnt-1;j++){
    let hist = hists[j];
    if (hist === undefined) {
      break;
    } 
    let userId=hist["userId"]; 
    let startedAt= dateFromSn(hist["startedAt"]);
    msg = hist["message"];
    if( msg === undefined){
       msg = "";
    }
    let i = indexOfUserId2(oValues,userId);
    if(i === -1){         
       oValues.push([hist["advertiserName"],1,hist["adPoint"],hist["contribution"],startedAt,msg,userId]);
     }
     else{
       oValues[i][1]++;
       oValues[i][2] += hist["adPoint"];
       if (oValues[i][2] === ""){ oValues[i][2] = msg}
     }
  }
  sheet.getRange(5, 3, oValues.length, 7).setValues(oValues);
}

広告者の重複処理

同じ広告者が複数回広告をすることがありますが、重複している情報はまとめます。
まずはuserIdが同じものがあるかを解析します。
重複していない場合は-1 それ以外はそのインデックス値を返します。
新規の場合は広告回数として1を代入、広告費をそのまま代入し重複している場合は広告回数をカウントし広告費をその分加算します。
メッセージは空白ではない方を採用しています。

indexOfUserId2.js
function indexOfUserId2(myArray,id){
  let i=0;
  for(i=0;i<myArray.length;i++){
    let v = myArray[i][6];
    if(v === id){
      return i;
      break;
    }     
  }
  return -1;
}

専用の書式指定で情報をまとめる

広告者の書式を指定することで表示を1つの文字列にまとめます。

縦方向の他に横方向の表示も作成します。
ソートしてから反映することも考慮してシートのセルに入力済みの値から解析を行います。

thanksToFormat.js
function thanksToFormat(){
  let i=0;
  const sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('情報');
  let fmt = sheet2.getRange(3, 4).getValue();
  let cut = sheet2.getRange(4, 4).getValue();
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('広告');
  let ss="";
  cnt = sheet.getLastRow() - 5;
  const iValues = sheet.getRange(5, 3, cnt, 8).getValues();
  let oValues = [[]];
  oValues.length = 0;  
  for(let i = 0;i < cnt ; i++){
    let advertiserName = iValues[i][0];
    let adCnt          = iValues[i][1];
    let adPoint        = iValues[i][2];
    let contribution   = iValues[i][3];
    let startedAt      = iValues[i][4];
    let message        = iValues[i][5];
    let userId         = iValues[i][6];    
      let s = fmt;
    s = s.replace("%n", advertiserName);
    s = s.replace("%m", message);
    s = s.replace("%c", adCnt);
    s = s.replace("%i", userId);
    s = s.replace("%p", adPoint);
    s = s.replace("%s", startedAt);
    s = s.replace("%f", contribution);
    oValues.push([s]);
    if(i > 0){
      ss = ss + cut;
    }
    ss = ss + s;
  }
  sheet.getRange(5, 2, oValues.length, 1).setValues(oValues);
  sheet.getRange(2, 2).setValue(ss);
}

最後に

いかがだったでしょうか?
次回は使用素材(コンテンツ)を取得する方法を紹介します。

上記方法で作成したニコメアのDLはこちら

ニコメア

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?