-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
105 lines (89 loc) · 2.93 KB
/
spotify.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
from mpd import MPDClient
import sys
class Spotify(object):
def __init__(self):
self.client = MPDClient()
self.client.timeout = 10
self.client.idletimeout = None
def connect(self):
self.client.connect("localhost", 6600)
def isConnected(self):
try:
self.client.currentsong()
except:
return False
return True
# This is needed because the connection to mpd is very inconsistent
def connectIfNC(self):
if not self.isConnected():
self.connect()
def playSong(self, song_name):
self.connectIfNC()
self.client.clear()
self.connectIfNC()
self.client.searchadd("any", song_name)
self.connectIfNC()
print(self.client.playlistinfo())
try:
self.client.play(0)
except:
self.playLibrary()
def toggleMusic(self):
self.connectIfNC()
state = self.client.status().get('state', 'pause')
self.connectIfNC()
self.client.pause(1 if state == 'play' else 0)
def next(self):
self.connectIfNC()
self.client.next()
def stop(self):
self.connectIfNC()
self.client.stop()
# seek to exact time in seconds
def seekTo(self, time):
self.connectIfNC()
self.client.seekcur(time)
# seek relative to current location in song
def seek(self, time):
self.connectIfNC()
self.client.seekcur(int(float(self.client.status().get('elapsed'))) + time)
# volume in range [0, 100]
def setVol(self, vol):
self.connectIfNC()
self.client.setvol(vol)
def playLibrary(self, playlist=None):
playlist = playlist if playlist else "Greatest Hits Ever (by playlistmeukfeatured)"
self.connectIfNC()
self.client.stop()
self.connectIfNC()
self.client.clear()
self.connectIfNC()
self.client.load(playlist)
self.connectIfNC()
self.client.play(0)
if __name__ == '__main__':
spotify = Spotify()
while True:
try:
try:
print "\n\nMenu:\n1. Play Song\n2. Pause/Play \n3. Skip \n4. Seek \n5. Stop \n6. Play Library \nElse: Exit"
choice = int(raw_input("Choice: "))
if choice == 1:
spotify.playSong(raw_input("\n\nEnter Song name: "))
elif choice == 2:
spotify.toggleMusic()
elif choice == 3:
spotify.next()
elif choice == 4:
spotify.seek(int(raw_input("Enter time in seconds: ")))
elif choice == 5:
spotify.stop()
elif choice == 6:
spotify.playLibrary()
else:
print 'exit'
sys.exit(0)
except KeyboardInterrupt:
sys.exit(0)
except:
pass