Code Monkey home page Code Monkey logo

unsplash-bridge's Introduction

Unsplash Bridge

This repository contains Cloudflare Worker that picks photos from Unsplash collections via Unsplash API.

Cloudflare Workers are like serverless cloud functions, but they also can cache stuff in Cloudflare CDN to make things faster. If you are not familiar with Cloudflare Workers, visit Cloudflare Workers documentation. Or you can just press the button instead:

Deploy to Cloudflare Workers

Motivation

This is a bridge to Unsplash Image API which respects Unsplash API Guidelines.

Current implementation solves following problems:

  • Hides Unsplash API Access Key on server side, so you don't need to pass access key to your client-side application, which is not secure and forbidden by Unsplash API Guidelines.
  • Automatically notifies Unsplash about photo downloads, which is also mandatory by Unsplash. It means when a photo is picked or used for something, you should notify Unsplash about this. The worker handles this automatically. See "Guideline: Triggering a Download" for more details.

It was developed to be used for Camellia needs, so the current subset of features is pretty limited.

API

The bridge exposes following endpoints:

GET /random-collection-entry

Returns random photo from passed Unsplash collection.

It also notifies Unsplash about downloads. See "Guideline: Triggering a Download" for more details.

A response is cached on clients for 12 hours with Cache-Control header.

It accepts following query-parameters:

  • id โ€” collection ID.

Example:

GET https://your-worker-name.workers.dev/random-collection-entry?id=123

Returns following JSON in response:

{
  "image": {
    "url": "https://images.unsplash.com/photo-1634482895955-712847090dfd?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwxNDQwNjN8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NjA4NTA5OTc&ixlib=rb-1.2.1&q=80",
    "resolution": {
      "width": 3591,
      "height": 2394
    }
  },
  "photographer": {
    "name": "Nick Night",
    "url": "https://unsplash.com/@lvenfoto",
    "avatar": {
      "small": {
        "url": "https://images.unsplash.com/profile-1575888295849-0025f8946dfcimage?ixlib=rb-1.2.1&crop=faces&fit=crop&w=32&h=32",
        "resolution": {
          "width": 32,
          "height": 32
        }
      },
      "medium": {
        "url": "https://images.unsplash.com/profile-1575888295849-0025f8946dfcimage?ixlib=rb-1.2.1&crop=faces&fit=crop&w=64&h=64",
        "resolution": {
          "width": 64,
          "height": 64
        }
      },
      "large": {
        "url": "https://images.unsplash.com/profile-1575888295849-0025f8946dfcimage?ixlib=rb-1.2.1&crop=faces&fit=crop&w=128&h=128",
        "resolution": {
          "width": 128,
          "height": 128
        }
      }
    }
  },
  "webPageUrl": "https://unsplash.com/photos/y3sKIJqiY40"
}

Developing the worker

Clone the repository. Then, install dependencies:

npm ci

Start webpack:

webpack watch

Open another terminal and then run the worker in development mode:

wrangler dev

unsplash-bridge's People

Contributors

flaksp avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

Andy Callaghan avatar Tanay Lakhani avatar Tony Thomas avatar NOISE avatar

unsplash-bridge's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.