👌

漫画の配信をdiscordで受け取る

2024/06/11に公開

はじめに

漫画の配信が最近RSS対応しているものがあるので、新しい漫画の配信があればdiscordで受け取る。

構成

シート

code

main.js
function rss(){
  getRssList().forEach(rss => rss.checkNewItem());
}
class/Rss.js
class Rss{
  constructor(row, index){
    this.rowIndex = index + SHEET.rss.row.data;
    this.url = row[SHEET.rss.column.url - 1];
    this.lastTitle = row[SHEET.rss.column.lastTitle - 1];

  }

  checkNewItem(){
    const xml = UrlFetchApp.fetch(this.url).getContentText();
    const document = XmlService.parse(xml);
    const item = document.getRootElement().getChild('channel').getChildren('item').shift();
    const lastTitle = item.getChild('title').getText();

    if(lastTitle === this.lastTitle) return;

    discord(DISCORD.webhook.notice,`RSS配信: ${item.getChild('link').getText()}`, true);
    setText(SHEET.rss, this.rowIndex, SHEET.rss.column.lastTitle, lastTitle);

  }
}

sheet.js
const SHEET = {
  rss: {
    name: 'rss',
    row : {
      data : 2, 
    },
    column : {
      url : 1,
      lastTitle : 2,
    },
  },
};

function getRssList(){
  return getSheetData(SHEET.rss).map((row, index) => new Rss(row, index));
}

function getSheetData(sheetConfig){
  let data = getSheetDataFull(sheetConfig);
  [...Array(sheetConfig.row.data - 1)].forEach(_ => data.shift());
  return data;
}

function getSheetDataFull(sheetConfig){
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetConfig.name);
  return sheet.getDataRange().getValues();
}

function setText(sheetConfig, row, column, text){
  setList(sheetConfig, row, column, [[text]]);
}

function setList(sheetConfig, row, column, list){
  if(!list.length) return;
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetConfig.name);
  sheet.getRange(row, column, list.length, list[0].length).setValues(list);
}

discord.js
const DISCORD = {
  id : '<自分のid>',
  webhook : {
    notice : '<webhook>',
  },
};

function discord(webhook, text, mentionFlg){

  const payload = {
    username: 'notice',
    content: (mentionFlg) ? `<@${DISCORD.id}>\n${text}` : text,
  };

  UrlFetchApp.fetch(
    webhook,
    {
      method: 'post',
      contentType: 'application/json',
      payload: JSON.stringify(payload),
    }
  );
}

結果

Discussion