From d9f12831186077736fcd9c163a4b31a1fa28690e Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 30 Oct 2023 09:40:24 +0100 Subject: [sway.wallpaper] move brightness check behind flag use sunwait and check for available commands before running --- .config/sway/bin/wallpaper.py | 85 +++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/.config/sway/bin/wallpaper.py b/.config/sway/bin/wallpaper.py index 906b9fe..8838053 100755 --- a/.config/sway/bin/wallpaper.py +++ b/.config/sway/bin/wallpaper.py @@ -3,7 +3,7 @@ import sys, os import argparse, json -import random, re, subprocess, time +import random, re, subprocess, shutil, time import datetime @@ -32,11 +32,30 @@ parser.add_argument( default=False ) +parser.add_argument( + '--consider_daylight', + default=False +) + +parser.add_argument( + '--brightness_threshold', + default=0.35 +) + args = parser.parse_args() config = json.loads(args.config) +if shutil.which('swaybg') is None: + sys.exit('swaybg not found') + +if args.consider_daylight and shutil.which('sunwait') is None: + sys.exit('sunwait not found') +if args.consider_daylight and shutil.which('identify') is None: + sys.exit('identify not found') + + """ " Program """ @@ -48,6 +67,14 @@ if args.unique: config[weight]['id'] = hash(json.dumps(config_item, sort_keys=True)) already_selected_file_paths[config_item['id']] = [] +# set variables for --consider_daylight flag +location = {} +if args.consider_daylight: + location = { + 'latitude': float(subprocess.run(['pass', 'latitude'], capture_output=True, text=True).stdout), + 'longitude': float(subprocess.run(['pass', 'longitude'], capture_output=True, text=True).stdout), + } + # run the loop running_bg_process = None while True: @@ -94,33 +121,35 @@ while True: found_file_paths = filtered_file_paths # select file - selected_file_path = '' - tries = 0 - while True: - selected_file_path = random.choice(found_file_paths) - - now_hour = datetime.datetime.now().hour - if now_hour > 6 and now_hour < 17: # daylight - break - - identify_process = subprocess.run(['identify', '-format', '%[fx:mean]', selected_file_path], capture_output=True, text=True) - brightness = float(identify_process.stdout) - if brightness < 0.35: # non-bright image - break - else: - # remove identified bright image from possible selections - found_file_paths.remove(selected_file_path) - - if args.unique: - already_selected_file_paths[selected_config['id']].append(selected_file_path) - - # can't find suitable image - tries += 1 - if tries >= len(found_file_paths) - 1: - if args.unique: - already_selected_file_paths[selected_config['id']] = [] - - break + selected_file_path = random.choice(found_file_paths) + + if args.consider_daylight: + daytime_process = subprocess.run(['sunwait', 'poll', '{0}N'.format(location['latitude']), '{0}E'.format(location['longitude'])], capture_output=True, text=True) + daytime = daytime_process.stdout.strip() + + if daytime == 'NIGHT': + tries = 0 + while True: + selected_file_path = random.choice(found_file_paths) + + identify_process = subprocess.run(['identify', '-format', '%[fx:mean]', selected_file_path], capture_output=True, text=True) + brightness = float(identify_process.stdout) + if brightness < float(args.brightness_threshold): # non-bright image + break + else: + # remove identified bright image from possible selections + found_file_paths.remove(selected_file_path) + + if args.unique: + already_selected_file_paths[selected_config['id']].append(selected_file_path) + + # can't find suitable image + tries += 1 + if tries >= len(found_file_paths) - 1: + if args.unique: + already_selected_file_paths[selected_config['id']] = [] + + break if args.unique: already_selected_file_paths[selected_config['id']].append(selected_file_path) -- cgit v1.2.3