コード例 #1
0
ファイル: test_collections.py プロジェクト: sdan/pyunsplash
    def test_collections_curated(self):
        type = 'curated'
        resource_filepath = self.store_mapping[type]
        stored_response = json.loads(open(resource_filepath).read())

        responses.add(
            responses.GET,
            '{}{}'.format(
                API_ROOT,
                stored_response.get('url').split('?')[0]
            ),  # cheating on the url, because the class always inits without query params
            json=stored_response.get('body'),
            status=stored_response.get('status_code'),
            content_type='application/json',
            adding_headers=stored_response.get('headers'))
        pu_obj = PyUnsplash(api_key=api_key)
        collections = pu_obj.collections(type_=type)
        assert collections.body is not None
        assert collections.header is not None
        assert collections.status_code == 200
        for collection in collections.entries:
            print(collection.id, collection.title, collection.description,
                  collection.user, collection.link_photos,
                  collection.link_related)
        assert collections.link_next is not None
        assert collections.link_previous is not None
        assert collections.link_first is not None
        assert collections.link_last is not None
コード例 #2
0
# If you need to change that, use getLogger/setLevel
# on the module logger, like this:
logging.getLogger(PyUnsplash.logger_name).setLevel(logging.DEBUG)


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

# Get a page from the collections api
# Parameters:
#   'type' : 'generic', 'curated', 'featured'
#            default: 'generic'
#   'page' : <int>, which page to retrieve
#   'per_page': <int>, how many collections to include in each page
#
collections_page = py_un.collections(type_='generic', per_page=3)

# iterate through all the collections retrieved in the collections_page, two ways
# 1) iterating through 'body' returns just a dictionary, no additional API call
for collection in collections_page.body:
    print('Collection as dictionary', collection.get('id'), collection.get('title'))

# 2) iterating through 'entries' returns an instance of 'Collection'
for collection in collections_page.entries:
    print('Collection as object', collection.id, collection.title)

    # a Collection object allows for a richer interaction:
    # follow the 'related' collections, if any
    related_collections = collection.related
    # it's a Collections object too
    for related in related_collections.entries:
コード例 #3
0
import logging
from pyunsplash import PyUnsplash
api_key = '#######################'

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

# 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").setLevel(logging.DEBUG)

# 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=20)
while collections.has_next:
    for collection in collections.entries:
        photos = collection.photos()
        for photo in photos.entries:
            print(photo.link_download)

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