Instantly share code, notes, and snippets.
Save pfefferle/834226ee0ea20093df83f3661822b4eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
functiondoGet(){ | |
// What is the original language of the RSS Feed | |
varfromLang="de"; | |
// What is the destination language | |
vartoLang="en"; | |
// Enter the full URL of the RSS feed | |
varrssFeed="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. | |
*/ | |
varfeed=parseRSS(rssFeed,fromLang,toLang); | |
returnContentService.createTextOutput(feed).setMimeType(ContentService.MimeType.RSS); | |
} | |
functionparseRSS(feed,fromLang,toLang){ | |
varid=Utilities.base64Encode(feed+fromLang+toLang); | |
varcache=CacheService.getPublicCache(); | |
varrss=cache.get(id); | |
if(rss!=null){ | |
returnrss; | |
} | |
varitem,title,desc; | |
vartxt=UrlFetchApp.fetch(feed).getContentText(); | |
vardoc=XmlService.parse(txt); | |
varcontent=XmlService.getNamespace("http://purl.org/rss/1.0/modules/content/"); | |
varatom=XmlService.getNamespace("http://www.w3.org/2005/Atom"); | |
varroot=doc.getRootElement(); | |
varchannel=root.getChild("channel"); | |
channel.getChild("title").setText(channel.getChild("title").getText()+" ("+toLang+")"); | |
channel.getChild("language").setText(toLang); | |
varlink=channel.getChild("link").getText(); | |
link+=(link.split("?")[1] ?"&":"?")+"lang="+toLang; | |
channel.getChild("link").setText(link); | |
varitems=channel.getChildren("item"); | |
for(variinitems){ | |
try{ | |
item=items[i]; | |
channel.removeContent(item); | |
vartitle=item.getChild("title").getText(); | |
vardesc=item.getChild("description").getText(); | |
varguid=item.getChild("guid").getText(); | |
varcont=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); | |
} | |
} | |
varrss=XmlService.getCompactFormat().setEncoding('UTF-8').format(doc); | |
cache.put(id,rss,3600);// Cache the feed for an hour | |
returnrss; | |
} |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment