Last active
July 31, 2018 02:19
-
-
Save TrungNguyen1909/c10fcd10726af0c240316f183033f186 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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