-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallpaper.py
54 lines (39 loc) · 1.4 KB
/
wallpaper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import requests
import shutil # to save image locally
import json
import random
def get_url(page):
url = f'https://unsplash.com/napi/search/photos?query=nepal&xp=feedback-loop-v2%3Aexperiment&per_page=20&page={page}&orientation=landscape'
return url
def get_random_page():
response = requests.get(get_url(1))
response_json = json.loads(response.text)
total_pages = response_json['total_pages']
page = random.randint(1, total_pages)
return page
def get_random_image():
page = get_random_page()
response = requests.get(get_url(page))
response_json = json.loads(response.text)
image = random.choice(response_json['results'])['urls']['raw']
return image
def save_image():
image = get_random_image()
# stream = True, to return the stream content.
r = requests.get(image, stream=True)
if r.status_code == 200:
# decode_content = True, to ensure the downloaded image size will not be zero.
r.raw.decode_content = True
filename = "wallpaper.jpg"
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return filename
else:
print('Unable to download image')
def set_wallpaper():
filename = save_image()
cmd = f"/usr/bin/gsettings set org.gnome.desktop.background picture-uri 'file://{os.getcwd()}/{filename}'"
os.system(cmd)
if __name__ == '__main__':
set_wallpaper()