Last active
August 22, 2025 04:37
-
-
Save ronsen/bc2df782e482e3de78adf70012058f5d to your computer and use it in GitHub Desktop.
Get the latest Bing wallpaper
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 sys | |
| import requests | |
| import string | |
| import subprocess | |
| from pathlib import Path | |
| debug = True | |
| url = "https://peapix.com/bing/feed?country=us&n=1" | |
| pictures_dir = Path.home() / "pictures/bing" | |
| pictures_dir.mkdir(parents=True, exist_ok=True) | |
| def main(url: string, debug: bool): | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| if response.status_code == 200: | |
| data = response.json() | |
| image = data[0] | |
| filename = slug(image["title"]) + ".jpg" | |
| path = pictures_dir / filename | |
| if not path.exists(): | |
| resp = requests.get(image["imageUrl"]) | |
| resp.raise_for_status() | |
| if resp.status_code == 200: | |
| with open(path, "wb") as file: | |
| for chunk in resp.iter_content(8192): | |
| file.write(chunk) | |
| if debug: | |
| print(f"File dowloaded: {filename}") | |
| else: | |
| if debug: | |
| print(f"File exists: {filename}") | |
| change_background(path) | |
| else: | |
| if debug: | |
| print(f"Failed to download from {url}") | |
| except Exception as e: | |
| if debug: | |
| print(f"Error: {e}") | |
| def change_background(image_path: str): | |
| monitor = subprocess.getoutput( | |
| "xfconf-query -c xfce4-desktop -l | grep last-image | head -n 1" | |
| ) | |
| subprocess.run( | |
| ["xfconf-query", "-c", "xfce4-desktop", "-p", monitor, "-s", image_path] | |
| ) | |
| def slug(title: str) -> str: | |
| translator = str.maketrans("", "", string.punctuation) | |
| title = title.translate(translator) | |
| return title.replace(" ", "") | |
| def str_to_bool(s: str) -> bool: | |
| if s.lower() in ["true", "1", "t", "y", "yes"]: | |
| return True | |
| elif s.lower() in ["false", "0", "f", "n", "no"]: | |
| return False | |
| else: | |
| return True | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| debug = str_to_bool(sys.argv[1]) | |
| main(url, debug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment