Download Unsplash Photos to Google Drive

Unsplash is the best source for free images on the Internet. The images have the Creative Commons zero license meaning you can do anything with the photos.

This Google Script uses the Unsplash API to fetch the most recently uploaded photos and downloads them to your Google Drive. The photo details, like the height, width, creator name, full RAW link, etc. are appended to a Google Spreadsheet.

You can set this is a time-based trigger to automatically save all the new Unsplash photos in your Google Drive. Change the page parameter to download all the old pictures as well. You would however need to create your CLIENT_ID for the API call.

function getUnsplashPhotos() {

    try {

        var ss = SpreadsheetApp.getActiveSheet();

        // Fetch a maximum of 30 photos per API call
        var url = "https://api.unsplash.com/photos/?client_id=API_CLIENT_ID&per_page=30&page=1";

        // Parse the JSON response in an Array
        var photos = JSON.parse(UrlFetchApp.fetch(url).getContentText());

        for (var p = 0; p < photos.length; p++) {

            var categories = [],
                photo = photos[p];

            for (var c = 0; c < photo.categories.length; c++) {
                categories.push(photo.categories[c].title);
            }

            var blob = UrlFetchApp.fetch(photos.urls.full).getBlob();

            var file = DriveApp.createFile(blob);

            file.setName(photos.user.name);

            var row = [
                photo.id,
                photo.created_at.substr(0, 10),
                categories.join(", "),
                photo.width,
                photo.height,
                photo.color, // Main Color Hex Mode
                photo.likes, // How popular is the photograph
                photo.user.name, // Credit the photographer
                photo.user.links.html,
                photo.urls.raw, // Full high res version URL
                photo.urls.full,
                file.getUrl() // URL of the photo in Google Drive
            ];

            ss.appendRow(row);

        }

    } catch (f) {
        Logger.log(f.toString());
    }
}