1 | #!/usr/bin/env python3 |
2 | """Performs automatic Unsplash queries for a desktop wallpaper slideshow""" |
3 |
|
4 | import time |
5 | import sys |
6 | import os |
7 | import random |
8 | import requests |
9 |
|
10 | UNSPLASH_API = "https://api.unsplash.com/search/photos" |
11 |
|
12 | REQUEST_HEADERS = {"Accept-Version": "v1"} |
13 | REQUEST_PAYLOAD = {"per_page": 10, "orientation": "landscape"} |
14 |
|
15 | CROP_PARAMS = "&fm=png&fit=crop&w=3840&h=1080" |
16 |
|
17 |
|
18 | def fetch_images(api_key: str, query: str, page_num: int): |
19 | """Make Unsplash API request for image search results""" |
20 |
|
21 | # prepare payload |
22 | page_headers = REQUEST_HEADERS |
23 | page_headers["Authorization"] = f"Client-ID {api_key}" |
24 |
|
25 | page_params = REQUEST_PAYLOAD |
26 | page_params["page"] = page_num |
27 | page_params["query"] = query |
28 |
|
29 | # make request |
30 | result = requests.get(UNSPLASH_API, headers=page_headers, params=page_params) |
31 |
|
32 | # handle API error |
33 | if result.status_code != 200: |
34 | print("unsplash API error:") |
35 | print(result.json) |
36 | return 1 |
37 |
|
38 | # parse JSON |
39 | json = result.json() |
40 | images = [] |
41 | for image in json["results"]: |
42 | images.append(image["urls"]["raw"] + CROP_PARAMS) |
43 |
|
44 | return images |
45 |
|
46 |
|
47 | def display_image(url: str): |
48 | """Download image by URL to /tmp/unsplash_bg.png and set as background""" |
49 |
|
50 | # download |
51 | image = requests.get(url, stream=True) |
52 | if image.status_code != 200: |
53 | print("unsplash image error") |
54 | print(image) |
55 | return 1 |
56 |
|
57 | # save to /tmp/unsplash_bg.png |
58 | with open("/tmp/unsplash_bg.png", "wb") as image_file: |
59 | for chunk in image: |
60 | image_file.write(chunk) |
61 |
|
62 | # use gsettings to set as new background |
63 | os.system( |
64 | "/usr/bin/gsettings set org.gnome.desktop.background picture-uri /tmp/unsplash_bg.png" |
65 | ) |
66 | return 0 |
67 |
|
68 |
|
69 | def main(): |
70 | """Main controlling logic, handles command arguments""" |
71 | api_key = "" |
72 | interval = 60 |
73 | queries = [] |
74 | if len(sys.argv) < 3: |
75 | print( |
76 | "usage: ./unsplash-background.py <unsplash-api-key> <second-interval> [keywords]+" |
77 | ) |
78 | sys.exit(1) |
79 | else: |
80 | try: |
81 | api_key = str(sys.argv[1]) |
82 | interval = int(sys.argv[2]) |
83 | queries = [str(q) for q in sys.argv[3:]] |
84 | except ValueError: |
85 | print( |
86 | "usage: ./unsplash-background.py <unsplash-api-key> <second-interval> [keywords]+" |
87 | ) |
88 | sys.exit(1) |
89 | page_num = 0 |
90 | while True: |
91 | page_num += 1 |
92 | images = [] |
93 | for query in queries: |
94 | search = None |
95 | while True: |
96 | # repeat request every 15 secs until 200 if error |
97 | search = fetch_images(api_key, query, page_num) |
98 | if search == 1: |
99 | time.sleep(15) |
100 | else: |
101 | break |
102 | images += search |
103 | random.seed() |
104 | random.shuffle(images) |
105 | for image in images: |
106 | if display_image(image) == 0: |
107 | time.sleep(interval) |
108 |
|
109 |
|
110 | if __name__ == "__main__": |
111 | main() |
112 |
|