PyUnsplash

An open source Python wrapper for the Unsplash REST API.

The source code is available on GitHub at https://github.com/salvoventura/pyunsplash.

Installation

PyUnsplash is available on PyPI and thus can be installed with pip on most platforms.

$ pip install pyunsplash

Dependencies

This library depends on Requests to make - well - requests to the Unsplash API. This additional package should be automatically installed at installation time, or you can simply install it by:

$ pip install requests

Usage

Creating an instance

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash Application ID>')

API keys can be obtained from Unsplash Developers.


Authorization workflow

TODO


Error handling

TODO


Library logging

The PyUnsplash library internal logging subsystem is driven by the application. The default logging level of the library is set to be logging.ERROR. If you want to access the library logging subsystem, you can fine-tune the logger with id PyUnsplash.logger_name as per the following example:

from pyunsplash import PyUnsplash

# Initialize app logging
logger = logging.getLogger()
logging.basicConfig(filename='app.log', level=logging.DEBUG)

# pyunsplash logger defaults to level logging.ERROR
# If you need to change that, use getLogger/setLevel
# on the module logger, like this:
logging.getLogger(PyUnsplash.logger_name).setLevel(logging.DEBUG)

pu = PyUnsplash(api_key='<your Unsplash Application ID>')
# ... continue

Examples

This example shows how the interaction with the paging functionality of the unsplash API is greatly abstracted and simplified. The code below will iterate through all collections, and retrieve each photo in there, and print their download link.

import logging
from pyunsplash import PyUnsplash
api_key = 'YOUR_APPLICATION_ID'

# instantiate PyUnsplash object
py_un = PyUnsplash(api_key=api_key)

# Start with the generic collection, maximize number of items
# note: this will run until all photos of all collections have
#       been visited, unless a connection error occurs.
#       Typically the API hourly limit gets hit during this
#
collections = py_un.collections(per_page=30)
while collections.has_next:
    for collection in collections.entries:
        photos = collection.photos()
        for photo in photos.entries:
            print collection.title, photo.link_download, photo.get_attribution()

    # no need to specify per_page: will take from original object
    collections = collections.get_next_page()

API: Class PyUnsplash

This is the main class used to interact with the Unsplash REST API.

PyUnsplash(api_key)

Create an instance of class PyUnsplash. The api_key parameter is required. API keys can be obtained from Unsplash Developers.

Parameters

Argument Type Optional/Required Notes
api_key string required Your application ID

Returns

Object Instance of class PyUnsplash

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

Methods and properties

Methods and properties exposed by the PyUnsplash class.

PyUnsplash.user(username, w, h)

To interact with the user API, create an instance of class User.

Parameters

Argument Type Optional/Required Notes
username string required The user’s username.
w number optional Profile image width in pixels.
h number optional Profile image height in pixels.

Returns

Object Instance of class User

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)

PyUnsplash.collections(type_, page, per_page)

To interact with the collections API, create an instance of class Collections.

Parameters

Argument Type Optional/Required Notes
type_ string optional (default: generic) generic, featured (curated has been deprecated)
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)

Returns

Object

Instance of class Collections, CuratedCollections, or FeaturedCollections depending on the value of type

NOTE curated endpoint has been deprecated by Unsplash

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

# retrieve a page from the featured collections, with a maximum
# of 5 collections per-page
collections_page = pu.collections(type_='featured', per_page=5)

PyUnsplash.photos(type_, page, per_page, order_by)

To interact with the photos API, create an instance of class Photos.

NOTE curated endpoint has been deprecated by Unsplash

Generic Photos

Parameters

Argument Type Optional/Required Notes
type_ string optional (default: generic) generic
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)
order_by string optional (default: latest) latest, oldest, popular

Returns

Object

Instance of class Photos depending on the value of type

NOTE curated endpoint has been deprecated by Unsplash

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

# retrieve a page from the generic photos, with a maximum
# of 15 photos per-page
collections_page = pu.photos(type_='generic', per_page=15)
Random Photos

Parameters

Argument Type Optional/Required Notes
type_ string required random
collections string optional Public collection ID(s) to filter selection If multiple, comma-separated
featured bool optional Limit selection to featured photos Completely remove if you don’t want featured Setting to False doesn’t work
username string optional Limit selection to a single user
query string optional Limit selection to photos matching a search term
orientation string optional landscape, portrait, squarish
count number required Number of items per page (max: 30)

Note You can’t use the collections and query parameters in the same call

Returns

Object Instance of class RandomPhotos

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

# retrieve 4 random photos, which are featured, and tagged as "dog"
collections_page = pu.photos(type_='random', count=4, featured=True, query="dog")
for photo in photos.entries:
    print(photo.id, photo.link_download)

Single Photo

Parameters

Argument Type Optional/Required Notes
type_ string required single
photo_id string required ID of the photo you want to load

Returns

Object Instance of class SinglePhoto

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

# retrieve specific photo by its ID, and print the attribution
photo = py_un.photos(type_="single", photo_id='l0_kVknpO2g')
print(photo.entries.get_attribution(format='txt'))
print(photo.entries.get_attribution(format='html'))

PyUnsplash.search(type_, type, page, per_page, query)

To interact with the search API, create an instance of class Search.

Parameters

Argument Type Optional/Required Notes
type_ string required photos, collections, users
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)
query string optional Search terms

Returns

Object Instance of class Search

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

#
#
search = pu.search(type_='photos', query='red,car')
for entry in search.entries:
    print entry.link_html

PyUnsplash.stats()

To interact with the stats API, create an instance of class Stats.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

Object Instance of class Stats

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')

stats = pu.stats()
print stats.total   # this is json

API: Class Collection

This class is used to interact with individual Collection objects typically returned by PyUnsplash as part of Collections object entries.

Methods and properties

Methods and properties exposed by the Collection class.

Collection.refresh()

Reload the full object from its REST API URI. Required if there has been an update to the object, or if you need to retrieve the full object starting from a simplified content copy coming from a list.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

None  

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    collection.refresh()
    print collection.id

Collection.id

Unique identifier for this user resource.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string unique resource identifier

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    print collection.id

Collection.title

Title of this collection.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string title of this collection

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    print collection.title

Collection.description

Description for this collection.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string description for this collection

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    print collection.description

Collection.user

User who created this collection.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string id of user who created this collection

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    print collection.user



Collection.photos(page, per_page, order_by)

Allows easy access to each photo in this Collection. Returns Photos collection object from this collection’s link_photos url.

Parameters

Argument Type Optional/Required Notes
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)
order_by string optional (default: latest) latest, oldest, popular

Returns

Object Instance of class Photos

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
collections_page = pu.collections(type_='featured', per_page=5)
for collection in collections_page.entries:
    photos = collection.photos(order_by='popular', per_page=3)
    for photo in photos.entries:
        print photo.id, photo.link_download

API: Class Collections

This class is used to interact with the Unsplash /collections REST API but also with lists of Collection objects as returned by certain unsplash REST APIs.

The constructor can be invoked through the main PyUnsplash class as PyUnsplash.collections(type, page, per_page).

The following class methods/properties in PyUnsplash also return a Collections class object:
  • Collection.related()
  • User.collections(page, per_page, order_by)

Methods and properties

Methods and properties exposed by the Collections class.

Collections.entries

Returns an iterator for the Collection objects contained in this Collections instance

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

iterator each time an instance of class Collection

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
collections = this_user.collections(page=1, per_page=5)
for collection in collections.entries:
    print collection.id, collection.title

API: Class Photo

This class is used to interact with individual Photo objects typically returned by PyUnsplash as part of Photos object entries.

Methods and properties

Methods and properties exposed by the Photo class.

Photo.refresh()

Reload the full object from its REST API URI. Required if there has been an update to the object, or if you need to retrieve the full object starting from a simplified content copy coming from a list.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

None  

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos()    # photos is an instance of class Photos
for photo in photos.entries:
    photo.refresh()
    print photo.id, photo.link_download

Photo.id

Unique identifier for this user resource.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string unique resource identifier

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos()    # photos is an instance of class Photos
for photo in photos.entries:
    photo.refresh()
    print photo.id, photo.link_download



Photo.stats

Retrieve a single photo’s stats.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

json JSON encoded stats information about this photo

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos()    # photos is an instance of class Photos
for photo in photos.entries:
    print photo.stats

Photo.get_attribution(format=’txt’)

Generate and return a standard attribution string according to ‘format’ parameter.

Note format parameter value ‘str’ has been deprecated in favor of ‘txt’.

Parameters

Argument Type Optional/Required Notes
format string optional Valid values: ‘txt’, ‘html’

Returns

string Text or HTML standard attribution string.

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos()    # photos is an instance of class Photos
for photo in photos.entries:
    print photo.get_attribution()               #    Photo by salvatore ventura on Unsplash
    print photo.get_attribution(format='txt')   #    Photo by salvatore ventura on Unsplash
    print photo.get_attribution(format='html')  #    <span>Photo by <a href="https://unsplash.com/@salvoventura">salvatore ventura</a> on <a href="https://unsplash.com/@salvoventura">Unsplash</a></span>

Photo.body

This is the full object returned by the API in JSON format.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string Photo object returned by API, in JSON format

API: Class Photos

This class is used to interact with the Unsplash /photos REST API but also with lists of Photo objects as returned by certain unsplash REST APIs.

The constructor can be invoked through the main PyUnsplash class as PyUnsplash.photos(type, page, per_page, order_by).

The following class methods/properties in PyUnsplash also return a Photos class object:
  • User.likes(page, per_page, order_by)
  • User.photos(page, per_page, order_by)

Methods and properties

Methods and properties exposed by the Photos class.

Photos.entries

Returns an iterator for the Photo objects contained in this Photos instance

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

iterator each time an instance of class Photo

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos()    # photos is an instance of class Photos
for photo in photos.entries:
    print photo.id, photo.link_download

API: Class Stats

This class is used to interact with the Unsplash /stats/total REST API.

The constructor is invoked through the main PyUnsplash class as PyUnsplash.stats()

Methods and properties

Methods and properties exposed by the Stats class.

Stats.total

Description

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

json JSON encoded stats information about Unsplash.com

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
stats = pu.stats()
print stats.total

Stats.body

This is the full object returned by the API in JSON format.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string Stats object returned by API, in JSON format

API: Class User

This class is used to interact with the Unsplash /users/:username REST API.

The constructor is invoked through the main PyUnsplash class as PyUnsplash.user(username, w, h)

Methods and properties

Methods and properties exposed by the User class.

User.refresh()

Reload the full object from its REST API URI. Required if there has been an update to the object, or if you need to retrieve the full object starting from a simplified content copy coming from a list.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

None  

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
this_user.refresh()

User.id

Unique identifier for this user resource.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string unique resource identifier

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
print this_user.id







User.photos(page, per_page, order_by)

Allows easy access to each photo from this user. Returns Photos collection object from this user’s link_photos url.

Parameters

Argument Type Optional/Required Notes
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)
order_by string optional (default: latest) latest, oldest, popular

Returns

Object Instance of class Photos

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.photos(per_page=5)
for photo in photos.entries:
    print photo.id, photo.link_download

User.followers()

Allows easy access to each user that is a follower of this user. Returns Users collection object from this user’s link_followers url.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

Object Instance of class Users

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
followers = this_user.followers()
for user in followers.entries:
    print user.id, user.body.get('first_name'), user.body.get('last_name')

User.following()

Allows easy access to each user that is followed by this user. Returns Users collection object from this user’s link_following url.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

Object Instance of class Users

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
following = this_user.following()
for user in following.entries:
    print user.id, user.body.get('first_name'), user.body.get('last_name')

User.likes(page, per_page, order_by)

Allows easy access to each photo liked by this user. Returns Photos collection object.

Parameters

Argument Type Optional/Required Notes
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)
order_by string optional (default: latest) latest, oldest, popular

Returns

Object Instance of class Photos

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
photos = this_user.likes(per_page=5)
for photo in photos.entries:
    print photo.id, photo.link_download

User.collections(page, per_page, order_by)

Allow easy access to the collections created by this user. Returns Collections object.

Parameters

Argument Type Optional/Required Notes
page number optional (default: 1) Page number to retrieve.
per_page number optional (default: 10) Number of items per page (max: 30)

Returns

Object Instance of class Collections

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
collections = this_user.collections(page=1, per_page=5)
for collection in collections.entries:
    print collection.id, collection.title

User.statistics(resolution, quantity)

Retrieve the consolidated number of downloads, views and likes of this user’s photos, as well as the historical breakdown and average of these stats in a specific timeframe (default is 30 days).

Returns UserStatistics object.

Parameters

Argument Type Optional/Required Notes
resolution number optional (default: days) The frequency of the stats.
quantity number optional (default: 30) The amount of for each stat.

NOTE Currently, the only resolution param supported is days and the quantity param can be any number between 1 and 30.

Returns

Object Instance of class UserStatistics

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
user_statistics = this_user.statistics()

User.body

This is the full object returned by the API in JSON format.

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

string User object returned by API, in JSON format

API: Class Users

This class is used to interact with lists of User objects as returned by certain unsplash REST APIs.

The following class methods/properties in PyUnsplash return a Users class object:
  • User.followers()
  • User.following()

Methods and properties

Methods and properties exposed by the Users class.

Users.entries

Returns an iterator for the User objects contained in this Users instance

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

iterator each time an instance of class User

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura', w=100, h=100)
followers = this_user.followers()    # followers is an instance of class Users
for user in followers.entries:
    print user.id, user.body.get('first_name'), user.body.get('last_name')

API: Class UserStatistics

This class is used to interact with the Unsplash /users/:username/statistics REST API.

The constructor is invoked through the PyUnsplash.User class as PyUnsplash.User.statistics()

Methods and properties

Methods and properties exposed by the UserStatistics class.

UserStatistics.downloads

Description

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

dict Dictionary containing statistic data from downloads

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura')
user_statistics = this_user.statistics()
print (user_statistics.downloads.get('total'))
print (user_statistics.downloads.get('historical', {}).get('change', None))

UserStatistics.views

Description

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

dict Dictionary containing statistic data from views

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura')
user_statistics = this_user.statistics()
print (user_statistics.views.get('total'))
print (user_statistics.views.get('historical', {}).get('change', None))

UserStatistics.likes

Description

Parameters

Argument Type Optional/Required Notes
N/A      

Returns

dict Dictionary containing statistic data from likes

Example

import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
this_user = pu.user('salvoventura')
user_statistics = this_user.statistics()
print (user_statistics.likes.get('total'))
print (user_statistics.likes.get('historical', {}).get('change', None))

Version

PyUnsplash v1.0.0rc2 (release candidate, v2)

This is the second release candidate for production ready 1.0.0 of PyUnsplash. Thanks to all the early adopters and bug reporters: thanks to you there have been a number of improvements and bugfixes and we are progressing the rc process.

This release brings mainly one bugfix:

  • #13 search.get_next_page() does not work

Todo - Authorization workflow & Current user - Write operations

License

PyUnsplash is released under the MIT License.