Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created June 27, 2012 22:26
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mhawksey/3007293 to your computer and use it in GitHub Desktop.
Save mhawksey/3007293 to your computer and use it in GitHub Desktop.
Turn Google Site pages into RSS feed
/*
Copyright 2011 Martin Hawksey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Uses the new ContentServices to generate a RSS feed from a set of Google Site pages.
To use install then Publish > Deploy as web app. You'll need to use the olds style
service url https://script.google.com/macros/exec?service={service_key}&siteurl={url
encode_Google_Sites_url&page={page_stub}
e.g. https://script.google.com/macros/exec?service={service_key_removed}&siteurl=htt
ps%3A%2F%2Fsites.google.com%2Fsite%2Fscriptsexamples%2F&page=custom-methods
To make available to aggregators like Google Reader the service needs to allow
anonymous access
*/
function doGet(e) {
var siteUrl = decodeURIComponent(e.parameter.siteurl);
var page = decodeURIComponent(e.parameter.page);
var cache = CacheService.getPrivateCache();// using Cache service to prevent too many urlfetch
var cached = cache.get(siteUrl+page);
if (cached != null) { // if value in cache return it
var feed = cached;
} else {
var feed = turnSitesPageIntoRSS(siteUrl,page);
try {
cache.put(siteUrl+page, feed, 21600);// cache for 1/4 day
} catch (e) {
}
}
return ContentService.createTextOutput(feed).setMimeType(ContentService.MimeType.RSS);
}
function turnSitesPageIntoRSS(siteUrl, page){
//var siteUrl = 'https://sites.google.com/site/scriptsexamples/';
//var page = 'custom-methods';
var output = "";
var site = SitesApp.getSiteByUrl(siteUrl);
var topPage = SitesApp.getPageByUrl(siteUrl+page);
var rssHead = '<?xml version="1.0" encoding="UTF-8" ?> '+
' <rss version="2.0"><channel>'+
' <title>'+site.getTitle()+': '+topPage.getTitle()+'</title>' +
' <description>'+site.getSummary()+'</description>'+
' <link>'+siteUrl+page+'</link>';
var rssFoot = ' </channel></rss>';
var items = "";
output = rssHead;
var pages = topPage.getAllDescendants();
for (i in pages){
items = '<item>'+
'<title>'+pages[i].getTitle()+'</title>'+
'<link>'+pages[i].getUrl()+'</link>'+
'<description><![CDATA['+pages[i].getHtmlContent()+']]></description>'+
'<pubDate>'+Utilities.formatDate(pages[i].getLastUpdated(), 'GMT', 'EEE, d MMM yy HH:mm:ss z') +'</pubDate>'+
'</item>';
output += items;
}
output += rssFoot;
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment