-
Notifications
You must be signed in to change notification settings - Fork 0
/
gplayer.py
112 lines (93 loc) · 3.06 KB
/
gplayer.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
import threading
import mpc
import time
from gmusicapi import Api
from random import randint
class PlayerUpdate(threading.Thread):
def __init__(self, player):
self.__player = player
self.__name = "Playlist Updater"
self.__running = True
threading.Thread.__init__(self)
def setRunning(self, running):
self.__running = running
def run(self):
while(self.__running):
current = mpc.sysCmd('mpc current')
if(len(current.strip()) == 0):
next = self.__player.next()
if next is None:
self.__running = False
else:
url = Player.api.get_stream_urls(next)[0]
mpc.telnetCmd('clear')
mpc.telnetCmd('add ' + str(url))
mpc.telnetCmd('play')
time.sleep(3)
class Player(object):
api = None
updater = None
current = None
playlist = None
random = False
def __init__(self, a):
Player.api = a
Player.updater = PlayerUpdate(self)
Player.random = True
def getCurrentSong(self):
return Player.current;
def setPlaylist(self, pl):
self.stop()
Player.playlist = pl
def stop(self):
Player.updater.setRunning(False)
mpc.telnetCmd('clear')
mpc.telnetCmd('stop')
return str(True);
def next(self):
index = 0
if Player.playlist is None:
return None
if Player.random:
index = randint(0, len(Player.playlist.songs))
else:
for track in Player.playlist.songs:
if(track.id != Player.current):
index+=1
else:
break
index+=1
if index < len(Player.playlist):
Player.current = Player.playlist.songs[index].id
else:
Player.current = None
return Player.current
def previous(self):
index = 0
if Player.playlist is None:
return None
for track in Player.playlist.songs:
if(track.id != Player.current):
index+=1;
else:
break
index-=1
if index >= 0:
Player.current = Player.playlist.songs[index].id
else:
Player.current = Player.playlist.songs[0].id
return Player.current
def playSong(self, songId=None):
self.stop()
if(songId is None):
Player.current = Player.playlist.songs[0].id
else:
Player.current = songId;
url = Player.api.get_stream_urls(Player.current)[0]
mpc.telnetCmd('clear')
mpc.telnetCmd('add ' + str(url))
mpc.telnetCmd('play')
Player.updater = PlayerUpdate(self)
Player.updater.setRunning(True)
Player.updater.start()
return Player.current