public
Last active

Turn Google Site pages into RSS feed

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*
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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.