Last active
September 23, 2020 19:37
-
-
Save epicserve/b1962de136aa55dc866b72187012a158 to your computer and use it in GitHub Desktop.
Download your unsplash liked images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import shutil | |
unsplash_username = 'epicserve' | |
url_pattern = 'https://unsplash.com/napi/users/{username}/likes?page={page}&per_page=10&order_by=latest' | |
download_folder = '~/Downloads/unsplash_likes' | |
def download_image(image_url, filename): | |
# Open the url image, set stream to True, this will return the stream content. | |
r = requests.get(image_url, stream=True) | |
# Check if the image was retrieved successfully | |
if r.status_code == 200: | |
# Set decode_content value to True, otherwise the downloaded image file's size will be zero. | |
r.raw.decode_content = True | |
# Open a local file with wb ( write binary ) permission. | |
with open(filename, 'wb') as f: | |
shutil.copyfileobj(r.raw, f) | |
print('Image sucessfully Downloaded: ', filename) | |
else: | |
print('Image Couldn\'t be retreived') | |
num = 0 | |
for i in range(1, 5): | |
url = url_pattern.format(page=i, username=unsplash_username) | |
r = requests.get(url) | |
for photo in r.json(): | |
num += 1 | |
download_image(photo['urls']['raw'], f'photo-{num}.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment