Google 驱动和翻译脚本

Google Drive and Translation Script

我正在使用这个脚本,但每次获取 RSS 时都会创建一个新的 HTML 文件。

我希望它只重写以前的文件而不是创建一个新文件并且每天只获取一次。在那之后,如果有任何方法可以修复这个不断变化的 mime 或任何其他选项,我将分享 file is not validated in RSS Validator。如有任何帮助,我们将不胜感激。

function doGet() {

  var fromLang = "en";    
  var toLang   = "es";    
  var rssFeed  = "http://xkcd.com/rss.xml";  

  var feed = parseRSS(rssFeed, fromLang, toLang);   
  DriveApp.createFile("rssTest", feed, MimeType.HTML);
  return ContentService.createTextOutput(feed)
           .setMimeType(ContentService.MimeType.RSS);    
}

function parseRSS(feed, fromLang, toLang) {

  var id = Utilities.base64Encode(feed + fromLang + toLang);

  // Cache the RSS feeds for an hour
  var cache = CacheService.getPublicCache();
  var rss   = cache.get(id);

  if (rss != null) {
    return rss;
  }

  var item, date, title, link, desc, guid; 

  var txt = UrlFetchApp.fetch(feed).getContentText();
  var doc = Xml.parse(txt, false);  

  title = doc.getElement().getElement("channel").getElement("title").getText();

  // The RSS Feed is translated using Google Translate
  rss = '<rss version="2.0">';
  rss += "<channel><title>";
  rss += LanguageApp.translate(title, fromLang, toLang);
  rss += " (" + title + ")</title>";

  var items = doc.getElement().getElement("channel").getElements("item");   

  // Parsing single items in the RSS Feed
  for (var i in items) {

    try {

      item  = items[i];

      title = item.getElement("title").getText();
      link  = item.getElement("link").getText();
      date  = item.getElement("pubDate").getText();
      desc  = item.getElement("description").getText();

      guid  = Utilities.base64Encode(link + fromLang + toLang);

      title = LanguageApp.translate(title, fromLang, toLang);
      desc  = LanguageApp.translate(desc,  fromLang, toLang, 
                                       {contentType: "html"});

      rss += "<item>";
      rss += "  <title>"   + title + "</title>";
      rss += "  <link>"    + link  + "</link>";
      rss += "  <pubDate>" + date  + "</pubDate>";
      rss += "  <guid>"    + guid  + "</guid>";
      rss += "  <description><![CDATA[" + desc + "]]></description>";
      rss += "</item>";

    } catch (e) {
      Logger.log(e);
    }
  }

  rss += "</channel></rss>";

  cache.put(id, rss, 3600);
  return rss;

}

您可以通过调用 setContent(). And if you know the ID you can get the file through DriveApp.getFileById().

方法来更改文件的内容

所以这部分代码:

DriveApp.createFile("rssTest", feed, MimeType.HTML);

可以改成这样:

var file = DriveApp.getFileById("<your file ID>");
file.setContent(feed);

我已尝试在 validator you sent 中验证此 RSS。

为了验证,我需要做一些事情。

  1. guid标签里有错误,基本上,你need to add是PermaLink="false".

  2. 之后记住 channel 元素需要 description 元素并且 linkw3schools 检查它。

这样做对我来说是一个有效的 RSS。因此,您需要更改 parseRSS 才能使其正常工作。

function parseRSS(feed, fromLang, toLang) {

  var id = Utilities.base64Encode(feed + fromLang + toLang);

  // Cache the RSS feeds for an hour
  var cache = CacheService.getPublicCache();
  var rss   = cache.get(id);

  if (rss != null) {
    return rss;
  }

  var item, date, title, link, desc, guid; 

  var txt = UrlFetchApp.fetch(feed).getContentText();
  var doc = Xml.parse(txt, false);  

  title = doc.getElement().getElement("channel").getElement("title").getText();

  // The RSS Feed is translated using Google Translate
  rss = '<rss version="2.0">';
  rss += "<channel><title>";
  rss += LanguageApp.translate(title, fromLang, toLang);
  rss += " (" + title + ")</title>";
  rss += "<description>Description you need to fill</description>"; // Add this line
  rss += "<link>Link you need to fill</link>"; // Add this line 

  var items = doc.getElement().getElement("channel").getElements("item");   

  // Parsing single items in the RSS Feed
  for (var i in items) {

    try {

      item  = items[i];

      title = item.getElement("title").getText();
      link  = item.getElement("link").getText();
      date  = item.getElement("pubDate").getText();
      desc  = item.getElement("description").getText();

      guid  = Utilities.base64Encode(link + fromLang + toLang);

      title = LanguageApp.translate(title, fromLang, toLang);
      desc  = LanguageApp.translate(desc,  fromLang, toLang, 
                                       {contentType: "html"});

      rss += "<item>";
      rss += "  <title>"   + title + "</title>";
      rss += "  <link>"    + link  + "</link>";
      rss += "  <pubDate>" + date  + "</pubDate>";
      rss += "  <guid isPermaLink="false">"    + guid  + "</guid>"; // Modified this line
      rss += "  <description><![CDATA[" + desc + "]]></description>";
      rss += "</item>";

    } catch (e) {
      Logger.log(e);
    }
  }

  rss += "</channel></rss>";

  cache.put(id, rss, 3600);
  return rss;

}