Skip to content

Instantly share code, notes, and snippets.

@TrungNguyen1909
Last active July 31, 2018 02:19
Show Gist options
  • Save TrungNguyen1909/c10fcd10726af0c240316f183033f186 to your computer and use it in GitHub Desktop.
Save TrungNguyen1909/c10fcd10726af0c240316f183033f186 to your computer and use it in GitHub Desktop.
from urllib.request import urlopen,urlretrieve
import urllib
import sys
import json
key="ACCESS_KEY"
width=1920
height=1080
url="https://api.unsplash.com/photos/random?orientation=landscape&w="+str(width)+"&h="+str(height)+"&client_id="+key
response=""
try:
response=urlopen(url)
except urllib.error.URLError:
print("Cannot connect to image server. Check internet connection.",file=sys.stderr)
sys.exit(1)
if response:
d=json.load(response.fp)
imageurl=d["urls"]["full"]
try:
urlretrieve(url=imageurl,filename="background.jpg")
sys.exit(0)
except urllib.error.URLError:
print("Cannot connect to image server. Check internet connection.",file=sys.stderr)
sys.exit(1)
else:
print("Request is null",file=sys.stderr)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment