Created
March 2, 2022 11:22
-
-
Save dakyskye/512a9c24366a2b034202948f800c5647 to your computer and use it in GitHub Desktop.
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
import ctypes | |
import pathlib | |
from glob import glob | |
import random | |
import sys | |
from time import sleep | |
last_wallpaper = '' | |
def set_random_wallpaper(): | |
wallpapers_dir = f"{pathlib.Path(__file__).parent.absolute()}\wallpapers" | |
wallpapers_list = glob(f"{wallpapers_dir}\*.png") + glob(f"{wallpapers_dir}\*.jpg") | |
global last_wallpaper | |
random_wallpaper = random.choice(wallpapers_list) | |
if len(wallpapers_list) > 1: | |
while random_wallpaper == last_wallpaper: | |
random_wallpaper = random.choice(wallpapers_list) | |
last_wallpaper = random_wallpaper | |
ctypes.windll.user32.SystemParametersInfoW(20, 0, random_wallpaper, 0) | |
def get_schedule(): | |
if len(sys.argv) == 1: | |
return None | |
arg = sys.argv[1] | |
multiplier = 1 | |
if arg.endswith(('m', 'h', 's')): | |
match arg[-1]: | |
case 'm': | |
multiplier = 60 | |
case 'h': | |
multiplier = 60 * 60 | |
arg = arg[:-1] | |
try: | |
arg = int(arg) | |
except ValueError: | |
raise Exception("please input a valid integer with [m/h/s] postfix") | |
return arg * multiplier | |
def main(): | |
try: | |
schedule = get_schedule() | |
except Exception as e: | |
print(e) | |
return | |
if schedule is None: | |
set_random_wallpaper() | |
return | |
try: | |
while True: | |
set_random_wallpaper() | |
sleep(schedule) | |
except KeyboardInterrupt: | |
return | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment