-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackQueueActor.py
282 lines (239 loc) · 11.2 KB
/
TrackQueueActor.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import urllib, time, os
import subprocess
import os.path
from Queue import Queue
import logging
import pykka
import signal
import sys
from pytube import YouTube
class Player(pykka.ThreadingActor):
def __init__(self, mqtt_actor, queue_actor):
super(Player, self).__init__()
self.mqtt_actor = mqtt_actor
self.queue_actor = queue_actor
prev = None
startup_sound = '/usr/uproar/startup.mp3'
log_count = 0
def play(self, track, with_command, args, delete):
args = [] if args is None else args
args.insert(0, with_command)
args.insert(1, track)
p = subprocess.Popen(args)
self.queue_actor.tell({'command': 'playing_process', "p": p})
# TODO schedule kill
p.wait()
if self.prev is not None:
os.remove(self.prev)
print ('remove ' + self.prev)
if track != self.startup_sound and os.path.isfile(track) and delete:
self.prev = track
def play_and_report(self, message):
track = message.get('track')
track['message'] = 'playing'
self.mqtt_actor.tell({'command': 'update_track_status', 'track': track})
if message.get("file"):
self.play(message.get("file"), track.get("play_with"), track.get("args"), True)
elif message.get("track").get("url"):
if message.get("track").get("type") == "ytb":
self.play(message.get("file"), track.get("play_with"), track.get("args"), True)
track['message'] = 'done'
self.mqtt_actor.tell({'command': 'update_track_status', 'track': track})
self.check_queue()
def check_queue(self):
to_play = self.queue_actor.ask({'command': 'pop_play'})
if to_play is not None:
if to_play.get('action') == 'skip' or to_play.get('action') == 'promote':
self.check_queue()
else:
self.play_and_report(to_play)
def on_receive(self, message):
try:
if message.get('command') == 'check':
self.check_queue()
elif message.get('command') == 'startup':
if os.path.isfile(self.startup_sound):
self.play(self.startup_sound, "mpg123", [], False)
except Exception as ex:
logging.exception(ex)
class Downloader(pykka.ThreadingActor):
def __init__(self, mqtt_actor, queue_actor):
super(Downloader, self).__init__()
self.mqtt_actor = mqtt_actor
self.queue_actor = queue_actor
def download(self, track):
print ('download track: ' + track.get("title_safe"))
track['message'] = 'download'
self.mqtt_actor.tell({'command': 'update_track_status', 'track': track})
type = track.get("type")
if type == "track":
track_url = track.get('track_url')
# it's download a alot of stuff can happen! (omg, what a shitty shit)
try:
resp = urllib.urlretrieve(track_url,
str(track.get('count')) + '.mp3')
file = resp[0]
# print ('convert track to wav')
# song = AudioSegment.from_mp3(mp3_track)
# wav_track = str(self.count) + '.wav'
# song.export(wav_track, format='wav')
# os.remove(mp3_track)
track["play_with"] = "mpg123"
self.queue_actor.tell({'command': 'downloaded', 'track': track, 'file': file})
except Exception as ex:
print logging.exception(ex)
elif type == "ytb":
try:
print ('extracting ytb video link')
yt = YouTube(track.get("url"))
video = yt.streams.filter(file_extension = 'mp4').order_by('resolution').asc().first()
if video:
track["url"] = video.url
resp = urllib.urlretrieve(video.url,
str(track.get('count')) + '.mp4')
subprocess.Popen(["wget", "-O", str(track.get('count')) + '.mp4'])
_file = resp[0]
track["play_with"] = "mplayer"
track["args"] =["-framedrop"]
if 'darwin' in sys.platform:
track["args"].insert(0, "-fs")
else:
track["args"].insert(0, "-vo")
track["args"].insert(1, "null")
# track["kill"] = "killall -9 VLC"
self.queue_actor.tell({'command': 'downloaded', 'track': track, 'file': _file})
except Exception as ex:
print logging.exception(ex)
self.check_queue()
def check_queue(self):
to_download = self.queue_actor.ask({'command': 'pop_download'})
if to_download is not None:
if to_download.get('action') == 'skip' or to_download.get('action') == 'promote':
self.check_queue()
else:
self.download(to_download)
def on_receive(self, message):
if message.get('command') == 'check':
self.check_queue()
class TrackQueueActor(pykka.ThreadingActor):
def __init__(self, mqtt_actor):
super(TrackQueueActor, self).__init__()
self.mqtt_actor = mqtt_actor
self.download_queue = Queue()
self.player_queue = Queue()
self.download_queue_promoted = Queue()
self.player_queue_promoted = Queue()
self.count = 0
self.player = Player.start(mqtt_actor, self.actor_ref)
self.downloader = Downloader.start(mqtt_actor, self.actor_ref)
self.downloading = None
self.playing = None
self.skip_current_download = False
self.promote_current_download = False
self.p = None
self.boring = False
# def check_download(self):
# if self.downloadQueue.qsize() > 0:
# self.count += 1
#
# track = self.downloadQueue.get()
# track['count'] = self.count
# self.downloader.tell({'command':'download', 'track': track})
def on_move_action(self, orig, action):
track = None
if self.downloading is not None and self.downloading.get('orig') == orig:
if action == 'skip':
self.skip_current_download = True
self.promote_current_download = False
else:
self.skip_current_download = False
self.promote_current_download = True
track = self.downloading
elif self.playing is not None and self.playing.get('track').get('orig') == orig:
track = self.playing.get('track')
if action == 'skip' and self.p is not None:
if track.get("kill"):
os.system(track.get("kill"))
else:
self.p.terminate()
else:
for qp in self.player_queue.queue:
if qp.get('track').get('orig') == orig and qp.get("action") is None:
if action == "promote":
self.player_queue_promoted.put(qp.copy())
qp['action'] = action
track = qp.get('track')
for qd in self.download_queue.queue:
if qd.get('orig') == orig and qd.get("action") is None:
if action == "promote":
self.download_queue_promoted.put(qd.copy())
qd['action'] = action
track = qd
if track is not None:
track['message'] = action
self.mqtt_actor.tell({'command': 'update_track_status', 'track': track})
def on_receive(self, message):
try:
if message.get('command') == 'add_content':
track = message.get("content").get("audio")
ytb = message.get("content").get("youtube_link")
if track or ytb:
if track:
content = track
content["type"] = "track"
elif ytb:
content = ytb
content["type"] = "ytb"
content['title_safe'] = content.get('title').encode('ascii', 'ignore').decode(
'ascii')
print ('add content ' + content.get('title_safe') + ' to download queue')
self.download_queue.put(content)
self.boring = True
self.downloader.tell({'command': 'check'})
elif message.get('command') == 'pop_download':
self.downloading = None if self.download_queue_promoted.empty() else self.download_queue_promoted.get(
block=False)
if self.downloading is None:
self.downloading = None if self.download_queue.empty() else self.download_queue.get(block=False)
if self.downloading is not None:
self.count += 1
self.downloading['count'] = self.count
return self.downloading
elif message.get('command') == 'pop_play':
self.playing = None if self.player_queue_promoted.empty() else self.player_queue_promoted.get(
block=False)
if self.playing is None:
self.playing = None if self.player_queue.empty() else self.player_queue.get(block=False)
if self.playing is None and self.downloading is None and self.download_queue.empty() and self.download_queue_promoted.empty() and self.boring:
self.boring = False
self.mqtt_actor.tell({"command": "update", "update": "boring"})
return self.playing
# elif message.get('command') == 'check_download':
# self.check_download()
elif message.get('command') == 'startup':
self.player.tell(message)
elif message.get('command') == 'skip':
self.on_move_action(message.get('orig'), 'skip')
elif message.get('command') == 'promote':
self.on_move_action(message.get('orig'), 'promote')
# elif message.get('command') == 'check_player':
# self.check_player()
elif message.get('command') == 'downloaded':
if self.skip_current_download:
self.skip_current_download = False
else:
if self.promote_current_download or message.get("track").get("action") == "promote":
if self.promote_current_download:
self.promote_current_download = False
self.player_queue_promoted.put(message)
else:
self.player_queue.put(message)
track = message.get('track')
track['message'] = 'queue'
self.mqtt_actor.tell({'command': 'update_track_status', 'track': track})
print ('add track ' + message.get('track').get('title_safe') + ' to play queue')
self.player.tell({'command': 'check'})
elif message.get('command') == 'playing_process':
self.p = message.get('p')
except Exception as ex:
logging.exception(ex)