App StoreからランキングをスクレイピングしてSlackに配信(コード付き)
0
まず以下のようにセルに入れる
下にいくつでも追加可能です。
idの直前にアプリ名を入れます。
https://apps.apple.com/jp/app/hogefuga!/id000000000000

function main() {
var webHookUrl = "https://hooks.slack.com/services/hogehoge";
var spaces = "=".repeat(36);
var n = "\n";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheets()[2];
const lastRow = sh.getLastRow();
var rankingTexts = initRankingText();
if (lastRow > 1) {
for(let i = 2;i <= lastRow;i++) {
var url = sh.getRange(i,1).getDisplayValues().toString();
rankingTexts += spaces
+ n
+ sendRatingCount(url)
+ n;
}
}
rankingTexts += "\nアプリの追加はOOまで"
sendSlack(rankingTexts, webHookUrl);
}
function initRankingText() {
var now = new Date();
var date = Utilities.formatDate(now, 'Asia/Tokyo', 'yyyy年MM月dd日');
var text = ":crown: " + date +"のiOSアプリランキングをお知らせします :crown:\n";
return text
}
function sendRatingCount(url) {
var ratingCount = fetchRatingCount(url);
if (ratingCount == null) {
return;
}
const arr = url.split("/");
let spaceNum = 20 - get2byteLength(ratingCount);
return arr[arr.length-2]+ "\n" + " ".repeat(spaceNum) + ratingCount;
}
function fetchRatingCount(url) {
var response = UrlFetchApp.fetch(url);
var responseCode = response.getResponseCode();
if (responseCode != 200) {
return null;
}
var contentText = response.getContentText("utf-8");
var regexp = /<li class="inline-list__item">([\s\S]*?)<\/li>/;
try {
var ratingCount = contentText.match(regexp);
return ratingCount[1].replace(/[\s\t\n]/g,"");
} catch (e) {
return null;
}
}
function sendSlack(text, webHookUrl) {
var payload = {
"text" : text
};
var options = {
"method" : "post",
"muteHttpExceptions": true,
"payload" : JSON.stringify(payload)
};
UrlFetchApp.fetch(webHookUrl, options);
}
サンプル
2021年03月30日のiOSアプリランキングをお知らせします
==================================== あああああああああああ 「ヘルスケア/フィットネス」内184位 ==================================== dファファえわ 「教育」内12位 ==================================== ういうい 「教育」内1位 ==================================== あああ 「教育」内25位
0