Last active
February 12, 2024 09:43
-
-
Save Dj0ulo/6b3787ef66d8c5291b5d551bb6d0b319 to your computer and use it in GitHub Desktop.
Set daily Bing Wallpaper to Ubuntu
This file contains 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 | |
""" | |
- To get a random wallpaper: | |
$ ./bingwallpaper.py --random | |
- For a daily wallpaper, run: | |
$ crontab -e | |
And add the following : | |
@reboot <path-to-script>/bingwallpaper.py | |
0 */1 * * * <path-to-script>/bingwallpaper.py | |
""" | |
import datetime | |
import json | |
import os | |
from os.path import expanduser | |
import random | |
import requests | |
import subprocess | |
import sys | |
def is_file_modified_today_after_8am(filename): | |
try: | |
modified_time = os.path.getmtime(filename) | |
today = datetime.datetime.today() | |
modified_date = datetime.datetime.fromtimestamp(modified_time) | |
if today.date() == modified_date.date() and modified_date.time() >= datetime.time(8, 0, 0): | |
return True | |
else: | |
return False | |
except OSError: | |
return False | |
def fetch_image(url): | |
"""Fetch image from the web.""" | |
response = requests.get(url) | |
return response.content | |
def save_image(image, filename): | |
"""Save image to disk.""" | |
with open(filename, 'wb') as f: | |
f.write(image) | |
def fetch_wallpaper_info(count = 1, day_offset = 0, market = 'en-US', format = 'js'): | |
""" | |
Fetch Bing wallpaper information from the web. | |
:param count: number of wallpapers to be fetched (max 8) | |
:param day_offset: day offset from today (max 7) | |
:param market: market (en-US) | |
:param format: js or xml | |
""" | |
response = requests.get( | |
f"https://www.bing.com/HPImageArchive.aspx?&format={format}&n={count}&idx={day_offset}&mkt={market}", | |
) | |
return json.loads(response.text) | |
def fetch_wallpaper(day_offset = 0): | |
"""Fetch wallpaper from the web.""" | |
index = 0 | |
count = 1 | |
if day_offset > 14: | |
raise Exception("Day offset can only be 0-14") | |
if day_offset > 7: | |
count = 8 | |
day_offset -= 7 | |
index = -1 | |
info = fetch_wallpaper_info(count = count, day_offset = day_offset) | |
image_info = info['images'][index] | |
print(f"Bing Image of the day ({image_info['startdate']}): {image_info['copyright']}") | |
image = fetch_image(f"https://www.bing.com{image_info['url'].replace('1920x1080', 'UHD')}") | |
return image | |
def set_wallpaper_path(absolute_image_path): | |
"""Set wallpaper on Ubuntu""" | |
gsettings = subprocess.check_output(['which', 'gsettings']).decode("utf-8").strip() | |
if not gsettings: | |
raise Exception("gsettings not found") | |
for setting in ['picture-uri', 'picture-uri-dark']: | |
list = subprocess.check_output([gsettings, 'list-recursively', 'org.gnome.desktop.background']).decode("utf-8").strip() | |
if setting in list: | |
subprocess.call([gsettings, 'set', 'org.gnome.desktop.background', setting, f'file://{absolute_image_path}']) | |
def set_wallpaper_from_day(day_offset, force=False): | |
filename = f'{expanduser("~")}/Pictures/bingwallpaper.jpg' | |
if not force and is_file_modified_today_after_8am(filename): | |
print('Wallpaper already set today, skipping (use --force to set it anyway)') | |
return | |
image = fetch_wallpaper(day_offset) | |
save_image(image, filename) | |
set_wallpaper_path(filename) | |
def set_wallpaper_from_random_day(max=14, force=False): | |
random_day = random.randint(0, max) | |
print('Random day offset:', random_day) | |
set_wallpaper_from_day(random_day, force) | |
if __name__ == '__main__': | |
force = '--force' in sys.argv | |
if '--random' in sys.argv: | |
set_wallpaper_from_random_day(force=force) | |
else: | |
set_wallpaper_from_day(0, force=force) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment