Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Tuesday, 8 November 2011

Pulling Blogger content into Google Sites with Google Apps Script

You may remember that I migrated the Heswall Disabled Children's Holiday Fund website to Google Apps a couple of weeks ago, and started to use Google Sites for the web pages.

One of the things I'd been wanting to do for the camp is create a blog where news and developments can be published. Google Sites offers an "Announcement" page type, but it's not really as nice to work with a Blogger, as I had discovered here already. But the trouble with trying to combine a Blogger blog with a Google Sites website, is that both use templates neither of which can easily be made to be part of the other.

The solution I came up with is to use Blogger for writing the blog, but have the RSS feed published into a Google Sites page. Conveniently, Google provides a scripting toolset, Google Apps Script (apparently this doesn't have anything to do with Google Apps, the service, but with Google apps, the suite of applications), that allows JavaScript access to the Sites object model (amongst other things).

Here's the script I came up with:

function loadContent() {
  // ... Initialisation ...
  
  var feedDocument = Xml.parse(UrlFetchApp.fetch(rssUrl).getContentText(), false);
  var feedUpdated = feedDocument.feed.updated.getText();
  var pageUpdated = CacheService.getPublicCache().get("updated");
  
  CacheService.getPublicCache().put("updated", feedUpdated, 4800);

  if (feedUpdated == pageUpdated) {
    return;
  }
  
  var site = SitesApp.getSite(domainName, siteName);
  var page = site.getChildByName(pageName);
  var template = getTemplate(site.getTemplates());
  
  var entriesArray = feedDocument.getElement().getElements("entry");
  var newPageHTML = "";
  for (var i = 0; i < entriesArray.length && i < numberPosts; i++) {
    var entry = entriesArray[i];

    newPageHTML = newPageHTML + "<div>"+
      "<h3><a href='"+getLink(entry)+"'>"+entry.title.getText()+"</a></h3>"+
      "<p>"+entry.content.getText()+"</p>"+
      "<div style='font-size:0.8em;background-color:#E7E7E7'>Posted by "+entry.author.name.getText()+" on "+getDate(entry)+"</div>"+
      "</div>";
  }
  
  if (entriesArray.length > numberPosts) {
    newPageHTML = newPageHTML + "<p><a href='"+blogspotUrl+"'>View older posts</a></p>";
  }
  
  newPageHTML = newPageHTML + "<p><a href='"+rssUrl+"'>Subscribe to Posts (Atom)</a></p>"
      
  page.setHtmlContent(template.getHtmlContent().replace(/<br[^>]*>/i, newPageHTML));
}

function getTemplate(templates) {
  var templateName = ScriptProperties.getProperty("templateName");
  for (var i = 0; i < templates.length; i++) {
    var t = templates[i];
    if (t.getPageType() == PageType.WEB_PAGE && t.getName() == templateName) {
      return t;
    }
  }
}

function getLink(entry) { ... } // Gets the right link from the entry
function getDate(entry) { ... } // Parses and formats the published date
Pretty simple, and seems to work ok. Admittedly at the time of writing, there's only one entry to publish, but I don't think more should be a problem.

Monday, 24 October 2011

Transferring to Google Apps

As some of you know, I've been helping a charity that runs a camp for disabled children, Heswall Disabled Children's Holiday Fund, for nearly 15 years - an organisation that has been very close to my heart ever since - and not least because it was there that I met Clare, who eventually became Mrs Potes in July 2006.

One of the things I do for the charity is do the best I can creating and maintaining their presence on the internet - and I'm no web designer, I can tell you. Until recently, I was using hosting space with my very own hand-crafted HTML - it wasn't bad, but it wasn't that great either, and because of the hosting it was costing money. So in the interest of saving the charity a bit of cash, I started considering the options for replacement.

The domain I use for this blog is hosted using Google Apps, which I've been using for many years now, so that seemed the obvious choice. The maturity of Google Sites (which was added a couple of years ago) meant that I could create the site in a much more WYSIWYG way, and importing the content from the old site took half an hour while I ate my sandwich in front of my computer at work one lunchtime. To complete the migration, I reverted the DNS settings for the HDCHF domain, and added the required CNAME entry for the "www" subdomain, and MX records for email, both of which Google provides you instructions for, and away we went. I'm no DNS expert, but it seems CNAME entries can be used to create domain redirections, and provided the server that they redirect to can interpret the path part of the URL, you can use them in just the same way as URLs referencing subdomains declared using A records (where the hosting server is contacted directly).

As Google Apps basically gives you (almost) all of Google's products wrapped up with a bit of enterprise-y management stuff, and so we've also got the usual range of calendars, docs, gmail, and so on - whether I can convince the other committee members (whose ranks I have just joined) to use them will remain to be seen.

The only downside is that as a UK Charity we do not qualify for the not-for-profits version of Google Apps (boo, hiss!!), so we're restricted to just 10 organisation users - everyone else will have to use their normal email address, enabled for Google use - I hope this won't stop them using the private wiki site, also transferred to Google Sites.