1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#!/usr/bin/env python
import sys, os
import argparse, json
import random, re, subprocess, time
import datetime
"""
" CLI
"""
parser = argparse.ArgumentParser(
prog='wallpaper',
description='Cycle through wallpapers'
)
parser.add_argument(
'--config',
required=True,
help='{"<weight>": { "find": "<path>", "exclude": "<regex>" }, "<weight>":...}'
)
parser.add_argument(
'--interval',
default=600
)
parser.add_argument(
'--unique',
default=False
)
args = parser.parse_args()
config = json.loads(args.config)
"""
" Program
"""
# set variables for --unique flag
already_selected_file_paths = {}
if args.unique:
for weight, config_item in config.items():
config[weight]['id'] = hash(json.dumps(config_item, sort_keys=True))
already_selected_file_paths[config_item['id']] = []
# run the loop
running_bg_process = None
while True:
probability_number = random.randrange(start=1, stop=100, step=1)
# select config to use based on args.config
# see https://stackoverflow.com/questions/16489449/select-element-from-array-with-probability-proportional-to-its-value/16490300#16490300
selected_config = {}
accumulative = 0
for weight, config_item in config.items():
lower_bound = accumulative
accumulative += int(weight)
upper_bound = accumulative
if lower_bound <= probability_number and probability_number <= upper_bound:
selected_config = config_item
break
# build list of file paths to choose from
found_file_paths = []
for dirpath, dirnames, filenames in os.walk(os.path.expandvars(selected_config['find'])):
for filename in filenames:
found_file_paths.append(f'{dirpath}/{filename}')
if 'exclude' in selected_config.keys():
found_file_paths = list(filter(
lambda file_path: (not re.match(os.path.expandvars(selected_config['exclude']), file_path)),
found_file_paths
))
if args.unique:
# filter file paths from already selected paths
filtered_file_paths = [
item
for item in found_file_paths
if item not in already_selected_file_paths[selected_config['id']]
]
# if we cycled through all of them, repeat
if not filtered_file_paths:
filtered_file_paths = found_file_paths
already_selected_file_paths[selected_config['id']] = []
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
if args.unique:
already_selected_file_paths[selected_config['id']].append(selected_file_path)
# run swaybg
swaybg_cmd = ['swaybg', '-i', selected_file_path, '-m', 'fill']
replacing_process = subprocess.Popen(swaybg_cmd)
# wait until new instance of swaybg has booted up properly
time.sleep(5)
# kill the previous process
# the new swaybg process takes its place automatically
if running_bg_process:
running_bg_process.terminate()
# set running process for next iteration
running_bg_process = replacing_process
# wait $INTERVAL seconds
time.sleep(int(args.interval))
|