def test_photos_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) photos = pu_obj.photos(type_=type) assert photos.body is not None assert photos.header is not None assert photos.status_code == 200 for photo in photos.entries: # if any of the fields breaks, then it's a problem print(photo.id, photo.link_html, photo.link_download, photo.link_download_location ) # , photo.stats # TODO: include stats in unit test
def unsplash_parse_resp(subject): """ From Unsplash API, collect the top 4 images from results. :param subject: The subject to be used for the image search or type(None). If None, random photos are fetched. :rtype images: A list containing data on the fetched images. :except AttributeErrror: Occurs when resp fails to fetch images and enumerate cannot parse resp. """ py_un = PyUnsplash(api_key=UNSPLASH_CLIENT_ID) images = [] if subject is not None: resp = py_un.search("photos", query=subject, per_page=4) else: resp = py_un.photos(type_="random", count=4) # Gather data from resp object. try: for num, item in enumerate(resp.entries, 1): image_info = { "author_name": item.body["user"]["name"], "full_image": item.body["urls"]["full"], "image_id": item.id, "author_profile": f"{item.body['user']['links']['html']}?utm_source=Wallie&utm_medium=referral", "download_location": item.link_download_location, } images.append(image_info) return images except AttributeError as err: handle_err( f"Failed to parse unsplash resp object: {err}\nCheck that your API_KEYs are setup correctly." )
def get_link(self): pu = PyUnsplash(self.api_key) photos = pu.photos(type_='random', order_by='popular', orientaion='landscape', count=3) link = [] for photo in photos.entries: photo.refresh() data = {'Photo Id': photo.id, 'Url': photo.link_download} link.append(data) #print(data['Url']) print(link)
def getImages(client_id): py_un = PyUnsplash(api_key=client_id) logging.getLogger("pyunsplash").setLevel(logging.DEBUG) # retrieve 4 random photos, which are featured, and tagged as "dog" return py_un.photos(type_='random', count=1, query='noir')
# Author: Salvatore Ventura <*****@*****.**> # Date: 5/9/2020 # Purpose: Example file for SinglePhoto # # Revision: 1 # Comment: What's new in revision 1 # ############################################################################### import logging import os from pyunsplash import PyUnsplash api_key = os.environ.get('APPLICATION_ID', None) or 'DUMMY_APPLICATION_ID' # 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) # instantiate PyUnsplash object py_un = PyUnsplash(api_key=api_key) # Get a single photo from a known ID photo = py_un.photos(type_="single", photo_id='l0_kVknpO2g') print(photo.entries.get_attribution(format='txt')) print(photo.entries.get_attribution(format='html'))