DTPab

印刷やデザイン、アドビ製アプリやスクリプトなど、雑多な技術ブログ

VS Codeの拡張機能の更新を監視して変更があったらSlackに通知するGASを書いた

はじめに

DTPスクリプト書きからすると、これがなきゃ困るというVS Codeの拡張機能、ExtendScript Debugger for VS Codeが今年6月に大きくバージョンアップされたんですが、拡張機能の更新なのでしれっと行われるわけですよね。
v2.0.2からv2.0.3がリリースされたときもそう。もちろんv2リリースの際にはAdobeも大々的にTech Blogやフォーラムで宣伝してくれたわけですが。

どうにかしてスクレイピングして、通知を受け取りたいなと思ってGASで書いてみました。効果の程は実際に更新されてからのお楽しみということで。
もくもく会のSlackにある、#vscodeチャンネルにその更新が届くようにしました。

コード

GASのコードはこんな感じです。

function scraping() {
  const URL = "https://marketplace.visualstudio.com/items?itemName=Adobe.extendscript-debug";
  const response = UrlFetchApp.fetch(URL);
  const html = response.getContentText("UTF-8");

  const re = {
    lastUpdate: /"LastUpdatedDateString":"(.+?)"/,
    version: /"Version":"(.+?)"/
  };
  const postURL = "hogehoge"; // 実際にはwebhookのURLが入ります

  // 調査当日の0時を設定
  const today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);

  // 更新日の取得
  const jstDateString = new Date(html.match(re.lastUpdate)[1]);

  // 差の計算
  const diff = Math.floor((today.getTime() - jstDateString.getTime()) / 8.64e+7) + 1;
  Logger.log(`today:${today}, lastUpdate:${jstDateString}, diff:${diff}`);
  if (diff !== 1) { return; }

  // 更新日翌日であればデータを加工する
  const dateString = `${jstDateString.getFullYear()}${jstDateString.getMonth()+1}${jstDateString.getDate()}${jstDateString.getHours()}${jstDateString.getMinutes()}分`
  const message = `ESDebuggerが更新されました!\r最終更新: ${dateString}\rバージョン: ${html.match(re.version)[1]}`;
  const userName = "ESDebugger更新監視bot";
  const icon = ":white_check_mark:";
  const jsonData = {
   "username" : userName,
   "icon_emoji" : icon,
   "text" : message,
  };
  const payload = JSON.stringify(jsonData);
  const options = {
    "method": "post",
    "contentType": "application/json",
    "payload" : payload,
  };
  UrlFetchApp.fetch(postURL, options);
}

つまづいたところ・勉強になったところ

  1. String.match()メソッドの引数の正規表現オブジェクトにgフラグをつけるとキャプチャグループが動作しないことを完全に失念していた
  2. Googleでミリ秒 日 換算などと検索するとかんたんに換算できた(1日が何ミリ秒かすぐ分かった。Google天才すぎ)

ほかはそんなにつまづくところはなかったかな。日付の計算をどうしようかと思って時間がかかったくらい。

参考文献