-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotofocli.py
249 lines (211 loc) · 7.01 KB
/
spotofocli.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from __future__ import print_function
import django
django.setup()
import os
import json
import click
import spotofo
def print_trackinfo(ti, nl=True):
print(ti.username, end=' ')
if ti.active_device and ti.track:
print('||', ti.track, '||', end=' ')
print(ti.artist, '||', end=' ')
print(ti.album, ti.progress, end=' ')
if nl:
print()
@click.group()
@click.option('-c', 'cfn', help='Config file', default=os.path.expanduser('~/.spotofo.conf'))
@click.pass_context
def cli(ctx, cfn):
pass
@cli.command()
@click.pass_context
def currently_playing(ctx):
"""Currently playing tracks for all users"""
users = spotofo.get_users()
for ti in spotofo.get_currently_playing_trackinfo(users):
print_trackinfo(ti)
@cli.command()
@click.pass_context
@click.option('--topic', help='Topic to subscribe', default=None)
def update_playlist(ctx, topic):
"""Add currently playing tracks to the playlist
Only add tracks which are not yet in the playlist.
Only add tracks playing in authorized devices.
Send trackinfo to analytics tracking.
"""
active_tracks = []
users = spotofo.get_users()
for ti in spotofo.get_currently_playing_trackinfo(users):
print_trackinfo(ti, nl=False)
if ti.is_playing and ti.active_device:
spotofo.analyze_play(ti)
active_tracks.append(ti)
print(True, end=' ')
print()
active_track_uris = map(lambda x: x.uri, active_tracks)
to_be_added_track_uris = spotofo.deduplicate_tracks(active_track_uris)
spotofo.add_tracks_to_playlist(to_be_added_track_uris)
mqtt_data = [x for x in active_tracks if x.uri in to_be_added_track_uris]
if len(mqtt_data):
spotofo.mqtt_single(json.dumps(mqtt_data), topic)
@cli.command()
@click.pass_context
@click.argument('track')
def add_track(ctx, track):
"""Add track to playlist"""
to_be_added_tracks = spotofo.deduplicate_tracks([track])
spotofo.add_tracks_to_playlist(to_be_added_tracks)
@cli.command()
@click.pass_context
@click.argument('host')
@click.option('--port', default=1883)
@click.option('--topic', default='spotofo')
@click.option('--username', default=None)
@click.option('--password', default=None)
def mqtt_config(ctx, host, port, topic, username, password):
"""Set MQTT connection parameters"""
spotofo.set_mqtt_config(host, port, topic, username, password)
print(repr(spotofo.get_mqtt_config()))
@cli.command()
@click.pass_context
@click.argument('msg')
@click.option('--topic', default=None)
def mqtt_send(ctx, msg, topic):
"""Send message to MQTT topic"""
try:
spotofo.mqtt_single(msg, topic)
except ValueError as e:
print(e)
return
@cli.command()
@click.pass_context
@click.option('--host', default=None)
@click.option('--port', default=None)
@click.option('--topic', help='Topic to subscribe', default=None)
@click.option('--username', default=None)
@click.option('--password', default=None)
def mqtt_client(ctx, host, port, topic, username, password):
"""Monitor MQTT topic for messages"""
try:
import paho.mqtt.client as mqtt
except ImportError:
print('Install paho-mqtt')
return
topic_arg, conf = spotofo.get_mqtt_config()
topic_arg = topic or topic_arg
try:
username = username or conf['auth']['username']
password = password or conf['auth']['password']
except TypeError:
username = None
password = None
host = host or conf['hostname']
port = port or conf['port']
def on_connect(client, userdata, flags, rc):
if rc == 0:
client.subscribe(topic_arg)
print('Subscribed:', repr(topic_arg))
else:
print(mqtt.error_string(rc))
def on_message(client, userdata, msg):
print(repr(msg.topic), repr(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
if username and password:
client.username_pw_set(username, password)
try:
client.connect(host, int(port), 60)
except ValueError as e:
print(e)
return
client.loop_forever()
@cli.command()
@click.argument('username')
@click.pass_context
def devices(ctx, username):
"""List available devices for user"""
devices = spotofo.get_user_devices(username)
for device in devices:
print(device['type'], repr(device['name']), 'ID:', device['id'])
@cli.command()
@click.pass_context
def authorized(ctx):
"""List authorized users and devices"""
print('Authorized to query data from user / device:')
for username in spotofo.get_users():
for device in spotofo.get_devices(username):
print(username, '/', device)
@cli.command()
@click.pass_context
def config(ctx):
"""Show config"""
print('Nothing to show')
@cli.command()
@click.argument('username')
@click.pass_context
def authorize_user(ctx, username):
"""Add new user"""
state, data = _oauth_authorize(username, scope=spotofo.DEFAULT_SCOPE)
if state == 'token' and data:
spotofo.save_token_info(username, data)
print('Authorized to query data from user', username)
else:
print("Can't get token for", username)
@cli.command()
@click.argument('username')
@click.pass_context
def authorize_device(ctx, username):
"""Add new device"""
devices = spotofo.get_user_devices(username)
device_ids = []
if devices:
for device in devices:
print(device['type'], repr(device['name']), 'ID:', device['id'])
device_ids.append(device['id'])
device_id = input('Enter the device ID you want to authorize: ')
for device in devices:
if device_id == device['id']:
spotofo.add_user_device(username, device)
print('Authorized to query data from user', username, 'device', device['id'])
return
print("Can't authorize device", device)
@cli.command()
@click.argument('target')
@click.pass_context
def authorize_playlist(ctx, target):
"""Change target playlist"""
username, playlist = spotofo.split_playlist(target)
if not username:
print('Unable to handle playlist')
return
scope = 'playlist-modify-private'
state, data = _oauth_authorize(username, scope=scope)
if state == 'token' and data:
spotofo.save_token_info(username, data)
spotofo.save_playlist(username, playlist)
print('Authorized to change playlist', target)
else:
print("Can't get token for", username)
def _update_shared_playlist():
all_tracks = []
active_tracks = []
users = spotofo.get_users()
for ti in spotofo.get_currently_playing_trackinfo(users):
all_tracks.append(ti)
if ti.is_playing and spotofo.is_authorized_device(ti.username, ti.device):
spotofo.analyze_play(ti)
active_tracks.append(ti)
active_track_uris = map(lambda x: x.uri, active_tracks)
to_be_added_tracks = spotofo.deduplicate_tracks(active_track_uris)
spotofo.add_tracks_to_playlist(to_be_added_tracks)
return (all_tracks, to_be_added_tracks)
def _oauth_authorize(username, scope=None, response=None):
state, data = spotofo.authorize_with_scope(username, scope=scope)
print('Please navigate here: %s' % data)
response = input('Enter the URL you were redirected to: ')
state, data = spotofo.authorize_with_scope(username, scope=scope, response=response)
return state, data
if __name__ == '__main__':
cli()