Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·149 lines (126 loc) · 4.51 KB
/
Copy pathmain.py
File metadata and controls
executable file
·149 lines (126 loc) · 4.51 KB
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
#!/usr/bin/env python
import cmd
import getpass
import gsclient
import readline
import subprocess
import sys
import urllib
version = 0.0
def tr(s, l):
if len(s) < l:
return s
return s[0:l-3] + '...'
class MainCmd(cmd.Cmd):
intro = 'GS Command Line %s, type "help" or "?" for help' % version
prompt = 'gscmd> '
def __init__(self):
cmd.Cmd.__init__(self)
self._client = gsclient.ClientWrapper()
self._more = None
self._results = None
self._results_idx = None
self._select = None
def do_EOF(self, rest):
print
sys.exit(0)
def _select_album(self, album):
print "I don't know what to do with albums."
def _show_albums(self, albums):
i = self._results_idx + 1
artist_max = max([len(a.artist.name) for a in albums])
format = " [%%3d] %%%ds - %%s" % artist_max
for a in albums:
print format % (i, a.artist.name, a.title)
i += 1
def do_album(self, rest):
"""Search for albums with the given title."""
self._more = self._show_albums
self._results = self._client.search_album(rest)
self._results_idx = 0
self._select = self._select_album
self.do_more(None)
def _select_artist(self, artist):
print "I don't know what to do with artists."
def _show_artists(self, artists):
i = self._results_idx + 1
for a in artists:
print " [%3d] %s" % (i, a.name)
i += 1
def do_artist(self, rest):
"""Search for artists with the given name."""
self._more = self._show_artists
self._results = self._client.search_artist(rest)
self._results_idx = 0
self._select = self._select_artist
self.do_more(None)
def do_login(self, rest):
"""Log in (prompts for username and password)."""
if self._client.user_id is None:
sys.stdout.write("Username: ")
user = sys.stdin.readline().strip().rstrip()
password = getpass.getpass()
self._client.login(user, password)
else:
print "You are already logged in."
def do_logout(self, rest):
"""Log out and clear current session."""
self._client.new_session()
def do_quit(self, rest):
sys.exit(0)
def do_more(self, rest):
"""Show more search results."""
if self._more:
if self._results_idx < len(self._results):
self._more(self._results[self._results_idx:self._results_idx + 30])
self._results_idx += 30
else:
print "No more search results."
else:
print "No search results."
def _select_playlist(self, pl):
print "I don't know what to do with playlists."
def _show_playlists(self, pls):
i = self._results_idx + 1
for pl in pls:
print " [%3d] %s" % (i, pl.name)
i += 1
def do_playlists(self, rest):
"""Show user playlists."""
self._more = self._show_playlists
self._results = self._client.get_playlists()
self._results_idx = 0
self._select = self._select_playlist
self.do_more(None)
def do_select(self, rest):
"""Select the given index from the last search results."""
index = int(rest.strip().rstrip())
if self._select:
if index >= 1 and index <= len(self._results):
self._select(self._results[index - 1])
else:
print "Invalid index."
else:
print "No search results."
def _select_song(self, song):
(url, postdata) = self._client.get_stream(song)
opener = urllib.URLopener()
stream = opener.open(url, data = postdata)
subprocess.call(['mplayer', '-cache', '2048', '-'], stdin = stream)
def _show_songs(self, songs):
i = self._results_idx + 1
artist_max = max([len(s.artist.name) for s in songs])
album_max = max([len(s.album.title) for s in songs])
format = " [%%3d] %%%ds - %%%ds - %%s" % (artist_max, album_max)
for s in songs:
print format % (i, s.artist.name, s.album.title, s.title)
i += 1
def do_song(self, rest):
"""Search for songs with the given title."""
self._more = self._show_songs
self._results = self._client.search_song(rest)
self._results_idx = 0
self._select = self._select_song
self.do_more(None)
if __name__ == '__main__':
MainCmd().cmdloop()