-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathltunez-client.py
210 lines (200 loc) · 6.49 KB
/
ltunez-client.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
import select
import socket
import curses
import re
import sys
import os
import plp
def connect(host,port):
""" Connect to server and return socket. """
sock = None
for res in socket.getaddrinfo(host,int(port), socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
except socket.error, msg:
sock = None
continue
try:
sock.connect(sa)
except socket.error, msg:
sock.close()
sock = None
continue
break
return sock
class Playlist:
""" Stores and manage playlist """
cursor = -1
START = 0
END = 19
def __init__(self,winH,winPL,winNPL):
self.winH = winH
self.winPL = winPL
self.playlist = dict()
self.winNPL = winNPL
self.vlc = 0
def parse(self,data):
""" Parse m3u playlist. """
index = 0
try:
lines = data.splitlines()
except:#be more explicit
print 'data.splitlines()'
if lines[0] == "#EXTM3U":
for line in lines[0:len(lines) - 1]:
if lines.index(line)%2 == 0:
continue
try:
lst = re.split('#EXTINF:|,|-',line,3)
except:#be more explicit
print 're.split()'
self.playlist[index] = [int(lst[1]),lst[2],lst[3],lines[(lines.index(line))+1]]
index += 1
self.draw()
def next(self):
""" Next song. """
if self.cursor < len(self.playlist) - 1:
self.cursor += 1
if self.cursor > self.END:
self.START += 1
self.END += 1
self.draw()
def previous(self):
""" Previous song """
if self.cursor > 0:
self.cursor -= 1
if self.cursor < self.START:
self.START -= 1
self.END -= 1
self.draw()
def play(self):
if self.vlc != 0:
os.system('kill -2 '+str(self.vlc))
pid = os.fork()
if pid == 0:
os.execlp("vlc","vlc","--quiet", self.playlist[self.cursor][3])
else:
self.vlc = pid
def draw(self):
self.winH.border()
self.winH.addstr(1, 35, "***** LTUNES CLIENT *****",curses.A_DIM)
self.winH.noutrefresh()
self.winPL.clear()
self.winPL.border()
self.winPL.addstr(1,5,"INDEX",curses.A_BOLD)
self.winPL.addstr(1,15,"ARTIST",curses.A_BOLD)
self.winPL.addstr(1,35,"SONG",curses.A_BOLD)
self.winPL.addstr(1,65,"LENGTH",curses.A_BOLD)
if len(self.playlist) == 0:
self.winPL.addstr(10,25,"Please wait,playlist is being downloaded.")
else:
for song in self.playlist:
if song >= self.START and song <= self.END:
if song == self.cursor:
EFFECT = curses.A_BOLD
else:
EFFECT = curses.A_DIM
self.winPL.addstr(song - self.START + 3, 5, str(song),EFFECT)
self.winPL.addstr(song - self.START + 3, 15, self.playlist[song][1],EFFECT)
self.winPL.addstr(song - self.START + 3, 35, self.playlist[song][2],EFFECT)
self.winPL.addstr(song - self.START + 3, 65, str(self.playlist[song][0])+' sec',EFFECT)
self.winPL.noutrefresh()
self.winNPL.clear()
self.winNPL.border()
self.winNPL.addstr(1,5,"NOW PLAYING:",curses.A_BOLD)
if self.cursor != -1:
self.winNPL.addstr(1,18,self.playlist[self.cursor][2])
self.winNPL.noutrefresh()
def main(host,port):
""" Handle all events. """
#Initialize curses,terminal graphics
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
#Header window
x_axis = 0
y_axis = 0
height = 3
width = 100
winH = curses.newwin(height, width, y_axis, x_axis)
#Playlist window
x_axis = 0
y_axis = 3
height = 25
width = 100
winPL = curses.newwin(height, width, y_axis, x_axis)
#Now playing window
x_axis = 0
y_axis = 28
height = 3
width = 100
winNPL = curses.newwin(height, width,y_axis, x_axis)
#Playlist object
playlist = Playlist(winH,winPL,winNPL)
playlist.draw
curses.doupdate()
#Possible inputs
inputs = []
inputs.append(sys.stdin)
#Send request to playlist server
sock = connect(host,port)
plpmessage = plp.PLPMessage()
if(sock != None):
req = plpmessage.createLTunezClientRequest()
sock.send(req)
inputs.append(sock)
quit = False;
data = ''
#Loop to select user inputs
while True:
try:
inputready,outputready,exceptready = select.select(inputs,[],[])
except:
inputready = []
for ins in inputready:
if(ins == sys.stdin):
ch = stdscr.getch()
if ch == 113 or ch == 81:
quit = True
elif ch == 112 or ch == 80:
playlist.play()
elif ch == curses.KEY_RIGHT:
playlist.next()
elif ch == curses.KEY_LEFT:
playlist.previous()
elif ch == 114 or ch == 82:
sock = connect(host,port)
if(sock != None):
req = plpmessage.createLTunezClientRequest()
sock.send(req)
inputs.append(sock)
else:
data = data + ins.recv(1024)
if re.search('\\r\\n\\r\\n',data):
plpmessage.parse(data)
if plpmessage.command == "Playlist OK":
playlist.parse(plpmessage.playlist)
else:
print "Request failed\r\n"
data = ''
curses.doupdate()
if quit:
break
if playlist.vlc != 0:
os.system('kill -2 '+str(playlist.vlc))
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
if __name__ == "__main__":
if(len(sys.argv) == 1):
print 'Ltunez server address & port missing'
sys.exit(0)
elif(len(sys.argv) != 2):
print 'Extra parameters'
sys.exit(0)
else:
inf = sys.argv[1].split(':')
main(inf[0],inf[1])