forked from JoChaw/SpotipyTUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
195 lines (144 loc) · 6.61 KB
/
command.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
from curses import textpad
import curses
import subprocess
import requester
class CommandHandler(object):
def __init__(self, stdscreen):
track_list_length = 120
track_list_height = 38
search_buffer_length = 100
search_buffer_height = 1
self.country_id = None
self.stdscreen = stdscreen
self.track_list = None
self.track_start = 2
self.curr_position = self.track_start
self.track_window = stdscreen.subwin(track_list_height, track_list_length, 0, 0)
self.search_window = stdscreen.subwin(search_buffer_height, search_buffer_length, self.track_window.getmaxyx()[0]+1, 8)
self.input_buffer = textpad.Textbox(self.search_window)
self.input_buffer.stripspaces = 1
self.input_prompt = stdscreen.subwin(1, 10, self.track_window.getmaxyx()[0]+1, 1)
self.now_playing_window = stdscreen.subwin(1, 120, stdscreen.getmaxyx()[0] - 1, 0)
def setTrackList(self, track_list):
self.track_list = track_list
def setCurrPosition(self, curr_position):
self.curr_position = curr_position
def moveUp(self):
if self.track_list != None and self.curr_position > self.track_start:
self.curr_position -= 1
self.drawTrackList()
def moveDown(self):
if self.track_list != None and self.curr_position < (len(self.track_list) + self.track_start - 1):
self.curr_position += 1
self.drawTrackList()
def nextSong(self):
self.moveDown()
self.currentSong()
def prevSong(self):
self.moveUp()
self.currentSong()
def playAtIndex(self):
curses.curs_set(2)
self.input_prompt.addstr(0, 0, "Index#:")
self.input_prompt.refresh()
self.search_window.clear()
desired_index = self.input_buffer.edit()
self.input_prompt.clear()
self.input_prompt.refresh()
self.search_window.clear()
self.search_window.refresh()
try:
desired_index = int(desired_index)
screen_index = desired_index + self.track_start - 1
if self.track_list != None and screen_index <= (len(self.track_list) + self.track_start - 1) and screen_index >= self.track_start:
self.curr_position = screen_index
self.currentSong()
self.drawTrackList()
except ValueError:
#TODO Error Message for invalid index
pass
curses.curs_set(0)
def currentSong(self):
if self.track_list != None:
self.playSong(self.track_list[self.curr_position - self.track_start])
def togglePlayPause(self):
apple_script_call = ['osascript', '-e', 'tell application "Spotify" to playpause']
subprocess.call(apple_script_call)
def playSong(self, track):
track_spotify_uri = track[4]
apple_script_call = ['osascript', '-e', 'tell application "Spotify" to play track "{0}"'.format(track_spotify_uri)]
subprocess.call(apple_script_call)
self.updateNowPlaying(track)
def updateNowPlaying(self, track):
now_playing = ">>> Now Playing: {0} --- {1} <<<".format(track[1][:50], track[2][:40])
self.now_playing_window.clear()
self.now_playing_window.addstr(0, 0, now_playing)
self.now_playing_window.refresh()
def showClient(self):
get_client_command = 'tell application "Spotify" \n activate \n end tell'
apple_script_call = ['osascript', '-e', get_client_command]
subprocess.call(apple_script_call)
def searchContent(self):
curses.curs_set(2)
self.input_prompt.addstr(0, 0, "Search:")
self.input_prompt.refresh()
self.search_window.clear()
user_search = self.input_buffer.edit()
self.input_prompt.clear()
self.input_prompt.refresh()
self.search_window.clear()
self.search_window.refresh()
if len(user_search) > 0:
self.track_list = requester.execute_search(user_search)
self.curr_position = self.track_start
self.drawTrackList()
curses.curs_set(0)
def getArtistTop(self):
if self.track_list != None:
while self.country_id == None: #TODO: Ensure Valid Country Country Code
curses.curs_set(2)
self.input_prompt.addstr(0, 0, "Country:")
self.input_prompt.refresh()
self.search_window.clear()
self.country_id = self.input_buffer.edit().split()[0]
self.input_prompt.clear() #TODO: Bundle all these clear and refreshes into methods
self.input_prompt.refresh()
self.search_window.clear()
self.search_window.refresh()
curses.curs_set(0)
track = self.track_list[self.curr_position - self.track_start]
artist_name = track[2]
artist_id = track[7]
artist_uri = track[6]
self.track_list = requester.get_artist_top(artist_name, artist_id, artist_uri, self.country_id)
self.curr_position = self.track_start
self.drawTrackList()
def getAlbumTracks(self):
if self.track_list != None:
track = self.track_list[self.curr_position - self.track_start]
album_name = track[3]
album_id = track[8]
album_uri = track[5]
self.track_list = requester.get_album_tracks(album_name, album_id, album_uri)
self.curr_position = self.track_start
self.drawTrackList()
def drawTrackList(self):
self.track_window.clear()
result_line = '{0:<2} | {1:<40} | {2:<25} | {3:<40}'
result_header = result_line.format('#', 'Song Name', 'Artist', 'Album')
separator_bar = '=' * (self.track_window.getmaxyx()[1] - 5)
self.track_window.addstr(0, 0, result_header)
self.track_window.addstr(1, 0, separator_bar)
for song_index, track in enumerate(self.track_list, start=1):
if (self.curr_position - self.track_start) == track[0]:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
song_index = str(song_index)
if len(song_index) == 1:
song_index = '0' + song_index
track_string = result_line.format(song_index, track[1][:40], track[2][:25], track[3][:40])
self.track_window.addstr(track[0] + self.track_start, 0, track_string, mode)
bottom_bar_position = self.track_start + len(self.track_list)
self.track_window.addstr(bottom_bar_position, 0, separator_bar)
self.track_window.refresh()