-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
now_playing.py
163 lines (148 loc) · 5.47 KB
/
now_playing.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import argparse
import configparser
import json
from os import path
import signal
import sys
from roonapi import RoonApi
def signal_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
config = configparser.ConfigParser()
config.read("/usr/local/Roon/etc/roon_api.ini")
# Set to IP address of your Roon Core
server = config["DEFAULT"]["RoonCoreIP"]
# Set to Port of your Roon Core
port = config["DEFAULT"]["RoonCorePort"]
# Name of the file that holds a Roon API token
tokenfile = config["DEFAULT"]["TokenFileName"]
version = config["DEFAULT"]["RoonCommandLineVersion"]
release = config["DEFAULT"]["RoonCommandLineRelease"]
fullver = version + "-" + release
parser = argparse.ArgumentParser()
parser.add_argument("-z", "--zone", help="zone selection")
parser.add_argument(
"-P",
"--paused",
default=False,
action="store_true",
help="display only zones in paused state",
)
parser.add_argument(
"-p",
"--playing",
default=False,
action="store_true",
help="display only zones in playing state",
)
parser.add_argument(
"-a",
"--all",
default=False,
action="store_true",
help="display all zones regardless of state",
)
args = parser.parse_args()
if args.zone:
target_zone = args.zone
if target_zone == "__all__":
target_zone = None
else:
target_zone = None
if args.paused:
filterpaused = 1
else:
filterpaused = None
if args.playing:
filterplaying = 1
else:
filterplaying = None
if args.all:
filterplaying = None
filterpaused = None
appinfo = {
"extension_id": "roon_command_line",
"display_name": "Python library for Roon",
"display_version": fullver,
"publisher": "RoonCommandLine",
"email": "roon@ronrecord.com",
"website": "https://github.com/doctorfree/RoonCommandLine",
}
# Can be None if you don't yet have a token
if path.exists(tokenfile):
token = open(tokenfile).read()
else:
token = "None"
with RoonApi(appinfo, token, server, port) as roonapi:
# save the token for next time
with open(tokenfile, "w") as f:
f.write(str(roonapi.token))
outputs = roonapi.outputs
output_id = None
if target_zone is None:
for zone in roonapi.zones.values():
state = "Unknown"
if zone["state"] is not None:
state = zone["state"]
if filterplaying is not None and state != "playing":
continue
else:
if filterpaused is not None and state != "paused":
continue
else:
if "display_name" in zone:
zone_name = zone["display_name"]
else:
zone_name = "Unknown"
if "now_playing" in zone and zone["now_playing"] is not None:
print("\nNow playing in zone: %s" % zone_name)
track = json.dumps(
zone["now_playing"]["three_line"]["line1"],
ensure_ascii=False,
).encode("utf8")
print("\tTrack:\t %s" % track.decode())
artist = json.dumps(
zone["now_playing"]["three_line"]["line2"],
ensure_ascii=False,
).encode("utf8")
print("\tArtist:\t %s" % artist.decode())
album = json.dumps(
zone["now_playing"]["three_line"]["line3"],
ensure_ascii=False,
).encode("utf8")
print("\tAlbum:\t %s" % album.decode())
print("\tState:\t %s" % state)
else:
for k, v in outputs.items():
if target_zone in v["display_name"]:
state = "Unknown"
zone_name = v["display_name"]
output_id = k
zone = roonapi.zone_by_output_id(output_id)
if zone is not None:
if zone["state"] is not None:
state = zone["state"]
if filterplaying is not None and state != "playing":
continue
else:
if filterpaused is not None and state != "paused":
continue
else:
if zone["now_playing"] is not None:
print("\nNow playing in zone: %s" % zone_name)
track = json.dumps(
zone["now_playing"]["three_line"]["line1"],
ensure_ascii=False,
).encode("utf8")
print("\tTrack:\t %s" % track.decode())
artist = json.dumps(
zone["now_playing"]["three_line"]["line2"],
ensure_ascii=False,
).encode("utf8")
print("\tArtist:\t %s" % artist.decode())
album = json.dumps(
zone["now_playing"]["three_line"]["line3"],
ensure_ascii=False,
).encode("utf8")
print("\tAlbum:\t %s" % album.decode())
print("\tState:\t %s" % state)