Catfood Earth

Catfood Earth

Catfood Earth uses satellite imagery and data feeds to create live wallpaper for Windows and Android. Earth shows you the current extent of day and night combined with global cloud cover (clouds updated every hour). You can also choose to display time zones, places, earthquakes, volcanoes and weather radar. The Windows version includes a screen saver. Catfood Earth was first released in 2003. Version 4 was fully remastered to support 4K resolution on both platforms.

Catfood Earth Themes

Catfood Earth can be quickly configured with three different themes. After installing, run Catfood Earth Settings and click one of the buttons to activate a theme. If you want to customize further then visit the layers and advanced tabs of the settings app, described in detail below. The themes are:

Natural Theme

Natural is the default theme. This combines day, night and cloud cover. The day image uses NASA's blue marble next generation monthly global composite. combined daily so you can see changes in snow, ice and even vegetation throughout the year. Next, global cloud cover is blended on top of the day image (updated hourly). Finally Catfood Earth computes the extent of day and night and blends in NASA's city lights image. You can update the image as frequently as every minute and watch the terminator between day and night move across the planet and change shape with the passing seasons. The video below shows how all three components of the natural theme change over the course of a year:

Time Zones Theme

The time zones theme shows the current time zone in each country and optionally international zones as well. This theme is highly configurable, you can choose to show borders, pick colors, show hours or hours and minutes and control transparency. Catfood Earth can also show the local time for anywhere on Earth, see the places layer for more details. The video below shows how the time zones change by country from May 2021 to June 2022:

Earthquakes Theme

The final theme, earthquakes, uses a different daytime map with a more geological look and adds earthquakes for the past 24 hours. Size is proportional to magnitude and the quakes fade out over 24 hours so the most recent are highly visible. Again it's possible to customize extensively via layers. You can show active volcanoes and control the colors used, transparency and which magnitudes to plot. The video below shows a month of earthquakes as seen in Catfood Earth:

Catfood Earth Layers

The layers tab is where you can fully customize Catfood Earth. Use the checkboxes to activate layers and then click each layer to access detailed settings. Layers are listed in the order that they are drawn (i.e. clouds are drawn on top of the day map, and then night is drawn on top of the clouds and so on).

Day Map

The first layer is the daytime map. Catfood Earth ships with two defaults. The first uses NASA's blue marble 2 monthly images, merged with a more attractive ocean image. Catfood Earth interpolates these images daily so the extent of snow/ice and vegetation will change in a subtle way throughout the year. The other default is a less colorful map that I feel works well with earthquakes and volcanoes. You can also browse to and select a different image if you don't like the defaults.

All the images and data layers in Catfood Earth use an equirectangular projection so if you are using custom images you should make sure that they use the expected projection.

Clouds

Clouds come from the Space Science and Engineering Center at the University of Wisconsin-Madison. These images are web mercator by default, my website downloads the global composite every hour and transforms the image to equirectangular for Catfood Earth. If you have a different source for clouds you can enter a custom URL. You can also set the color and transparency of the clouds layer. The defaults are designed to make the clouds fairly subtle.

Night Map

The last natural layer is the nighttime map. Catfood Earth ships with NASA's 2016 update to city lights. This layer is drawn on top of daytime and clouds where it is currently nighttime (updated as often as every minute, see advanced options below). You can set the transparency of the layer and also the width used to blend the terminator between day and night (in degrees). It's also possible to select a different nighttime image if you prefer.

Time Zones

The time zones layer is based on the IANA time zone database. You can change the colors used to show the time zones and control the transparency of national and international zones. You can also choose if minutes are displayed with the time zone legends at the top of the screen. This is a good layer to use in combination with the Places layer and the Political Borders layer (both described below).

Political Borders

This layer displays political borders in a configurable color.

Places

The places layer shows a list of places optionally combined with the local time for each place. I've used this to show office locations around the world (handy to know if it's a good time to call someone) and also to track my geographically dispersed family. Catfood Earth ships with a default list of major cities. You can add to this, or remove everything and start from scratch. You can even export and import the list to make it easy to share between different devices. For each place you just need to provide the latitude, longitude and time zone (from the same source as the time zones layer).

Earthquakes

Earthquake data is downloaded from the USGS feed and can show global quakes from magnitude 2.5 up. There is a lot you can fine tune for this layer - The fill and accent colors, number of past hours to show (up to 24), the transparency range (older earthquakes are more transparent) and the minimum magnitude to plot. You can also choose whether to show descriptions or not and the order in which to plot the quakes (time based, or size based which makes it easier to see what's going on when a lot of quakes hit a small area).

Volcanoes

Volcano data comes from the Smithsonian Institution's Global Volcanism Program. This shows currently active volcanoes around the world. You can set the fill and accent colors, the transparency to use and whether to show descriptions or not.

Weather Radar

The final layer shows weather radar from the National Weather Service. This layer is US only. Enter a list of one or more radar stations to display, separated by commas. You can find your local station(s) at the NWS web site.

Catfood Earth Advanced

The advanced tab of Catfood Earth Settings controls how the final image is displayed and also allows you to render sequences of images (this is how I made most of the videos above).

Set how often the image is updated in minutes, as frequently as every minute which is handy if you're displaying the local time for places. You can also skip the first update when Catfood Earth loads (potentially useful at boot time when Windows is starting a lot of programs).

There are some settings relating to text display: a global font size and an option to use drop shadows.

Earth can be centered to a specific longitude (default 0) or to a specific time. If you choose a time then the image will rotate throughout the day to keep that local time at the center of the screen.

It is also possible to set a specific output size and location for the wallpaper. The default is to fill the screen.

Click Render Images to output a sequence of images. If you use a proxy server click Proxy Settings to configure.

Finally there are some troubleshooting options that you might be asked to use if you need help with the product.

Render Images

Render Images allows you to create frames from Catfood Earth based on current settings. This is useful for making animations based on how the Earth image changes over time. Set a start date/time (UTC), how to increment time between frames, the number of frames to generate, the size and an output folder. Earth will then create a sequence of JPEGs. This works well with computed layers like day/night and time zones. It will not work well with live data like clouds and earthquakes. I have made videos from live data as well, but this involves storing many images and/or data points and is not currently supported in Catfood Earth.

Catfood Earth for Android

Catfood Earth is a little different on Android. Because phones are mostly used in a portrait orientation the wallpaper shows the full range of latitude but only a segment of longitude depending on the screen size of the device. The wallpaper is the same as the natural theme in Catfood Earth for Windows (blue marble 2 day map, clouds, city lights night map) with the optional addition of earthquakes.

Run Catfood Earth and click Settings to configure. The slice of Earth displayed is based on a central longitude. By default this will update to your phone's current location. You can also set a manual location. -90 degrees works well to center North America. In addition to the central longitude you can opt to show recent earthquakes, control cloud and night transparency and the width of the terminator between day and night. There are advanced options to ignore screen size changes and reuse the most recent image, and to paint black under the menu bar which is useful on some devices.

After configuring Catood Earth just select it as your current wallpaper. You can do this from device settings or tap the Set Wallpaper button in the Catfood Earth app.

Catfood Earth for Android is optimized for battery life. It will update around every ten minutes and so it might take that long to see any changes to settings. Clouds are downloaded every hour. If using your phone location it accesses the last location fix established by other apps.

Installation and Support for Catfood Earth

Catfood Earth for Windows requires version 3.5 of the .NET Framework. On Windows 10 you need to go to Programs and Features and then Turn Windows features on or off, find .NET 3.5 and enable it. The installer for Catfood Earth is not currently digitally signed. You may get a warning when downloading and again when installing which you will need to ignore to proceed. I plan to fix both of these installation limitations at some point in the future.

For support please visit this post, check to see if your issue has already been addressed and if not leave a new comment.

Updates

Catfood Earth 4.10

Catfood Earth 4.10

Catfood Earth 4.10 is available for download.

The National Weather Service updated their weather radar API. The weather radar layer has changed a bit, you can enter one or more (comma separated) weather station IDs and Earth will show one hour precipitation for all of them. You used to be limited to a single station but with more options for the rader layer to display. Let me know if you love or hate the new version.

4.10 also includes the latest 2021a time zone database.

(I'm sure there are great reasons for it, but the 'new' NWS API is an XML document per station that links to a HTML folder listing of images where you can enjoy parsing out the latest only to download a TINY GZIPPED TIFF file FFS).

Catfood Earth 4.01

Catfood Earth 4.01 is available for download.

The timezone database has been updated to 2020a. There is also a small fix to a problem with screensaver installation on recent versions of Windows 10.

Catfood Earth for Android 4.00

Catfood Earth for Android 4.00

Catfood Earth for Android 4.00 is available for download and is updating through the Google Play Store.

As with the 4.00 update for Windows all images have been remastered to 4K resolution. Earth for Android has also been updated to better support Android 10 (updates are faster and the settings layout looks much better). You'll need to grant location permission in settings to have Earth automatically center on your current location. It's also possible to set a center longitude manually (I find -90 works well for centering most of the Americas).

Catfood Earth 4.00

Image from Catfood Earth 4.00

Catfood Earth 4.00 is available for download.

The main change is that all of the images shipped with Catfood Earth have been remastered to 4K resolution. This includes NASA Blue Marble 2 monthly images (which Catfood Earth interpolates daily) and the 2016 version of Black Marble (city lights at night). The Catfood Earth clouds service has been updated to full 4K resolution as well.

Earth 4.00 also includes an update to the 2019c version of the Time Zone Database.

As well as providing desktop wallpaper and a screensaver, Catfood Earth can render frames for any time and date. To celebrate the release of 4.00 I created the 4K video below which shows all of 2019, 45 minutes per frame, 9,855 frames. You'll see the shape of the terminator change over the course of the year (I always post the seasonal changes here: Spring Equinox, Summer SolsticeAutumnal Equinox, and Winter Solstice). If you watch closely you'll also see changes in snow and ice cover and even vegetation over the course of the year.

Catfood Earth 3.46

Improved clouds in Catfood Earth

Catfood Earth 3.46 is now available for download. Catfood Earth for Android 1.70 is available in the Google Play Store and will update automatically if you already have it installed.

This follows hot on the heels of the last release as the new clouds layer service running on this blog can update far more frequently than the source used prior to 3.45. You will now get a fresh helping of clouds every hour! Unrelated to this release I've improved the quality of the clouds image as well. If you're interested you can read about this in exhaustive detail here.

Catfood Earth 3.45

New cloud layer image in Catfood Earth

Catfood Earth 3.45 is now available to download. Catfood Earth for Android 1.60 is available on Google Play and will update automatically if you have it installed.

I only just released 3.44 with some timezone updates but in the past week the location I had been using for global cloud cover abruptly shut down. If you like up to date clouds you'll want to install the new versions as soon as possible. With this update I'm building a cloud image every three hours and serving through this blog (and thankfully CloudFlare) so any further changes should not require a code release.

Catfood Earth 3.44

Updated timezones in Catfood Earth 3.44

Catfood Earth 3.44 is now available to download.

The timezone database has been updated to 2018i.

Eric Muller's shapefile map of timezones is no longer maintained and so Catfood Earth has switched to Evan Siroky's timezone boundary builder version.

A bug that could cause all volcanoes to be plotted at 0,0 depending on your system locale has been fixed.

Download Catfood Earth.

(Previously)

Catfood Earth 3.43

North Korea moves back to UTC +09 on May 5, 2018

Catfood Earth 3.43 updates the timezone database to 2018e. The big change is that North Korea is moving back to UTC +09 today (May 5, 2018). The time zones layer in Catfood Earth shows the current time in each zone at the top of your screen and color codes each country and region. You can also display a list of specific places (either an included list of major cities or your own custom locations).

Catfood Earth is dynamic desktop wallpaper for Windows that includes day and night time satellite imagery, the terminator between daytime and nighttime, global cloud cover, time zones, political borders, places, earthquakes, volcanoes and weather radar. You can choose which layers to display an how often the wallpaper updates.

Download Catfood Earth.

(Previously, Previously)

Catfood Earth 3.42

Catfood Earth 3.42

Catfood Earth 3.42 is a small update to the latest (2016d) timezone database and the latest timezone world and countries maps from Eric Muller. If you use the political borders, places or time zones layers in Catfood Earth then you'll want to install this version.

Download the latest Catfood Earth.

(Previously)

Catfood Earth 3.41

Catfood Earth 3.41

Catfood Earth 3.41 fixes a problem that was preventing the weather radar layer from loading.

I've also updated to the latest (2015g) time zone database and the latest time zone map from Eric Muller.

Download the latest Catfood Earth.

(Previously)

Catfood Earth 3.40

Catfood Earth 3.40

I've just released Catfood Earth 3.40 for Windows and 1.50 for Android.

Both updates fix a problem with the clouds layer not updating. The Android update also adds compatibility for Android 5 / Lollipop.

Also, Catfood Earth for Android is now free. I had been charging $0.99 for the Android version but I've reached the conclusion that I'm never going to retire based on this (or even buy more than a couple of beers) so it's not worth the hassle. Catfood Earth for Windows has been free since 3.20.

Enjoy!

Volcanoes Back (Catfood Earth 3.30)

Volcanoes Back (Catfood Earth 3.30)

Catfood Earth fans will want to download Catfood Earth 3.30. This update fixes a problem where volcanoes were all plotted in the middle of the screen.

Catfood Earth for Android 1.40

Catfood Earth for Android 1.40

Catfood Earth for Android now includes the option to display earthquakes (magnitude 5.0 or higher) from the USGS feed.

Catfood Earth 3.20

Catfood Earth 3.20

Catfood Earth 3.20 for Windows is now available for download. This update fixes a change in the feed address for the earthquakes layer. I've also switched to using the new NASA Black Marble night-time image and 3.20 includes the latest time zone and political border data.

Earth for Android has been updated to 1.30. This includes the new Black Marble image.

Catfood Earth for Android 1.20

Catfood Earth for Android 1.20

I recently upgraded to the HTC One which has a transparent notification bar. This makes it hard to see notification icons when using Catfood Earth as your wallpaper, at least in the summer when it's always light at high latitudes and your white icons are displayed on top of polar ice and clouds.

Catfood Earth for Android 1.20 fixes this with an option to paint black under the notification bar. That's the only update other than the latest Xamarin runtime.

Catfood: Earth for Android 1.10

Catfood Earth for Android 1.10

I’ve just released Catfood Earth for Android 1.10. You can control the center of the screen manually (the most requested new feature) and also tweak the transparency of each layer and the width of the terminator between day and night. It also starts a lot faster and has fewer update glitches. Grab it from Google Play if this looks like your sort of live wallpaper.

Catfood: Earth for Android

EarthForAndroid

I’ve just released Catfood Earth for Android. It’s my second app created with Xamarin’s excellent toolkit. Being able to develop in C# allowed me to reuse a lot of code from the Windows version of Catfood Earth. The Android version doesn’t include all the same layers (yet) but it’s got the main ones – daytime (twelve different satellite images included, based on NASA’s Blue Marble Next Generation but with some special processing to make them look better), nighttime (city lights, shaded to show nighttime and the terminator between day and night) and a clouds layer that is downloaded every three hours.

My main worry had been that this would suck the phone battery dry, but after a fair amount of optimization it doesn’t even register on the battery consumption list. Grab it now from Google Play ($3.99, Android 2.2 or better).

(All Code Posts)

4K One Year Global Cloud Timelapse

Hurricane Dorian in Catfood Earth

Six 4K images a day at 24 frames per second (so each second is four days) from April 18, 2019 to April 17, 2020:

I made a version of this video a couple of years ago using xplanet clouds. That was lower resolution and only had one frame per day so it's pretty quick. This version uses the new 4K cloud image I developed for Catfood Earth just over a year ago. I've been patiently saving the image six times a day (well, patiently waiting as a script does this for me). It's pretty amazing to see storms developing and careening around the planet. The still frame at the top of the post shows Dorian hitting Florida back in September.

(All Code Posts)

How to backup Google Photos to Google Drive automatically after July 2019 with Apps Script

Updated on Sunday, March 14, 2021

Google Photos backup to Google Drive shutting down in July 2019

Google has decided that backing up your photos via Google Drive is 'confusing' and so Drive based backup is going away this month. I love Google Photos but I don't trust it - I pull everything into Drive and then I stick a monthly backup from there onto an external drive in a fire safe. There is a way to get Drive backup working again using Google Apps Script and the Google Photos API. There are a few steps to follow but it's pretty straightforward - you should only need to change two lines in the script to get this working for your account.

First two four caveats to be aware of. Apps Script has a time limit and so it's possible that it could fail if moving a large number of photos. You should get an email if the script ever fails so watch out for that. Secondly and more seriously you could end up with two copies of your photos. If you use Backup and Sync to add photos from Google Drive then these photos will be downloaded from Google Photos by the script and added to Drive again. You need to either upload directly to Google Photos (i.e. from the mobile app or web site) or handle the duplicates in some way. If you run Windows then I have released a command line tool that sorts photos into year+month taken folders and handles de-duplication.

One more limitation. After a comment by 'Logan' below I realized that Apps Script has a 50MB limitation for adding files to Google Drive. The latest version of the script will detect this and send you an email listing any files that could not be copied automatically.

And a fourth limitation after investigating a comment by 'Tim' it turns out there is a bug in the Google Photos API that means it will not download original quality video files. You get some lower resolution version instead. Together with the file size limit this is a bit of a deal breaker for most videos. Photos will be fine, but videos will need a different fix.

On to the script. In Google Drive create a new spreadsheet. This is just a host for the script and makes it easy to authorize it to access Google Photos. Select 'Script editor' from the Tools menu to create a new Apps Script project.

In the script editor select 'Libraries...' from the Resources menu. Enter 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF next to 'Add a library' and click add. This will find the Google OAuth2 library Pick the most recent version and click Save.

Select 'Project properties' from the File menu and copy the Script ID (a long sequence of letters and numbers). You'll need this when configuring the Google Photos API.

In a new window open the Google API Console, part of the Google Cloud Platform. Create a new project, click Enable APIs and Services and find and enable the Google Photos API. Then go to the Keys section and create an OAuth Client ID. You'll need to add a consent screen, the only field you need to fill out is the product name. Choose Web Application as the application type. When prompted for the authorized redirect URL enter https://script.google.com/macros/d/{SCRIPTID}/usercallback and replace {SCRIPTID} with the Script ID you copied above. Copy the Client ID and Client Secret which will be used in the next step.

Go back to the Apps Script project and paste the code below into the Code.gs window:

Enter the Client ID and Client Secret inside the empty quotes at the top of the file. You also need to add an email address to receive alerts for large files. There is a BackupFolder option at the top as well - the default is 'Google Photos' which will mimic the old behavior. You can change this if you like but make sure that the desired folder exists before running the script. Save the script.

Go back to the spreadsheet you created and reload. After a few seconds you will have a Google Photos Backup menu (to the right of the Help menu). Choose 'Authorize' from this menu. You will be prompted to give the script various permissions which you should grant. After this a sidebar should appear on the spreadsheet (if not choose 'Authorize' from the Google Photos Backup menu again). Click the authorize link from the sidebar to grant access to Google Photos. Once this is done you should be in business - choose Backup Now from the Google Photos Backup menu and any new items from yesterday should be copied to the Google Photos folder in Drive (or the folder you configured above if you changed this).

Finally you should set up a trigger to automate running the script every day. Choose 'Script editor' from the Tools menu to re-open the script, and then in the script window choose 'Current project's triggers' from the Edit menu. This will open yet another window. Click 'Add Trigger' which is cunningly hidden at the bottom right of the window. Under 'Choose which function to run' select 'runBackup'. Then under 'Select event source' select 'Time-driven'. Under 'Select type of time based trigger' select 'Day timer'. Under 'Select time of day' select the time window that works best for you. Click Save. The backup should now run every day.

The way the script is written you'll get a backup of anything added the previous day each time it runs. If there are any duplicate filenames in the backup folder the script will save a new copy of the file with (1) appended in front of the filename. Let me know in the comments if you use this script or have any suggestions to improve it.

More Google Apps Script Projects

(All Code Posts)

Facebook Interoperability

Updated on Friday, May 22, 2020

In TechCrunch today Josh Constine gets friend portability for Facebook almost right:

"In other words, the government should pass regulations forcing Facebook to let you export your friend list to other social networks in a privacy-safe way. This would allow you to connect with or follow those people elsewhere so you could leave Facebook without losing touch with your friends. The increased threat of people ditching Facebook for competitors would create a much stronger incentive to protect users and society."

The problem is having a list of friends does me no good at all when none of them are on Google Plus, Diaspora or whatever.

What we need is legislation that forces interoperability. I can share with my friends via an open protocol, and Facebook is forced to both send and receive posts from other networks. This would actually create an opportunity for plausible competition in a way that a friend export could never do. Social networking should work like email, not CompuServe.

(All Marketing Posts)

Better related posts with word2vec (C#)

I have been experimenting with word2vec recently. Word2vec trains a neural network to guess which word is likely to appear given the context of the surrounding words. The result is a vector representation of each word in the trained vocabulary with some amazing properties (the canonical example is king - man + woman = queen). You can also find similar words by looking at cosine distance - words that are close in meaning have vectors that are close in orientation.

This sounds like it should work well for finding related posts. Spoiler alert: it does!

My old system listed posts with similar tags. This worked reasonably well, but it depended on me remembering to add enough tags to each post and a lot of the time it really just listed a few recent posts that were loosely related. The new system (live now) does a much better job which should be helpful to visitors and is likely to help with SEO as well.

I don't have a full implementation to share as it's reasonably tightly coupled to my custom CMS but here is a code snippet which should be enough to get this up and running anywhere:

The first step is getting a vector representation of a post. Word2vec just gives you a vector for a word (or short phrase depending on how the model is trained). A related technology, doc2vec, adds the document to the vector. This could be useful but isn't really what I needed here (i.e. I could solve my forgetfulness around adding tags by training a model to suggest them for me - might be a good project for another day). I ended up using a pre-trained model and then averaging together the vectors for each word. This paper (PDF) suggests that this isn't too crazy.

For the model I used word2vec-slim which condenses the Google News model down from 3 million words to 300k. This is because my blog runs on a very modest EC2 instance and a multi-gigabyte model might kill it. I load the model into Word2vec.Tools (available via NuGet) and then just get the word vectors (GetRepresentationFor(...).NumericVector) and average them together.

I haven't included code to build the word list but I just took every word from the post, title, meta description and tag list, removed stop words (the, and, etc) and converted to lower case.

Now that each post has a vector representation it's easy to compute the most related posts. For a given post compute the cosine distance between the post vector and every other post. Sort the list in ascending order and pick however many you want from the top (the distance between the post and itself would be 1, a totally unrelated post would be 0). The last line in the code sample shows this comparison for one post pair using Accord.Math, also on Nuget.

I'm really happy with the results. This was a fast implementation and a huge improvement over tag based related posts.

(All Code Posts)

Animation of a year of Global Cloud Cover

Updated on Sunday, May 2, 2021

Update April 19, 2020: I made a longer, higher resolution version which you can find here.

Animation of a year of Global Cloud Cover

Here's an animation showing a year of global cloud cover (from July 2017 to July 2018) :

The clouds are sourced from the free daily download at xplanet. I run a Google apps script that saves a copy of the image to Google Drive every day (basically the same as this script to save Nest cam images). The hard part was waiting a year to get enough frames. Xplanet combines GEOS, METEOSAT and GMS satellite imagery with some reflection near the poles. Although I didn't need to for this project note that you can subscribe to higher quality / more frequent downloads.

As well as the clouds you can also see the terminator between day and night change shape over the course of the year. This video starts and ends with the Summer equinox when days are longest in the Northern hemisphere.

Where it's nighttime the image is based on NASA's Black Marble. The daytime is based on Blue Marble, but blended with a different older image which has better ocean colors and interpolated daily between twelve monthly Blue Marble satellite images. The result of this is that you can see snow and ice coverage changing over the course of the year. If you look closely you'll also notice vegetation growing and dying back with the seasons.

Rendered in a slightly modified build of Catfood Earth (the main release doesn't know how to access my private cache of xplanet cloud images). As well as combining day, night and cloud images Catfood Earth can also show you earthquakes, volcanoes, US weather radar, political borders, places and time zones. It has been enlivening Windows desktop wallpaper for fifteen years now (as shareware back when that was a thing, these days it's a free download for Windows and Android).

(All Code Posts)

Export Google Fit Daily Steps, Weight and Distance to a Google Sheet

Updated on Sunday, July 11, 2021

Google Fit Daily Step Export

Google Fit is a great way to keep track of your daily step count without needing to carry a Fitbit or other dedicated tracker. It's not easy to get that data out though, as far as I can tell the only way is Google Takeout which is not made for automation. Luckily there is an API and you can do almost anything with Google Sheets.

If you're looking to export your step count, weight and distance this post has everything you need, just follow the instructions below to get your spreadsheet up and running. This is also a good primer on using OAuth2 with Google Apps Script and should be a decent starting point for a more complex Google Fit integration. If you have any questions or feedback please leave a comment below.

To get started you need a Google Sheet, an apps script project attached to the sheet and a Google API Project that will provide access to the Fitness API. That might sound intimidating but it should only take a few minutes to get everything up and running.

In Google Drive create a new spreadsheet and call it whatever you like. Rename the first tab to 'Metrics'. Enter 'Date' in cell A1, 'Steps' in B1, 'Weight' in C1 and 'Distance' in D1. To grab history as well create another tab called 'History' with the same headers. Next select 'Script editor...' from the Tools menu which will open a new apps script project.

Give the apps script project a name and then select 'Libraries...' from the Resources menu. Next to 'Add a library' enter 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF and click Add. This will find the Google OAuth2 library. Choose the most recent version (24 at the time of writing) and click Save. Then select 'Project properties' from the File menu and make a note of the Script ID (a long series of letters and numbers).

Open the Google API Console. Create a new project and name it something like 'Google Fit Sheet'. From the Dashboard click Enable APIs and Services and find and select the Fitness API. Then go to Keys and create an OAuth Client ID. You'll be asked to create a consent screen, the only field you need to enter is the product name (i.e. 'My Fit App'). Then choose Web Application as the application type. You need to set the name and the authorized redirect URL. The redirect URL is https://script.google.com/macros/d/{SCRIPTID}/usercallback replacing {SCRIPTID} with the actual Script ID you made a note of above. After adding this make a note of the Client ID and Client Secret.

Go back to the apps script project and paste the code below into the Code.gs window:

Right at the top of the code there are spaces to enter the Client ID and Client Secret from the API Console. Enter these and save the project.

Switch back to your Google Sheet and reload. After reloading there will be a Google Fit menu item. First select Authorize... You'll get a screen to authorize the script and then a sidebar with a link. Click the link to authorize the script to access your Google Fit data. You can then close the sidebar and select Get Metrics for Yesterday from the Google Fit menu. You should see a new row added to the spreadsheet with yesterday's date and fitness data.

The final step is to automate pulling in the data. Go back to the apps script project and select Current project's triggers from the Edit menu. Add a trigger to run getMetrics() as a time driven day timer - I recommend between 5 and 6am. You can also click notifications to add an email alert if anything goes wrong, like your Google Fit authorization expiring (in which case you just need to come back and authorize from the Google Fit menu again.

At this point you're all set. Every day the spreadsheet will automatically update with your step count from the day before. You can add charts, moving averages, export to other systems, pull in your weight or BMI, etc. I want to add a seven day moving average step count to this blog somewhere as a semi-public motivational tool... watch this space.

Note that weight will be blank in the spreadsheet for days with no weight data. Google Fit doesn't return the last known weight, only the known value for days where an update was recorded.

If you are looking to extend this sample to other data types then this API explorer page is very helpful for finding data types that the API documentation doesn't list.

A couple of times working on this script I got my authorization in a bad state and started getting a 400 error response from the API. If this happens run your Google Fit app, click the Profile icon at the bottom and then the Settings icon at the top right. Click Manage connected apps and then disconnect the script from Google Fit. Finally run the Reset Settings option from the menu in the sheet and then authorize again.

I updated this post on Jan 21, 2019 to extend the sample to handle weight and distance as well as steps. I also improved the history function to handle many days in one API call rather than a quick hack I added earlier that pulled a day at a time. I'd recommend using the code above rather than anything included in comments below (at least comments before this update).

More Google Apps Script Projects

(All Code Posts)

Enable GZIP compression for Amazon S3 hosted website in CloudFront

Updated on Tuesday, November 12, 2019

Enable GZIP compression for Amazon S3 hosted website in CloudFront

By default compression doesn't work in CloudFront for a website backed by an Amaxon S3 bucket.

The first step is pretty obvious - switch on compression in CloudFront:

Compress Objects Automatically option in Amazon CloudFront

To get to this setting open you distribution, go to the Behaviors tab and edit your behavior(s). Scroll down to the bottom and toggle Compress Objects Automatically to On. Save and drum your fingers while the distribution updates.

The less obvious piece is that CloudFront will only compress files between 1,000 and 10,000,000 bytes (as of writing this post) and it detects the filesize from the Content-Length header. What the documentation doesn't mention is that S3 does not send the Content-Length header by default and so no compression is applied.

Go to S3 and open the properties for your bucket (not for individual files). Expand Permissions and then click Edit CORS Configuration. You need to add Content-Length as an allowed header like this:

Amazon S3 CORS Configuration

(All Code Posts)

Catfood Software Support

Updated on Tuesday, May 4, 2021

Catfood Software Support

Need help with a Catfood Software product? Please leave a comment below.

(All Code Posts)

Catfood Earth 3.41

Updated on Monday, May 31, 2021

Catfood Earth 3.41

Catfood Earth 3.41 fixes a problem that was preventing the weather radar layer from loading.

I've also updated to the latest (2015g) time zone database and the latest time zone map from Eric Muller.

Download the latest Catfood Earth.

(Previously)

(All Code Posts)