Last active
November 6, 2021 15:56
-
-
Save mutsune/63093e054311e3367895dbb904f8eef2 to your computer and use it in GitHub Desktop.
Get photo URLs from unsplash collections
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
#! /usr/bin/env bash | |
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" | |
function exec_uncollections() { | |
ACCESS_KEY="$(security find-generic-password -a "${USER}" -s unsplash-uncollections-access-key -w)" | |
export ACCESS_KEY | |
python3 ./uncollections.py | |
} | |
DOWNLOAD_DIR="$(mktemp -d -p /tmp wallpaper.XXXXXXXXXX)" | |
URLS="$(exec_uncollections)" | |
echo "${URLS}" > "${DOWNLOAD_DIR}/url.list" | |
URL="$(echo "${URLS}" | shuf -n 1)" | |
echo "Download from: ${URL}" | |
WALLPAPER_FILE_FILE="$(wget -nv -P "${DOWNLOAD_DIR}" "${URL}" 2>&1 | cut -d\" -f2)" | |
if find "${WALLPAPER_FILE_FILE}"; then | |
wallpaper set "${WALLPAPER_FILE_FILE}" | |
fi |
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 os | |
from urllib import request | |
import urllib.parse | |
import json | |
from itertools import chain | |
ACCESS_KEY = os.getenv("ACCESS_KEY") | |
COLLECTION_IDS = [ | |
"220381", # https://unsplash.com/collections/220381/earth-is-awesome | |
"11649432", # https://unsplash.com/collections/11649432/landscape | |
"219941", # https://unsplash.com/collections/219941/architecture%2C-buildings%2C-spaces | |
"3178572", # https://unsplash.com/collections/3178572/winter | |
"1198107", # https://unsplash.com/collections/1198107/the-view-from-in-here | |
"1118894", # https://unsplash.com/collections/1118894/superior-interior | |
"894", # https://unsplash.com/collections/894/earth-%26-planets | |
] | |
def api_request(url: str, access_key: str, timeout: int = 3): | |
r = request.Request( | |
url, headers={"Authorization": f"Client-ID {access_key}"}, method="GET" | |
) | |
res = request.urlopen(r, timeout=timeout) | |
return res.read().decode("utf-8") | |
def get_collection_photos( | |
collection_id: str, | |
per_page: int = 10000, | |
orientation: str = "landscape", | |
): | |
""" | |
response sample: | |
[ | |
{ | |
"id": "xxxxxxxx", | |
"width": 6760, | |
"height": 4507, | |
"color": "#738ca6", | |
"urls": { | |
"raw": "https://images.unsplash.com/photo-xxxx?ixid=xxxxx&ixlib=xxxx", | |
"full": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=srgb&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=85", | |
"regular": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=1080", | |
"small": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=400", | |
"thumb": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=200" | |
}, | |
... | |
}, | |
{ | |
"id": "xxxxxxxx", | |
"width": 6760, | |
"height": 4507, | |
"color": "#738ca6", | |
"urls": { | |
"raw": "https://images.unsplash.com/photo-xxxx?ixid=xxxxx&ixlib=xxxx", | |
"full": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=srgb&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=85", | |
"regular": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=1080", | |
"small": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=400", | |
"thumb": "https://images.unsplash.com/photo-xxxx?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=xxxxx&ixlib=xxxx&q=80&w=200" | |
}, | |
... | |
""" | |
params = { | |
"per_page": per_page, | |
"orientation": orientation, | |
} | |
escaped_param_str = urllib.parse.urlencode(params) | |
url = f"https://api.unsplash.com/collections/{collection_id}/photos?{escaped_param_str}" | |
return api_request(url, ACCESS_KEY) | |
def get_photo_urls(collection_id: str): | |
content = get_collection_photos(collection_id) | |
d = json.loads(content) | |
return [p["urls"]["raw"] for p in d] | |
def main(): | |
urls = chain.from_iterable(get_photo_urls(_id) for _id in COLLECTION_IDS) | |
for url in set(urls): | |
print(url) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create access_key https://unsplash.com/oauth/applications