Download Unsplash Photos to Google Drive

D
Published in: Google Apps Script - 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());
    }
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Drive

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube