📜 ⬆️ ⬇️

Create your RSS feed using Google Apps Script

There is such a wonderful wiki Tcl / Tk resource like wiki.tcl.tk. This resource has an RSS feed of the latest changes. But the trouble is the tape of changes is the most minimal. It indicates only by whom, when and which page was edited. There are no complete changes and no tape GUID , therefore some RSS clients (for example, Google Reader) do not show most of the news, considering them to be the same.

To convert the tape into its own convenient format, the choice fell on such a specialized tool as Yahoo Pipes . But then there was a failure. To get changes to the page, the wiki strictly required the presence of a cookie named “wikit_e” in the http request, and it was impossible to teach Yahoo Pipes to send cookies. The focus with the HTTP GET request and the parameter in the form "? COOKIE =" also did not pass.
As a result, the choice fell on a more flexible tool - Google Apps Script . The result is this script:
//     ,     - function doGet() { //  URL  RSS  var feed = 'http://wiki.tcl.tk/rss.xml'; //  ID     var id = Utilities.base64Encode(feed); //           var cache = CacheService.getPublicCache(); var rss = cache.get(id); //       -     if (rss == null) { //   RSS    XML var data = UrlFetchApp.fetch(feed).getContentText(); var doc = Xml.parse(data); //   var channel = doc.getElement().getElement("channel"); //   , ,   var title = channel.getElement("title").getText(); var link = channel.getElement("link").getText(); var desc = channel.getElement("description").getText(); var guid, date; //    RSS ,  ,    rss = '<rss version="2.0">'; rss += "<channel>" rss += "<title>" + title + "</title>\n"; rss += "<link>" + link + "</link>\n"; rss += "<description>" + desc + "</description>\n"; //          var items = channel.getElements("item"); for (var i in items) { item = items[i]; //    , ,  ,  title = item.getElement("title").getText(); link = item.getElement("link").getText(); //     diff  link = link.replace(/\/(\d+)$/, "/_/diff?N=$1#diff0"); date = item.getElement("pubDate").getText(); desc = item.getElement("description").getText(); //  GUID    guid = Utilities.base64Encode(link + date); //  diff ,    Cookie "wikit_e"  "rss" var fullpage = UrlFetchApp.fetch(link, {"headers":{"Cookie":"wikit_e=rss"}}).getContentText(); //     body var matched = fullpage.match(/<body[^>]*>([\w\W]*)<\/body>/i)[1]; //      matched = matched.replace(/<div id='menu_area'>[\w\W]+$/i, ""); matched = matched.replace(/^[\w\W]+<div id='content'>/i, ""); //        matched = matched.replace(/(href|src)=(["'])\//ig, "$1=$2http://wiki.tcl.tk/"); // .. CSS  RSS  ,      matched = matched.replace(/class='newwikiline'/g, "style='margin:0;background:#80ff80;'"); matched = matched.replace(/class='oldwikiline'/g, "style='margin:0;background:#ffa0a0;'"); matched = matched.replace(/class='whitespacediff'/g, "style='margin:0;background:#f0f0ff;'"); matched = matched.replace(/class='wikit_categories'/g, "style='padding:2px 5px 2px 5px;text-align:left;border:1px solid gray;background-color:#DDD;'"); matched = matched.replace(/<pre>/g, "<pre style='color:#331100;background-color:#eeeeee;font-family:monospace;'>"); //    RSS   rss += "<item>\n"; rss += " <title>" + title + "</title>\n"; rss += " <link>" + link + "</link>\n"; rss += " <pubDate>" + date + "</pubDate>\n"; rss += " <guid isPermaLink='false'>" + guid + "</guid>\n"; rss += " <description><![CDATA[" + desc + "<br>\n" + matched + "]]></description>\n"; rss += "</item>\n"; }; //   RSS rss += "</channel></rss>"; //         30  //    - 100kb //      ,   try { cache.put(id, rss, 1800); } catch (e) { Logger.log(e); }; }; //      return ContentService.createTextOutput(rss).setMimeType(ContentService.MimeType.RSS); }; 

After saving the script, set the access level as "View an item can any user with a link" or "Publicly available on the Internet: any item can find and view an item." In the menu "File-> Versions ..." we create a new version of the application. Next, in the “Publish-> Expand as web application ...” menu, select the saved version, in the “How to run the application” drop-down list, select “On my behalf”, in the “Who has access to the application” dropdown list select “Everyone including anonymous users. " Get the link in the form " script.google.com/macros/s< ID >/exec script.google.com/macros/s< ID >/exec ", which we use in any RSS reader.

Bonuses:
  1. An example of using Google Apps Script to automatically transfer a tape to another language using Google Translate
  2. RSS 2.0 specification

')

Source: https://habr.com/ru/post/175775/


All Articles