-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·135 lines (109 loc) · 4.86 KB
/
main.py
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
import os
import json
import time
import sys
data_path = 'data.json'
name = 'Volume Control'
standard_ttl = 2500
standard_ttl_s = standard_ttl / 1000
def get_timestanp():
return int(round(time.time(), 0))
def get_volume_master():
command = 'awk -F"[][]" \'/Left:/ { print $2 }\' <(amixer sget Master)'
return os.popen(f'{command}').read().strip()
def get_if_muted():
command = "amixer get Master | grep 'Left:' | awk -F'[][]' '{print $4}'"
returnstate = os.popen(command).read().strip()
if returnstate == "off":
return True
else:
return False
def data_exists(data_path):
if not os.path.exists(data_path):
data = {"last_called" : get_timestanp(), "last_message_id" : 0, "volume" : get_volume_master(), "muted" : get_if_muted()}
save_data(data)
def save_data(data):
with open('data.json', 'w') as file:
json.dump(data, file)
def load_data(data_path):
data_exists(data_path)
with open(data_path, 'r') as file:
return json.load(file)
def cli_command(command):
return os.popen(f'{command}').read().strip()
def notify(message="No message provided.", id=0, ttl=standard_ttl):
return_data = os.popen(f'notify-send -t {ttl} -r {id} -p "{name}" "{message}"').read().strip()
return return_data
def main(data_path=data_path, name=name, standard_ttl=standard_ttl, standard_ttl_s=standard_ttl_s):
args = sys.argv
data = load_data(data_path)
if len(args) == 1:
notify("Error: No arguments provided. Please provide an argument.")
return
# check if the argument is valid
if not (args[1] == "raise" or args[1] == "lower" or args[1] == "mute" or args[1] == "set"):
notify("Error: Invalid argument provided. Please provide a valid argument.")
return
# do if argument raise
if args[1] == "raise":
cli_command(f'amixer set Master {args[2]}+')
if get_timestanp() - data['last_called'] <= standard_ttl_s:
return_data = notify(f'Volume raised from {data["volume"]} to {get_volume_master()}; Muted: {data["muted"]}', data['last_message_id'])
else:
return_data = notify(f'Volume raised from {data["volume"]} to {get_volume_master()}; Muted: {data["muted"]}')
# update the data
data['volume'] = get_volume_master()
data['last_message_id'] = return_data.strip()
data['last_called'] = get_timestanp()
save_data(data)
# do if argument lower
elif args[1] == "lower":
cli_command(f'amixer set Master {args[2]}-')
if get_timestanp() - data['last_called'] <= standard_ttl_s:
return_data = notify(f'Volume lowered from {data["volume"]} to {get_volume_master()}; Muted: {data["muted"]}', data['last_message_id'])
else:
return_data = notify(f'Volume lowered from {data["volume"]} to {get_volume_master()}; Muted: {data["muted"]}')
# update the data
data['volume'] = get_volume_master()
data['last_message_id'] = return_data.strip()
data['last_called'] = get_timestanp()
save_data(data)
elif args[1] == "set":
cli_command(f'amixer set Master {args[2]}')
if get_timestanp() - data['last_called'] <= standard_ttl_s:
return_data = notify(f'Volume set to {get_volume_master()}; Muted: {data["muted"]}', data['last_message_id'])
else:
return_data = notify(f'Volume set to {get_volume_master()}; Muted: {data["muted"]}')
# update the data
data['volume'] = get_volume_master()
data['last_message_id'] = return_data.strip()
data['last_called'] = get_timestanp()
save_data(data)
# do if argument mute
elif args[1] == "mute":
if data['muted']:
cli_command('amixer set Master unmute')
data['muted'] = False
if (get_timestanp() - data['last_called'] <= standard_ttl_s):
return_data = notify(f'Volume unmuted; Muted {data["muted"]}', data['last_message_id'])
else:
return_data = notify(f'Volume unmuted; Muted {data["muted"]}')
# update the data
data['last_message_id'] = return_data.strip()
data['last_called'] = get_timestanp()
save_data(data)
elif not data['muted']:
cli_command('amixer set Master mute')
data['muted'] = True
if (get_timestanp() - data['last_called'] <= standard_ttl_s):
return_data = notify(f'Volume muted; Muted {data["muted"]}', data['last_message_id'])
else:
return_data = notify(f'Volume muted; Muted {data["muted"]}')
# update the data
data['last_message_id'] = return_data.strip()
data['last_called'] = get_timestanp()
save_data(data)
else:
notify("Error: Something went wrong.")
if __name__ == "__main__":
main()