【GAS】Google Apps Script 活用事例 noteのアクセスログを定期的に取得するスクリプト
こちらの記事を見て、noteもAPIを使って、アクセス記録を取得できるんだ!!と思って、自分でもやってみました!!
大体、週3000人、1ヶ月で1万PVを超える程度になりました。GASというマイナーな言語の割には、自分の想像以上に多くの人に読まれているのかなと思います。読まれているなという反応が、ブログでのアウトプットの動機につながっています。ありがとうございます!!
上記のブログは、再代入が不要にも関わらず、letを使っている事が多かったので、constに直して、header部分と、シートに記載する部分にスクリプトを分割しました。
下記の記事も参考になりました。
スクリプト実行後
ちょっと少ない.....ケドも。
noteの週間と全期間のアクセスログを取得してスプレッドシートに書き出すスクリプト
function setWeeklyAccessLogData(){
const sheetName = '週間アクセス数';
const requestUrl = 'https://note.com/api/v1/stats/pv?filter=weekly&page=1&sort=pv';
getAccessLogData_(sheetName, requestUrl);
}
function setTotalAccessLogData(){
const sheetName = '全期間アクセス数';
const requestUrl = 'https://note.com/api/v1/stats/pv?filter=all&page=1&sort=pv';
getAccessLogData_(sheetName, requestUrl);
}
function getAccessLogData_(sheetName, requestUrl) {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName(sheetName);
const emptyRow = sheet.getLastRow() + 1;
const date = new Date();
const stringDate = Utilities.formatDate(date, "JST", "yyyy/MM/dd");
console.log('最終行直下: ', emptyRow);
console.log(stringDate);
const headers = getHeaders_();
const statsOptions = {
'method' : 'get',
'headers': headers,
'contentType': 'application/json',
};
const response = UrlFetchApp.fetch(requestUrl, statsOptions);
const json = JSON.parse(response.getContentText());
console.log(json);
//note 読まれている記事などの情報を別シートに転記する。
if(sheetName === '週間アクセス数'){
const noteStats = json.data.note_stats;
console.log(noteStats);
allContentStats_(spreadsheet, noteStats, stringDate);
}
//今までの「スキ」やPVの合計,
const like = json.data.total_like;
const pv = json.data.total_pv;
//総アクセス数などを最終行に転記する。
const values = [[stringDate, pv, like]];
sheet.getRange(emptyRow, 1, values.length, values[0].length).setValues(values);
}
function allContentStats_(spreadsheet, notes, stringDate){
const sheet = spreadsheet.getSheetByName('コンテンツ分析');
const emptyRow = sheet.getLastRow() + 1;
let values = [];
for(const note of notes){
const url = 'https://note.com/nepia_infinity/n/' + note.key;
values.push([stringDate, note.name, note.read_count, note.like_count, url]);
}
console.log(values);
//ノートの統計内容の貼り付け
sheet.getRange(emptyRow, 1, values.length, values[0].length)
.setValues(values)
.setBorder(null, null, true, null, null, null, '#000000', SpreadsheetApp.BorderStyle.DOTTED);
}
function getHeaders_() {
const loginData = {
'follow': null,
'likable_id': null,
'login': 'noteのユーザーネーム',
'magazine_follow': null,
'password': 'noteのパスワード',
'redirect_path': null
};
const options = {
'method' : 'post',
'contentType': 'application/json;charset=UTF-8',
'payload' : JSON.stringify(loginData)
};
const response = UrlFetchApp.fetch('https://note.com/api/v1/sessions/sign_in',options);
const setCookie = response.getHeaders()["Set-Cookie"];
console.log('レスポンスコード:', response.getResponseCode());
console.log(response.getContentText());
const cookies = setCookie.split(';');
let session;
for(const cookie of cookies){
const tmpcookie = cookie.split('=');
if (tmpcookie[0] === '_note_session_v5'){
session = tmpcookie[1];
break;
}
}//for
const headers = {
'cookie': '_note_session_v5=' + session + ';',
};
return headers
}
noteの公式APIリファレンスは無い....のかもしれない。
公式リファレンスのようなページが見つけられず、こちらの記事を参考にしました。自分でも下記のように調べる事が出来ます。
こちらは、週間のPV数を調べる際のリクエストURLです。
ハッシュタグを調べる事も出来ます。
GoogleAppsScript、Python、イラスト、マンガに関係するハッシュタグを調べました。業務改善というハッシュタグが良い感じだったので、自分でも取り入れました。
記事数が多いほど、使うのに適切なハッシュタグかというと、そうでも無いようでした。例えば、エッセイなどポピュラーなハッシュタグは、スパムのような中身が無い投稿も含まれるため、ノイズが多い印象でした。
ハッシュタグを調べるスクリプト
function searchHashTags() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName('ハッシュタグ');
const emptyRow = sheet.getLastRow() + 1;
const date = new Date();
const stringDate = Utilities.formatDate(date, "JST", "yyyy/MM/dd");
console.log('最終行直下: ', emptyRow);
console.log(stringDate);
const headers = getHeaders_();
const statsOptions = {
'method' : 'get',
'headers': headers,
'contentType': 'application/json',
};
let values = [];
const words = ['GAS', 'Python', '業務改善', 'イラスト', 'アニメ'];
for(const word of words){
//調べたいハッシュタグをwordsの数だけ順番にリクエストする。
const requestUrl = 'https://note.com/api/v2/hashtags/' + word;
const response = UrlFetchApp.fetch(requestUrl, statsOptions);
const json = JSON.parse(response.getContentText());
//検索したハッシュタグと関連性があるハッシュタグ
const tags = json.data.relatedHashtags;
console.log(json);
console.log('tags', tags);
for(const tag of tags){
values.push([stringDate, tag.name, tag.count]);
}//for
}//for
//シートに書き出し、最下部に罫線を引く
sheet.getRange(emptyRow, 1, values.length, values[0].length)
.setValues(values)
.setBorder(null, null, true, null, null, null, '#000000', SpreadsheetApp.BorderStyle.DOTTED);
}
この記事が参加している募集
この記事が気に入ったらサポートをしてみませんか?