Skip to content

Instantly share code, notes, and snippets.

@pfefferle

pfefferle/rss.gs

Last active May 4, 2021
Embed
What would you like to do?
function doGet() {
// What is the original language of the RSS Feed
var fromLang = "de";
// What is the destination language
var toLang = "en";
// Enter the full URL of the RSS feed
var rssFeed = "https://notiz.blog/feed/";
/*
T U T O R I A L
- - - - - - - -
Step 0. Go to File -> Make a Copy to copy this script to your Google Drive.
Step 1. Update the values of fromLang, toLang and rssFeed fields above.
Step 2. Go to File -> Manage Versions and Save a new version.
Step 3. Go to Publish -> Deploy as Web App, choose "Anyone, even Anonymous" under "Who can access the app" and click Deploy.
Google Script will provide a URL of the web app. That's the translated RSS feed which you can subscribe to in your Feed Reader.
Original version by @labnol (https://www.labnol.org/internet/google-translate-rss-feeds/5110/) modified by @pfefferle.
T H E G E E K Y S T U F F
- - - - - - - - - - - - -
You can ignore everything that's below this line.
*/
var feed = parseRSS(rssFeed, fromLang, toLang);
return ContentService.createTextOutput(feed).setMimeType(ContentService.MimeType.RSS);
}
function parseRSS(feed, fromLang, toLang) {
var id = Utilities.base64Encode(feed + fromLang + toLang);
var cache = CacheService.getPublicCache();
var rss = cache.get(id);
if (rss != null) {
return rss;
}
var item, title, desc;
var txt = UrlFetchApp.fetch(feed).getContentText();
var doc = XmlService.parse(txt);
var content = XmlService.getNamespace("http://purl.org/rss/1.0/modules/content/");
var atom = XmlService.getNamespace("http://www.w3.org/2005/Atom");
var root = doc.getRootElement();
var channel = root.getChild("channel");
channel.getChild("title").setText(channel.getChild("title").getText() + " (" + toLang + ")");
channel.getChild("language").setText(toLang);
var link = channel.getChild("link").getText();
link += (link.split("?")[1] ? "&":"?") + "lang=" + toLang;
channel.getChild("link").setText(link);
var items = channel.getChildren("item");
for (var i in items) {
try {
item = items[i];
channel.removeContent(item);
var title = item.getChild("title").getText();
var desc = item.getChild("description").getText();
var guid = item.getChild("guid").getText();
var cont = item.getChild("encoded", content).getText();
title = LanguageApp.translate(title, fromLang, toLang);
desc = LanguageApp.translate(desc, fromLang, toLang, {contentType: "html"});
guid += (guid.split("?")[1] ? "&":"?") + "lang=" + toLang;
cont = LanguageApp.translate(cont, fromLang, toLang, {contentType: "html"});
item.getChild("title").setText(title);
item.getChild("description").setText(desc);
item.getChild("guid").setText(guid);
item.getChild("encoded", content).setText(cont);
channel.addContent(item);
} catch (e) {
Logger.log(e);
}
}
var rss = XmlService.getCompactFormat().setEncoding('UTF-8').format(doc);
cache.put(id, rss, 3600); // Cache the feed for an hour
return rss;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment