Skip to content

Instantly share code, notes, and snippets.

@epicserve
Last active September 23, 2020 19:37
Show Gist options
  • Save epicserve/b1962de136aa55dc866b72187012a158 to your computer and use it in GitHub Desktop.
Save epicserve/b1962de136aa55dc866b72187012a158 to your computer and use it in GitHub Desktop.
Download your unsplash liked images
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