-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.py
More file actions
103 lines (83 loc) · 3.52 KB
/
Copy pathvolume.py
File metadata and controls
103 lines (83 loc) · 3.52 KB
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
#!/usr/bin/env python3
"""
Adjust MPD volume using python-mpd library.
This script allows you to adjust the volume of the Music Player Daemon (MPD) using the settings provided
in the 'mpd-extended.cfg' configuration file. If the configuration file or its settings are not found,
the script falls back to default values.
Usage:
volume.py [direction] [amount]
Arguments:
direction Direction to adjust the volume (up or down)
amount Amount by which to adjust volume
Examples:
volume.py up 5 # Increase volume by 5 units
volume.py down 10 # Decrease volume by 10 units
Note:
If the 'toggleMaxVolume' setting is enabled in the configuration file, the script ensures that the
volume does not exceed the 'maxVolume' setting when increasing the volume. Otherwise, it respects
the provided volume increase value.
Dependencies:
- python-mpd library (https://python-mpd.readthedocs.io/en/latest/)
"""
import os
import sys
import argparse
from mpd import MPDClient
import configparser
def read_config():
"""
Function to read MPD configuration from mpd-extended.cfg file.
Returns:
- Dictionary containing MPD configuration.
"""
config = configparser.ConfigParser()
mpd_extended_cfg_path = os.path.expanduser("~/.config/mpd/mpd-extended.cfg")
if not os.path.isfile(mpd_extended_cfg_path):
print(f"Error: MPD extended configuration file (mpd-extended.cfg) not found at {mpd_extended_cfg_path}")
sys.exit(1)
config.read(mpd_extended_cfg_path)
mpd_config = {
'volUp': int(config['MPD-SCRIPTS'].get('volUp', 5)),
'volDown': int(config['MPD-SCRIPTS'].get('volDown', 5)),
'toggleMaxVolume': config['MPD-SCRIPTS'].getboolean('toggleMaxVolume', fallback=False),
'maxVolume': int(config['MPD-SCRIPTS'].get('maxVolume', 80)),
'host': config['MPD'].get('HOST', 'localhost'),
'port': int(config['MPD'].get('PORT', '6600')),
'password': config['MPD'].get('PASSWORD', '')
}
return mpd_config
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Adjust MPD volume.')
parser.add_argument('direction', nargs='?', choices=['up', 'down'], help='Direction to adjust volume (up or down)')
parser.add_argument('amount', nargs='?', type=int, default=5, help='Amount by which to adjust volume')
args = parser.parse_args()
# Read MPD configuration from mpd-extended.cfg
mpd_config = read_config()
host = mpd_config['host']
port = mpd_config['port']
password = mpd_config['password']
# Connect to MPD server
client = MPDClient()
client.connect(host, port)
# Authenticate if password is provided
if password:
client.password(password)
# Get current volume
current_volume = client.status().get('volume', 'Unknown')
# If no arguments provided, show usage and current volume
if not args.direction:
print(f"usage: {sys.argv[0]} [-h] {{up,down}} [amount]\nCurrent volume: {current_volume}")
sys.exit(0)
# Adjust volume based on command-line argument
if args.direction == 'up':
client.volume(f'+{args.amount}') # Increase volume by specified amount
print(f"Volume increased by {args.amount} units.")
elif args.direction == 'down':
client.volume(f'-{args.amount}') # Decrease volume by specified amount
print(f"Volume decreased by {args.amount} units.")
# Disconnect from MPD server
client.close()
client.disconnect()
if __name__ == "__main__":
main()