grzegorz/server.py

147 lines
4.3 KiB
Python
Raw Normal View History

2016-09-11 20:45:37 +02:00
#!/usr/bin/env python3
import remi.gui as gui, random, os, time, shutil, sys
2016-09-11 18:20:14 +02:00
from remi import start, App
2016-09-11 20:45:37 +02:00
from threading import Timer
if sys.version_info[0] == 2:
from ConfigParser import ConfigParser
else:
from configparser import ConfigParser
if "--no-mpv" not in sys.argv:
from mpv_control import mpv
2016-09-11 20:45:37 +02:00
class namespace(object): pass
#globals:
COLOR_BLUE = "rgb(33, 150, 243)"
COLOR_BLUE_SHADOW = "rgba(33, 150, 243, 0.75)"
2016-09-11 18:20:14 +02:00
2017-02-05 15:16:00 +01:00
#todo: move this functionality to mpv_control.py or its own file, managing playlists and shit?
import youtube_dl
def get_youtube_metadata(url, ydl = youtube_dl.YoutubeDL()):
#todo: check if url is valid
#todo, stop it from doung the whole playlist
resp = ydl.extract_info(url, download=False)
#print resp.keys()
title = resp.get('title')
length = resp.get('duration')
#print( title, "%i:%.2i" % (length//60, length%60))
return title, "%i:%.2i" % (length//60, length%60)
2016-09-11 18:20:14 +02:00
class MyApp(App):
def __init__(self, *args):
2016-09-11 20:45:37 +02:00
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
super(MyApp, self).__init__(*args, static_paths=(res_path,))
2017-02-05 15:16:00 +01:00
#initalize mpv here?
2016-09-11 18:20:14 +02:00
def main(self):
2016-09-11 20:45:37 +02:00
container = gui.VBox(width=512)
#logo:
container.append(gui.Image('/res/logo.jpg', width=512))
2016-09-11 18:20:14 +02:00
#playback controls
2016-09-11 20:45:37 +02:00
playbackContainer = gui.HBox()#; container.append(playbackContainer)
self.playback = namespace()
for i in ("play", "pause", "next"):
button = gui.Button(i.capitalize(), margin="5px")
setattr(self.playback, i, button)
playbackContainer.append(button)
button.set_on_click_listener(self, 'playback_%s' % i)
self.playback.playing = gui.Label("Now playing: None")
self.playback.slider = gui.Slider(0, 0, 100, 1, width="85%", height=20, margin='10px')
2016-09-11 18:20:14 +02:00
2016-09-11 20:45:37 +02:00
container.append(self.playback.playing)
container.append(playbackContainer)
container.append(self.playback.slider)
2016-09-11 18:20:14 +02:00
#playlist
2016-09-11 20:45:37 +02:00
self.playlist = namespace()
self.playlist.table = gui.Table(width="100%", margin="10px")
self.playlist.table.from_2d_matrix([['#', 'Name', "length"]])
container.append(self.playlist.table)
2016-09-11 18:20:14 +02:00
2017-02-05 15:16:00 +01:00
self.playlist.queue = []#[i] = [source, name, length]
2016-09-11 20:45:37 +02:00
#input
container.append(gui.Label("Add songs:"))
inputContainer = gui.HBox(width=512)
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")
2016-09-11 18:20:14 +02:00
2016-09-11 20:45:37 +02:00
inputContainer.append(self.input.field)
inputContainer.append(self.input.submit)
container.append(inputContainer)
2016-09-11 18:20:14 +02:00
2016-09-11 20:45:37 +02:00
#return the container
self.mainLoop()
2016-09-11 18:20:14 +02:00
return container
2016-09-11 20:45:37 +02:00
def mainLoop(self):
#self.playback.slider.get_value()
2017-02-05 15:16:00 +01:00
self.playback_update()
self.playlist.table.from_2d_matrix(self.playlist_update())
2016-09-11 20:45:37 +02:00
Timer(1, self.mainLoop).start()
def playback_play(self): pass
def playback_pause(self): pass
2017-02-05 15:16:00 +01:00
def playback_next(self):
source, name, length = self.playlist.queue.pop(0)
pass
2016-09-11 20:45:37 +02:00
def input_submit(self, value=None):
if not value:
value = self.input.field.get_text()
2017-02-05 15:16:00 +01:00
title, length = get_youtube_metadata(value)
2016-09-11 20:45:37 +02:00
self.input.field.set_text("")
2017-02-05 15:16:00 +01:00
#playback steps:
def playback_update(self):
#talk to mpv, see wether the song is being played still
if 0:#if done:
self.playback_next()
self.playback.slider.set_value(0)
else:
self.playback.slider.set_value(100)
2017-02-05 15:16:00 +01:00
return
def playlist_update(self):
out = [['#', 'Name', "length"]]
2016-09-11 18:20:14 +02:00
2017-02-05 15:16:00 +01:00
for i, (source, name, length) in enumerate(self.playlist.queue):
out.append([str(i+1), name, length])
return out
def main():
if not os.path.exists("config.ini"):
shutil.copyfile("default_config.ini", "config.ini")
ini = ConfigParser()
with open("config.ini", "r") as f:
ini.readfp(f)
host = ini.get("server", "host")
port = ini.getint("server", "port")
start_browser = ini.getboolean("server", "start_browser")
multiple_instance = ini.getboolean("server", "multiple_instance")
# starts the webserver
start(MyApp, address=host, port=port, start_browser=start_browser, multiple_instance=multiple_instance)
if __name__ == "__main__":
main()