2018-02-27 00:06:15 +01:00
|
|
|
import random, os, time, shutil, sys
|
|
|
|
from threading import Timer
|
|
|
|
import remi.gui as gui
|
|
|
|
from remi import App
|
2018-03-04 04:09:09 +01:00
|
|
|
from .utils import Namespace, call_as_thread, get_youtube_metadata, seconds_to_timestamp
|
2018-03-03 00:41:14 +01:00
|
|
|
from . import api
|
2018-03-05 21:48:11 +01:00
|
|
|
from .colors import *
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#globals:
|
2018-03-01 13:56:31 +01:00
|
|
|
WIDTH = 512
|
2018-02-27 00:06:15 +01:00
|
|
|
|
2018-03-04 04:02:49 +01:00
|
|
|
class RemiApp(App):
|
2018-02-27 00:06:15 +01:00
|
|
|
def __init__(self, *args):
|
|
|
|
res_path = os.path.join(os.path.dirname(__file__), 'res')
|
2018-03-04 04:02:49 +01:00
|
|
|
super(RemiApp, self).__init__(*args, static_file_path=res_path)
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
def main(self):
|
2018-03-01 13:56:31 +01:00
|
|
|
container = gui.VBox(width=WIDTH)
|
|
|
|
container.style.update({"margin-left": "auto", "margin-right":"auto"})
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#logo:
|
2018-03-01 13:56:31 +01:00
|
|
|
container.append(gui.Image('/res/logo.png', width=WIDTH))
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#playback controls
|
|
|
|
self.playback = Namespace()
|
2018-03-01 13:55:50 +01:00
|
|
|
|
|
|
|
self.playback.playing = gui.Label("Now playing: None")# (TODO): update this
|
|
|
|
|
|
|
|
self.playback.previous, self.playback.play, self.playback.next \
|
|
|
|
= map(lambda x: gui.Button(f'<i class="fas fa-{x}"></i>', margin="3px", width="2.8em"),
|
|
|
|
("step-backward", "play", "step-forward"))
|
|
|
|
self.playback.previous.set_on_click_listener(self.playback_previous)
|
|
|
|
self.playback.play.set_on_click_listener(self.playback_play)
|
|
|
|
self.playback.next.set_on_click_listener(self.playback_next)
|
2018-02-27 00:06:15 +01:00
|
|
|
|
2018-02-28 22:17:44 +01:00
|
|
|
self.playback.volume_label = gui.Label("Volume:")
|
|
|
|
self.playback.volume_label.style["font-size"] = "0.8em"
|
|
|
|
self.playback.volume_slider = gui.Slider(100, 0, 100, 1, width="150px")
|
2018-03-01 13:56:31 +01:00
|
|
|
self.playback.volume_slider \
|
|
|
|
.style.update({"margin-left": "20px", "margin-bottom":"13px"})
|
2018-02-28 22:17:44 +01:00
|
|
|
self.playback.volume_slider.set_oninput_listener(self.change_volume)
|
|
|
|
|
|
|
|
self.playback.seek_slider = gui.Slider(0, 0, 100, 1, width="85%", height=20, margin='10px')
|
|
|
|
self.playback.seek_slider.set_oninput_listener(self.change_seek)
|
|
|
|
|
2018-03-04 04:09:09 +01:00
|
|
|
self.playback.timestamp = gui.Label("--:-- - --:--")
|
|
|
|
|
2018-03-01 13:55:50 +01:00
|
|
|
container.append(self.playback.playing)
|
|
|
|
|
2018-03-04 12:02:16 +01:00
|
|
|
playback_container = gui.HBox()
|
|
|
|
playback_container.append(self.playback.previous)
|
|
|
|
playback_container.append(self.playback.play)
|
|
|
|
playback_container.append(self.playback.next)
|
2018-02-28 22:17:44 +01:00
|
|
|
volume_container = gui.VBox()
|
|
|
|
volume_container.append(self.playback.volume_label)
|
|
|
|
volume_container.append(self.playback.volume_slider)
|
2018-03-04 12:02:16 +01:00
|
|
|
playback_container.append(volume_container)
|
|
|
|
container.append(playback_container)
|
2018-02-28 22:17:44 +01:00
|
|
|
container.append(self.playback.seek_slider)
|
2018-03-04 04:09:09 +01:00
|
|
|
container.append(self.playback.timestamp)
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#playlist
|
|
|
|
self.playlist = Namespace()
|
|
|
|
self.playlist.table = gui.Table(width="100%", margin="10px")
|
2018-03-05 21:48:11 +01:00
|
|
|
self.playlist.table.append_from_list([['#', 'Name', "length", "", "", ""]], fill_title=True)
|
2018-03-05 21:48:48 +01:00
|
|
|
self.playlist.clear = gui.Button("CLEAR", height="1.8em")
|
|
|
|
self.playlist.clear.set_on_click_listener(self.on_playlist_clear_click)
|
|
|
|
self.playlist.clear.style["font-size"] = "0.8em"
|
|
|
|
self.playlist.clear.style["margin-left"] = "auto"
|
|
|
|
self.playlist.clear.style["margin-right"] = "0.5em"
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
container.append(self.playlist.table)
|
2018-03-05 21:48:48 +01:00
|
|
|
container.append(self.playlist.clear)
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#input
|
2018-03-05 21:48:11 +01:00
|
|
|
container.append(gui.Label("Add song:"))
|
2018-03-04 12:02:16 +01:00
|
|
|
input_container = gui.HBox(width=WIDTH)
|
2018-02-27 00:06:15 +01:00
|
|
|
self.input = Namespace()
|
|
|
|
self.input.field = gui.TextInput(single_line=True, height="20px", margin="5px")
|
|
|
|
self.input.field.style["border"] = "1px solid %s" % COLOR_BLUE
|
|
|
|
self.input.field.style["box-shadow"] = "0px 0px 5px 0px %s" % COLOR_BLUE_SHADOW
|
|
|
|
self.input.submit = gui.Button("Submit!", margin="5px")
|
|
|
|
self.input.field.set_on_enter_listener(self.input_submit)
|
|
|
|
self.input.submit.set_on_click_listener(self.input_submit)
|
|
|
|
|
2018-03-04 12:02:16 +01:00
|
|
|
input_container.append(self.input.field)
|
|
|
|
input_container.append(self.input.submit)
|
|
|
|
container.append(input_container)
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
#return the container
|
|
|
|
self.mainLoop()
|
|
|
|
return container
|
|
|
|
def mainLoop(self):
|
2018-02-28 22:17:44 +01:00
|
|
|
#self.playback.seek_slider.get_value()
|
2018-02-27 00:06:15 +01:00
|
|
|
self.playback_update()
|
|
|
|
self.playlist_update()
|
|
|
|
|
2018-02-28 22:17:44 +01:00
|
|
|
Timer(1, self.mainLoop).start()
|
2018-02-27 00:06:15 +01:00
|
|
|
|
|
|
|
# events:
|
|
|
|
@call_as_thread
|
|
|
|
def playback_previous(self, widget):
|
|
|
|
api.playlist_previous()
|
|
|
|
@call_as_thread
|
|
|
|
def playback_play(self, widget):# toggle playblack
|
|
|
|
if api.is_playing():
|
|
|
|
api.set_playing(False)
|
2018-02-28 22:17:44 +01:00
|
|
|
self.set_playing(False)
|
2018-02-27 00:06:15 +01:00
|
|
|
else:
|
|
|
|
api.set_playing(True)
|
2018-02-28 22:17:44 +01:00
|
|
|
self.set_playing(True)
|
2018-02-27 00:06:15 +01:00
|
|
|
@call_as_thread
|
|
|
|
def playback_next(self, widget):
|
|
|
|
api.playlist_next()
|
|
|
|
@call_as_thread
|
|
|
|
def input_submit(self, widget, value=None):
|
|
|
|
if value is None:
|
|
|
|
value = self.input.field.get_text()
|
|
|
|
self.input.field.set_text("")
|
|
|
|
|
2018-03-04 04:08:20 +01:00
|
|
|
self.input.field.set_enabled(False)
|
|
|
|
self.input.submit.set_enabled(False)
|
|
|
|
try:
|
|
|
|
data = get_youtube_metadata(value)
|
|
|
|
finally:
|
|
|
|
self.input.field.set_enabled(True)
|
|
|
|
self.input.submit.set_enabled(True)
|
|
|
|
|
|
|
|
api.load_path(value, data)
|
2018-02-28 22:17:44 +01:00
|
|
|
@call_as_thread
|
|
|
|
def change_seek(self, widget, value):
|
|
|
|
api.seek_percent(value)
|
|
|
|
@call_as_thread
|
|
|
|
def change_volume(self, widget, value):
|
|
|
|
api.set_volume(value)
|
2018-03-05 21:48:48 +01:00
|
|
|
@call_as_thread
|
2018-03-04 04:08:20 +01:00
|
|
|
def on_table_row_click(self, row_widget, playlist_item):
|
2018-03-05 21:48:11 +01:00
|
|
|
print("row", playlist_item)
|
|
|
|
@call_as_thread
|
|
|
|
def on_table_item_move_click(self, row_widget, playlist_item, down = True):
|
|
|
|
index = playlist_item["index"]
|
|
|
|
dest = index + 2 if down else index-1
|
|
|
|
api.playlist_move(index, dest)
|
|
|
|
@call_as_thread
|
|
|
|
def on_table_item_remove_click(self, row_widget, playlist_item):
|
|
|
|
api.playlist_remove(playlist_item["index"])
|
|
|
|
@call_as_thread
|
|
|
|
def on_playlist_clear_click(self, row_widget):
|
|
|
|
api.playlist_clear()
|
|
|
|
|
2018-02-27 00:06:15 +01:00
|
|
|
# playback steps:
|
|
|
|
@call_as_thread
|
2018-02-28 22:17:44 +01:00
|
|
|
def playback_update(self, times_called=[0]):
|
|
|
|
is_playing = api.is_playing()
|
|
|
|
self.set_playing(is_playing)
|
|
|
|
|
|
|
|
if is_playing:
|
2018-03-04 04:08:20 +01:00
|
|
|
try:
|
|
|
|
playback_pos = api.get_playback_pos()
|
|
|
|
except api.APIError:
|
|
|
|
playback_pos = None
|
|
|
|
if playback_pos:
|
|
|
|
slider_pos = playback_pos["current"] / playback_pos["total"] * 100
|
|
|
|
if self.playback.seek_slider.get_value() != slider_pos:
|
|
|
|
self.playback.seek_slider.set_value(slider_pos)
|
2018-03-04 04:09:09 +01:00
|
|
|
self.playback.timestamp.set_text(
|
|
|
|
seconds_to_timestamp(playback_pos["current"])
|
|
|
|
+ " - " +
|
|
|
|
seconds_to_timestamp(playback_pos["total"])
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.playback.timestamp.set_text("--:-- - --:--")
|
2018-03-04 04:08:20 +01:00
|
|
|
|
2018-02-28 22:17:44 +01:00
|
|
|
if times_called[0] % 5 == 0:
|
|
|
|
volume = api.get_volume()
|
|
|
|
if volume > 100: volume = 100
|
|
|
|
if self.playback.volume_slider.get_value() != volume:
|
|
|
|
self.playback.volume_slider.set_value(volume)
|
|
|
|
times_called[0] += 1
|
2018-02-27 00:06:15 +01:00
|
|
|
@call_as_thread
|
|
|
|
def volume_update(self):
|
|
|
|
self.volume.slider.set_value(api.get_volume())
|
|
|
|
@call_as_thread
|
|
|
|
def playlist_update(self):
|
|
|
|
playlist = api.get_playlist()
|
|
|
|
|
2018-03-04 04:09:09 +01:00
|
|
|
N = len(playlist)
|
2018-02-27 00:06:15 +01:00
|
|
|
table = []
|
2018-03-04 04:09:09 +01:00
|
|
|
for i, playlist_item in enumerate(playlist):
|
2018-03-04 04:08:20 +01:00
|
|
|
name = playlist_item["filename"]
|
|
|
|
length = "--:--"
|
|
|
|
if "data" in playlist_item:
|
|
|
|
if "title" in playlist_item["data"]:
|
|
|
|
name = playlist_item["data"]["title"]
|
|
|
|
if "length" in playlist_item["data"]:
|
|
|
|
length = playlist_item["data"]["length"]
|
|
|
|
|
2018-03-04 04:09:09 +01:00
|
|
|
if playlist_item.get("current", False):
|
|
|
|
self.playback.previous.set_enabled(i != 0)
|
|
|
|
self.playback.next.set_enabled(i != N-1)
|
|
|
|
|
2018-02-27 00:06:15 +01:00
|
|
|
table.append([
|
2018-03-04 04:08:20 +01:00
|
|
|
playlist_item["index"],
|
|
|
|
name,
|
|
|
|
length,
|
2018-03-05 21:48:11 +01:00
|
|
|
'<i class="fas fa-arrow-up"></i>',
|
|
|
|
'<i class="fas fa-arrow-down"></i>',
|
|
|
|
'<i class="fas fa-trash"></i>',
|
2018-02-27 00:06:15 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
self.playlist.table.empty(keep_title=True)
|
|
|
|
self.playlist.table.append_from_list(table)
|
2018-03-04 04:08:20 +01:00
|
|
|
|
|
|
|
for row_widget, playlist_item in zip(
|
|
|
|
map(self.playlist.table.get_child, self.playlist.table._render_children_list[1:]),
|
|
|
|
playlist):
|
|
|
|
if "current" in playlist_item:
|
|
|
|
row_widget.style["background-color"] = COLOR_LIGHT_BLUE
|
|
|
|
else:
|
2018-03-05 21:48:11 +01:00
|
|
|
row_widget.style["color"] = COLOR_GRAY_DARK
|
2018-03-04 04:08:20 +01:00
|
|
|
row_widget.set_on_click_listener(self.on_table_row_click, playlist_item)
|
2018-03-05 21:48:11 +01:00
|
|
|
for index, (key, item_widget) in enumerate(zip(row_widget._render_children_list,
|
|
|
|
map(row_widget.get_child, row_widget._render_children_list))):
|
|
|
|
if index >= 3:
|
|
|
|
item_widget.style["width"] = "1.1em"
|
|
|
|
item_widget.style["color"] = COLOR_TEAL
|
|
|
|
if index == 3:
|
|
|
|
item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, False)
|
|
|
|
if playlist_item["index"] == 0:
|
|
|
|
item_widget.style["color"] = COLOR_GRAY_LIGHT
|
|
|
|
if index == 4:
|
|
|
|
item_widget.set_on_click_listener(self.on_table_item_move_click, playlist_item, True)
|
|
|
|
if playlist_item["index"] == N-1:
|
|
|
|
item_widget.style["color"] = COLOR_GRAY_LIGHT
|
|
|
|
if index == 5:
|
|
|
|
item_widget.style["color"] = COLOR_RED
|
|
|
|
item_widget.set_on_click_listener(self.on_table_item_remove_click, playlist_item)
|
|
|
|
#print(index, key, item_widget)
|
2018-03-04 04:08:20 +01:00
|
|
|
|
2018-02-28 22:17:44 +01:00
|
|
|
#helpers
|
|
|
|
def set_playing(self, is_playing:bool):
|
2018-03-01 13:55:50 +01:00
|
|
|
self.playback.play.set_text('<i class="fas fa-pause"></i>' if is_playing else '<i class="fas fa-play"></i>')
|
2018-02-28 22:17:44 +01:00
|
|
|
self.playback.seek_slider.set_enabled(is_playing)
|
|
|
|
|