-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
play_genre_artist.py
118 lines (105 loc) · 3.82 KB
/
play_genre_artist.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
import argparse
import configparser
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']
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--artist", help="artist search term")
parser.add_argument("-g", "--genre", help="genre search term")
parser.add_argument("-X", "--exartist", help="artist exclude search term")
parser.add_argument("-x", "--exgenre", help="genre exclude search term")
parser.add_argument("-z", "--zone", help="zone selection")
args = parser.parse_args()
if args.artist:
artistsearch = args.artist
else:
artistsearch = config['DEFAULT']['DefaultArtist']
if args.genre:
genresearch = args.genre
else:
genresearch = config['DEFAULT']['DefaultGenre']
if args.exartist:
exartistsearch = args.exartist
else:
exartistsearch = None
if args.exgenre:
exgenresearch = args.exgenre
else:
exgenresearch = None
if args.zone:
target_zone = args.zone
else:
target_zone = config['DEFAULT']['DefaultZone']
version = config['DEFAULT']['RoonCommandLineVersion']
release = config['DEFAULT']['RoonCommandLineRelease']
fullver = version + "-" + release
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"
roonapi = RoonApi(appinfo, token, server, port)
# save the token for next time
with open(tokenfile, "w") as f:
f.write(str(roonapi.token))
# get target zone output_id
outputs = roonapi.outputs
output_id = None
for (k, v) in outputs.items():
if target_zone in v["display_name"]:
output_id = k
if output_id is None:
err = "No zone found matching " + target_zone
sys.exit(err)
else:
# List matching genres
genres = roonapi.list_media(output_id, ["Genres", genresearch])
if genres:
artist = None
for genre in genres:
if exgenresearch is not None:
if exgenresearch in genre:
continue
# List matching artists
artists = roonapi.list_media(output_id,
["Genres", genre,
"Artists", artistsearch])
if exartistsearch is not None and artists:
artists = [chk for chk in artists if exartistsearch not in chk]
if artists:
artist = artists[0]
print("Playing artist name", artist, "in", genre, "genre")
roonapi.play_media(output_id,
["Genres", genre,
"Artists", artist], None, False)
if len(artists) > 1:
print("\nArtist names in", genre,
"genre matching", artistsearch, ":\n")
print(*artists, sep="\n")
print("\nTo play another artist in this genre by name either specify")
print("the full name or enough of a substring to provide a single match\n")
break
if artist is None:
print("No artists found matching", artistsearch)
else:
print("No genres found matching ", genresearch)