Ejemplo n.º 1
0
    def test_curated(self):
        index = 'curated'
        resource_filepath = self.store_mapping[index]
        stored_response = json.loads(open(resource_filepath).read())

        responses.add(
            responses.GET,
            '{}/{}{}'.format(API_ROOT, API_VERSION, stored_response.get('_url')),
            # _url contains only the short path like /curated?page=2&per_page=5
            json=stored_response.get('body'),
            status=stored_response.get('status_code'),
            content_type='application/json',
            adding_headers=stored_response.get('headers'),
            match_querystring=True,
        )
        py_pexels = PyPexels(api_key=api_key)
        curated_results_page = py_pexels.curated(page=2, per_page=5)

        # Page properties
        print(curated_results_page.page)
        print(curated_results_page.per_page)
        print(curated_results_page.has_next)
        print(curated_results_page.has_previous)
        print(curated_results_page.link_self)
        print(curated_results_page.link_first)
        print(curated_results_page.link_last)
        print(curated_results_page.link_next)
        print(curated_results_page.link_previous)

        # Entries
        for photo in curated_results_page.entries:
            print(photo.id, photo.photographer, photo.width, photo.height, photo.url)
            print(photo.src)
Ejemplo n.º 2
0
    def test_curated(self):
        index = 'curated'
        resource_filepath = self.store_mapping[index]
        stored_response = json.loads(open(resource_filepath).read())

        responses.add(
            responses.GET,
            'https://api.pexels.com/v1/curated?page=2&per_page=5',
            json=stored_response,
            status=200,
            content_type='application/json',
            adding_headers={'Authorization': api_key},
            match_querystring=True,
        )
        py_pexels = PyPexels(api_key=api_key)
        curated_results_page = py_pexels.curated(page=2, per_page=5)

        # Page properties
        print(curated_results_page.page)
        print(curated_results_page.per_page)
        print(curated_results_page.has_next)
        print(curated_results_page.has_previous)
        print(curated_results_page.link_self)
        print(curated_results_page.link_first)
        print(curated_results_page.link_last)
        print(curated_results_page.link_next)
        print(curated_results_page.link_previous)

        # Entries
        for photo in curated_results_page.entries:
            print(photo.id, photo.photographer, photo.width, photo.height, photo.url)
            print(photo.src)
Ejemplo n.º 3
0
OUTPUT_DIR = os.path.abspath(__file__)

px = PyPexels(api_key=API_KEY)


def get_image(img_url):
    # open
    img = Image.open(requests.get(img_url, stream=True).raw)

    # determine filename
    filename = img_url.split('/')[-1]
    os.path.join(OUTPUT_DIR, filename)

    # save the image
    img.save(filename)
    # print success message
    print(f"{filename} saved.")


if __name__ == '__main__':

    popular_photos = px.curated(per_page=30)

    img_url_list = [photo.src['original'] for photo in popular_photos.entries]

    pool = Pool(processes=5)
    pool_output = pool.map(get_image, img_url_list)
    pool.close()
    pool.join()