Created
April 17, 2023 14:30
-
-
Save jessestricker/558e8699203dc1bccd3a07f11565f97b to your computer and use it in GitHub Desktop.
Update the GNOME desktop wallpaper with a random image from Unsplash.
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
#! /usr/bin/env python3 | |
import argparse | |
import logging | |
import subprocess | |
from pathlib import Path | |
import dbus | |
import requests | |
Resolution = tuple[int, int] | |
def get_max_screen_res() -> Resolution: | |
bus = dbus.SessionBus() | |
display_config = bus.get_object( | |
"org.gnome.Mutter.DisplayConfig", "/org/gnome/Mutter/DisplayConfig" | |
) | |
_, monitors, _, _ = display_config.GetCurrentState( | |
dbus_interface="org.gnome.Mutter.DisplayConfig" | |
) | |
max_width, max_height = 0, 0 | |
for monitor in monitors: | |
modes = monitor[1] | |
for mode in modes: | |
properties = mode[6] | |
is_current = "is-current" in properties and properties["is-current"] | |
if is_current: | |
width = int(mode[1]) | |
height = int(mode[2]) | |
max_width = max(max_width, width) | |
max_height = max(max_height, height) | |
return max_width, max_height | |
def get_wallpaper_url(screen_res: Resolution) -> str: | |
# id of the *Unsplash Editorial* collection | |
coll_id = 317099 | |
url = f"https://source.unsplash.com/collection/{coll_id}/{screen_res[0]}x{screen_res[1]}" | |
return url | |
def get_wallpaper_file() -> Path: | |
pictures_dir = subprocess.run( | |
["xdg-user-dir", "PICTURES"], check=True, text=True, capture_output=True | |
).stdout.strip() | |
file_name = ".unsplash_wallpaper.jpg" | |
wallpaper_file = (Path(pictures_dir) / file_name).absolute() | |
return wallpaper_file | |
def download(url: str, file: Path): | |
chunk_size = 1024**2 # 1 MiB | |
logging.info("downloading...") | |
resp = requests.get(url, stream=True) | |
with open(file, "wb") as fd: | |
for chunk in resp.iter_content(chunk_size=chunk_size): | |
fd.write(chunk) | |
logging.info("download complete") | |
def apply_wallpaper(file: Path) -> None: | |
schema = "org.gnome.desktop.background" | |
keys = ["picture-uri", "picture-uri-dark"] | |
value = f"file://{file}" | |
for key in keys: | |
subprocess.run(["gsettings", "set", schema, key, value], check=True) | |
logging.info("applied wallpaper setting") | |
def main() -> None: | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--debug", action="store_true", help="print debug information") | |
args = parser.parse_args() | |
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) | |
max_screen_res = get_max_screen_res() | |
logging.debug("max screen res = %s", max_screen_res) | |
wallpaper_url = get_wallpaper_url(max_screen_res) | |
logging.debug("wallpaper url = %s", wallpaper_url) | |
wallpaper_file = get_wallpaper_file() | |
logging.debug("wallpaper file = %s", wallpaper_file) | |
download(wallpaper_url, wallpaper_file) | |
apply_wallpaper(wallpaper_file) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment