-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·244 lines (195 loc) · 5.96 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
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
#!/bin/env python
#logging.basicConfig(level=logging.DEBUG)
#what is this?
#from __future__ import unicode_literals
import logging
import threading
import cmd
import random
from collections import deque
import spotify
print("Spotify_terminal")
class Commander(cmd.Cmd):
# what is this
doc_header = 'Commands'
prompt = 'Command> '
logger = logging.getLogger('shell.commander')
container_root = 0
play_queue = deque ([])
randommode = 0
# Do some documentation on callbacks
def __init__(self):
cmd.Cmd.__init__(self)
# so whe can manage the state for these callbacks
self.logged_in = threading.Event()
self.logged_out = threading.Event()
self.end_of_track = threading.Event()
# set logged_out and end_of_track to true
self.logged_out.set()
self.end_of_track.set()
# create the spotify session
self.session = spotify.Session()
# look for event changes
self.session.on(
spotify.SessionEvent.CONNECTION_STATE_UPDATED,
self.on_connection_state_changed)
self.session.on(
spotify.SessionEvent.END_OF_TRACK, self.on_end_of_track)
# initialize the alsa audio
try:
self.audio_driver = spotify.AlsaSink(self.session)
except ImportError:
self.logger.warning(
'No audio sink found; audio playback unavailable.')
# start the event loop
self.event_loop = spotify.EventLoop(self.session)
self.event_loop.start()
# called when connection state changed,
# e.g. the user login got a successful callback
def on_connection_state_changed(self, session):
if session.connection.state is spotify.ConnectionState.LOGGED_IN:
# set the user login states
self.logged_in.set()
self.logged_out.clear()
# once logged in, load the users playlistcontainer
self.load_root_container()
elif session.connection.state is spotify.ConnectionState.LOGGED_OUT:
# log out properly when logout callback returns
self.logged_in.clear()
self.logged_out.set()
# When the the end_of_track callback returns
# try to continue the play_queue
def on_end_of_track(self, session):
self.logger.info("End of track")
self.session.player.play(False)
self.end_of_track.set()
self.go_next()
# document this
def precmd(self, line):
if line:
self.logger.debug('New command: %s', line)
return line
# document this
def emptyline(self):
pass
# cmd looks for methods within Commander starting with
# do_X as runnable commands.
def do_info(self, line):
"Show normal logging output"
print('Logging at INFO level')
print(self.container_root)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def do_login(self, line):
"login <username> <password>"
username, password = line.split(' ', 1)
self.session.login(username, password, remember_me=True)
self.logged_in.wait()
def do_exit(self, line):
"Exit"
if self.logged_in.is_set():
print('Logging out...')
self.session.logout()
self.logged_out.wait()
self.event_loop.stop()
print('')
return True
def do_playlists(self, line):
"print playlsits"
print("Playlists")
self.container_root = self.session.playlist_container
self.container_root.load()
i = 0
for playlist in self.container_root:
if( type(playlist) == spotify.playlist.Playlist):
a = i, playlist.name.encode('utf-8')
print(a)
i += 1
def do_list(self, line):
"Print contents of a playlist"
playlistnumber = line.split(' ', 0)
playlistnumber = int(playlistnumber[0])
playlist = self.container_root[playlistnumber]
playlist.load()
for track in playlist.tracks:
print( track.artists[0].name.encode('utf-8') , "-",
track.name.encode('utf-8'))
def do_playp(self, line):
"Play selected playlist in background"
self.current_playlist_counter = 0;
playlistnumber = line.split(' ', 0)
playlistnumber = int(playlistnumber[0])
playlist = self.container_root[playlistnumber]
playlist.load()
current_playlist = []
current_playlist.extend(playlist.tracks)
if self.randommode: random.shuffle(current_playlist)
self.play_queue.extend(current_playlist)
self.play(self.play_queue.popleft())
def do_n(self, line):
"Go to next song in current_playlist"
self.go_next()
def do_search(self, line):
"Search for a song"
def do_ls(self, line):
"alias for playlists"
self.do_playlists(line)
def do_clear(self, line):
self.play_queue.clear()
def do_pause(self, line):
self.session.player.play(False)
def do_resume(self, line):
self.session.player.play()
def do_random(self, line):
if(self.randommode): self.randommode = 0
else: self.randommode = 1
print("Random", self.randommode)
def do_queue(self, line):
"List current play queue"
for track in self.play_queue:
print(track.name.encode('utf-8'))
def do_search(self, query):
"search <query>"
if query is None: return
try:
result = self.session.search(query)
result.load()
except spotify.Error as e:
self.logger.warning(e)
return
print("\n")
print("%d tracks, %d albums, %d artists, and %d playlists found." %
(result.track_total, result.album_total,
result.artist_total, result.playlist_total))
i = 1
for track in result.tracks:
print(i, track.artists[0].name.encode('utf-8'), "-", track.name.encode('utf-8'))
i+=1
n = input("Select song to add to queue (0 = none)")
n = int(n) - 1
if(n == -1 ): return
if(n > 20 ): return
self.play_queue.appendleft(result.tracks[n])
print(self.end_of_track)
if(self.end_of_track.is_set()):
self.play(self.play_queue.pop())
print("\n")
def play(self, track):
track.load()
self.session.player.load(track)
self.session.player.play()
self.end_of_track.clear()
print("Playing: ", track.artists[0].name.encode('utf-8') ,
"-", track.name.encode('utf-8'))
def go_next(self):
if(len(self.play_queue) > 0):
self.play(self.play_queue.popleft())
else:
print("Play queue empty")
def load_root_container(self):
self.container_root = self.session.playlist_container
self.container_root.load()
# do the cmd loop
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
Commander().cmdloop()