Sunday, March 8, 2015

Doodles for your Google Apps domain

Since 1998, when the first doodle was released, they have been one of the most loved features of the Google home page. There have been doodles to celebrate all kinds of events, including national holidays, birthdays of artists and scientists, sports competitions, scientific discoveries and even video games! Also, doodles have evolved from simple static images to complex applications, such as the interactive electric guitar used to celebrate the birthday of Les Paul.

Want your company logo to change for selected events or holidays, just like doodles? The Admin Settings API allows domain administrators to write scripts to programmatically change the logo of their Google Apps domain, and Google App Engine offers the ability to configure regularly scheduled tasks, so that those scripts can run automatically every day.

With these two pieces combined, it is pretty easy to implement a complete solution to change the domain logo on a daily basis (assuming the graphic designers have prepared a doodle for each day), as in the following screenshot:

Let’s start with a Python App Engine script called doodleapps.py:

import gdata.apps.adminsettings.service
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from datetime import date

class DoodleHandler(webapp.RequestHandler):
# list of available doodles
DOODLES = {
1-1: images/newyearsday.jpg,
2-14: images/valentinesday.jpg,
10-31: images/halloween.jpg,
12-25: images/christmas.jpg
}

# returns the path to the doodle corresponding to the date
# or None if no doodle is available
def getHolidayDoodle(self, date):
key = %s-%s % (date.month, date.day)
if key not in self.DOODLES:
return None

return self.DOODLES[key]

# handles HTTP requests by setting today’s doodle
def get(self):
doodle = self.getHolidayDoodle(date.today())
self.response.out.write(doodle)

if doodle:
service = gdata.apps.adminsettings.service.AdminSettingsService()
// replace domain, email and password with your credentials
// or change the authorization mechanism to use OAuth
service.domain = MYDOMAIN.COM
service.email = ADMIN@MYDOMAIN.COM
service.password = MYPASSWORD
service.source = DoodleApps
service.ProgrammaticLogin()

# reads the doodle image and update the domain logo
doodle_bytes = open(doodle, "rb").read()
service.UpdateDomainLogo(doodle_bytes)

# webapp initialization
def main():
application = webapp.WSGIApplication([(/, DoodleHandler)],
debug=True)
util.run_wsgi_app(application)


if __name__ == __main__:
main()

The script uses a set of predefined doodles which can be edited to match your list of images or replaced with more sophisticated logic, such as using the Google Calendar API to get the list of holidays in your country.

Every time the script is triggered by an incoming HTTP request, it will check whether a doodle for the date is available and, if there is one, update the domain logo using the Admin Settings API.

In order for this script to be deployed on App Engine, you need to to configure the application by defining a app.yaml file with the following content:

application: doodleapps
version: 1
runtime: python
api_version: 1

handlers:
- url: .*
script: doodleapps.py

We want the script to run automatically every 24 hours, without the need for the administrator to send a request, so we also have to define another configuration file called cron.yaml:

cron:
- description: daily doodle update
url: /
schedule: every 24 hours

Once the application is deployed on App Engine, it will run the script on a daily basis and update the logo.

The holiday season is upon us. Could there be a better time for your company to start using doodles?

Happy Holidays!

Claudio Cherubino   profile | twitter | blog

Claudio is a Developer Programs Engineer working on Google Apps APIs and the Google Apps Marketplace. Prior to Google, he worked as software developer, technology evangelist, community manager, consultant, technical translator and has contributed to many open-source projects, including MySQL, PHP, Wordpress, Songbird and Project Voldemort.