Google Apps ScriptでRSSを表示

5-4 Googleサイトで表示を統合しよう!

をベースにATOMとRSS1.0を表示・リンク追加

f:id:imabari_ehime:20181019174027p:plain

function doGet(e) {
  var temp = HtmlService.createTemplateFromFile('fetch');
  temp.title = 'Fetch';
  temp.msg = '愛媛県内自治体のRSS';
  return temp.evaluate().setTitle('Fetch');
}


function getRSS(n) {
  var jenre = '愛媛県';
  var url = 'http://www.pref.ehime.jp/kurashi/bosai/juyo/juyo_rss1.xml';
  var rss_type = 'rss10';
  var rss_encode = 'utf-8';

  switch (n) {
    case 1:
      jenre = '新居浜市';
      url = 'https://www.city.niihama.lg.jp/rss/10/list3.xml';
      break;
    case 2:
      jenre = '西条市';
      url = 'https://www.city.saijo.ehime.jp/rss/10/list3.xml';
      break;
    case 3:
      jenre = '上島町';
      url = 'https://www.town.kamijima.lg.jp/rss/10/list3.xml';
      break;
    case 4:
      jenre = '松山市';
      url = 'https://www.city.matsuyama.ehime.jp/kurashi/rss_news.xml';
      rss_type = 'rss20';
      break;
    case 5:
      jenre = '久万高原町';
      url = 'https://www.kumakogen.jp/rss/10/list3.xml';
      break;
    case 6:
      jenre = '砥部町';
      url = 'https://www.town.tobe.ehime.jp/rss/10/list3.xml';
      break;
    case 7:
      jenre = '宇和島市';
      url = 'https://www.city.uwajima.ehime.jp/rss/10/list3.xml';
      break;
    case 8:
      jenre = '宇和島市水道局';
      url = 'https://www.city.uwajima.ehime.jp/rss/10/site-43.xml';
      break;
    case 9:
      jenre = '八幡浜市';
      url = 'http://www.city.yawatahama.ehime.jp/shinchaku/index.rss';
      break;
    case 10:
      jenre = '大洲市';
      url = 'https://www.city.ozu.ehime.jp/rss/10/list3.xml';
      break;
    case 11:
      jenre = '西予市';
      url = 'https://script.google.com/macros/s/AKfycbwT2zNtha826t9W7HZl-yJ5veAKA2wAltzF2KXcQSEista8lgQ/exec';
      rss_type = 'rss20';
      break;
    case 12:
      jenre = '西予市社会福祉協議会';
      url = 'http://seiyo-syakyo.jp/files/content_type/type014/rss/content_3.xml';
      break;
    case 13:
      jenre = '内子町';
      url = 'https://www.town.uchiko.ehime.jp/rss/10/list3.xml';
      break;
    case 14:
      jenre = '伊方町';
      url = 'https://www.town.ikata.ehime.jp/rss/10/list3.xml';
      break;
    case 15:
      jenre = '松野町';
      url = 'https://www.town.matsuno.ehime.jp/rss/10/list3.xml';
      break;
    case 16:
      jenre = '鬼北町';
      url = 'https://www.town.kihoku.ehime.jp/rss/10/list3.xml';
      break;
    case 17:
      jenre = '愛南町';
      url = 'http://www.town.ainan.ehime.jp/kurashi/news.rss';
      rss_encode = 'sjis';
      break;
  }

  var xml;

  if (rss_encode == 'sjis') {
    xml = UrlFetchApp.fetch(url).getContentText("Shift_JIS");
  } else {
    xml = UrlFetchApp.fetch(url).getContentText();
  }

  var document = XmlService.parse(xml);
  var root = document.getRootElement();

  var content = '<tr><th>' + jenre + '</th></tr>';
  var rss_max = 10;

  switch (rss_type) {
    case 'atom':
      var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
      var values = root.getChildren('entry', atom);

      if (values.length < 10) {
        rss_max = values.length;
      }

      for (var i = 0; i < rss_max; i++) {
        var data = values[i];
        content += '<tr><td><a href="' + data.getChild('link', atom).getAttribute('href').getValue() + '">' + data.getChild('title', atom).getValue() + '</a></td></tr>';
      }
      break;

    case 'rss10':
      var rss = XmlService.getNamespace('http://purl.org/rss/1.0/');
      var values = root.getChildren('item', rss);

      if (values.length < 10) {
        rss_max = values.length;
      }

      for (var i = 0; i < rss_max; i++) {
        var data = values[i];
        content += '<tr><td><a href="' + data.getChild('link', rss).getValue() + '">' + data.getChild('title', rss).getValue() + '</a></td></tr>';
      }
      break;

    case 'rss20':
      var values = root.getChild('channel').getChildren('item');

      if (values.length < 10) {
        rss_max = values.length;
      }

      for (var i = 0; i < rss_max; i++) {
        var data = values[i];
        content += '<tr><td><a href="' + data.getChild('link').getValue() + '">' + data.getChild('title').getValue() + '</a></td></tr>';
      }
      break;
  }

  return content;
}

feed.htmlで保存

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>RSS Views</title>
 <script>
 function doAction(n){
   google.script.run.withSuccessHandler(onSuccess).getRSS(n);
 }
 function onSuccess(res){
   var table = document.getElementById("table");
   table.innerHTML = res;
 }
 </script>
</head>
<body onload="doAction(0);">
  <style>
    tr th { border:#aaa solid 1px; padding:5px;
         background-color:#999; color:white; }
    tr td { border:#aaa solid 1px; padding:5px 30px; }
  </style>
  <center>
    <p>
    <button onclick="doAction(0);">愛媛県</button><button onclick="doAction(1);">新居浜市</button><button onclick="doAction(2);">西条市</button><button onclick="doAction(3);">上島町</button><button onclick="doAction(4);">松山市</button><button onclick="doAction(5);">久万高原町</button><button onclick="doAction(6);">砥部町</button><button onclick="doAction(7);">宇和島市</button><button onclick="doAction(8);">宇和島市水道局</button><button onclick="doAction(9);">八幡浜市</button><button onclick="doAction(10);">大洲市</button><button onclick="doAction(11);">西予市</button><button onclick="doAction(12);">西予市社会福祉協議会</button><button onclick="doAction(13);">内子町</button><button onclick="doAction(14);">伊方町</button><button onclick="doAction(15);">松野町</button><button onclick="doAction(16);">鬼北町</button><button onclick="doAction(17);">愛南町</button>
    </p>
    <table id="table">
    </table>
    <div style="height:10px;"></div>
  </center>
</body>
</html>