Initial commit and design
This commit is contained in:
commit
1b7933187e
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/default_config.py
|
||||
__pycache__/
|
||||
*.pyc
|
68
api.py
Normal file
68
api.py
Normal file
@ -0,0 +1,68 @@
|
||||
import requests, urllib
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
|
||||
# (TODO)Move to config?
|
||||
BASE_URL = Path('http://bokhylle.pvv.ntnu.no:8080/api')
|
||||
|
||||
# Exceptions:
|
||||
class APIError(Exception): pass
|
||||
|
||||
# decorator:
|
||||
def request_post(func):
|
||||
@wraps(func)
|
||||
def new_func(*args, **kwargs):
|
||||
url, data = func(*args, **kwargs)
|
||||
response = requests.post(url, data=data)
|
||||
json = json.loads(response.text)
|
||||
if "error" not in json or json["error"] != False:
|
||||
raise APIError(json["error_msg"])
|
||||
return json["success"]
|
||||
return new_func
|
||||
def request_get(func):
|
||||
@wraps(func)
|
||||
def new_func(*args, **kwargs):
|
||||
url = func(*args, **kwargs)
|
||||
response = requests.get(url)
|
||||
json = json.loads(response.text)
|
||||
if "error" not in json or json["error"] != False:
|
||||
raise APIError(json["error_msg"])
|
||||
return json["value"]
|
||||
return new_func
|
||||
|
||||
# methods:
|
||||
|
||||
@request_post
|
||||
def is_playing(path:str):
|
||||
args = urllib.urlencode(locals())
|
||||
return BASE_URL / f"play?{args}", None
|
||||
|
||||
@request_get
|
||||
def is_playing():
|
||||
return BASE_URL / f"play"
|
||||
|
||||
@request_post
|
||||
def set_playing(play:bool):
|
||||
args = urllib.urlencode(locals())
|
||||
return BASE_URL / f"play?{args}", None
|
||||
|
||||
@request_get
|
||||
def get_volume():
|
||||
return BASE_URL / f"volume"
|
||||
|
||||
@request_post
|
||||
def set_volume(volume:int):# between 0 and 100 (you may also exceed 100)
|
||||
args = urllib.urlencode(locals())
|
||||
return BASE_URL / f"volume?{args}", None
|
||||
|
||||
@request_get
|
||||
def get_playlist():
|
||||
return BASE_URL / f"playlist"
|
||||
|
||||
@request_post
|
||||
def playlist_next():
|
||||
return BASE_URL / f"playlist/next", None
|
||||
|
||||
@request_post
|
||||
def playlist_previous():
|
||||
return BASE_URL / f"playlist/previous", None
|
4
config.py
Normal file
4
config.py
Normal file
@ -0,0 +1,4 @@
|
||||
host = "0.0.0.0"
|
||||
port = 8080
|
||||
start_browser = False
|
||||
multiple_instance = True
|
142
main.py
Executable file
142
main.py
Executable file
@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
import random, os, time, shutil, sys
|
||||
from threading import Timer
|
||||
import remi.gui as gui
|
||||
from remi import start, App
|
||||
from utils import Namespace
|
||||
|
||||
#globals:
|
||||
COLOR_BLUE = "rgb(33, 150, 243)"
|
||||
COLOR_BLUE_SHADOW = "rgba(33, 150, 243, 0.75)"
|
||||
|
||||
class MyApp(App):
|
||||
def __init__(self, *args):
|
||||
res_path = os.path.join(os.path.dirname(__file__), 'res')
|
||||
super(MyApp, self).__init__(*args, static_file_path=res_path)
|
||||
|
||||
def main(self):
|
||||
container = gui.VBox(width=512)
|
||||
container.style["margin-left"] = "auto"
|
||||
container.style["margin-right"] = "auto"
|
||||
|
||||
#logo:
|
||||
container.append(gui.Image('/res/logo.jpg', width=512))
|
||||
|
||||
#playback controls
|
||||
playbackContainer = gui.HBox()#; container.append(playbackContainer)
|
||||
|
||||
self.playback = Namespace()
|
||||
for i in ("previous", "play", "next"):
|
||||
button = gui.Button(i.capitalize(), margin="5px")
|
||||
setattr(self.playback, i, button)
|
||||
playbackContainer.append(button)
|
||||
button.set_on_click_listener(getattr(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')
|
||||
|
||||
container.append(self.playback.playing)
|
||||
container.append(playbackContainer)
|
||||
container.append(self.playback.slider)
|
||||
|
||||
#playlist
|
||||
self.playlist = Namespace()
|
||||
self.playlist.table = gui.Table(width="100%", margin="10px")
|
||||
self.playlist.table.append_from_list([['#', 'Name', "length"]], fill_title=True)
|
||||
|
||||
container.append(self.playlist.table)
|
||||
|
||||
self.playlist.queue = []#[i] = [source, name, length]
|
||||
|
||||
#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)
|
||||
|
||||
inputContainer.append(self.input.field)
|
||||
inputContainer.append(self.input.submit)
|
||||
container.append(inputContainer)
|
||||
|
||||
#return the container
|
||||
self.mainLoop()
|
||||
return container
|
||||
def mainLoop(self):
|
||||
#self.playback.slider.get_value()
|
||||
|
||||
self.playback_update()
|
||||
|
||||
|
||||
self.playlist.table.empty(keep_title=True)
|
||||
self.playlist.table.append_from_list(self.playlist_update())
|
||||
|
||||
Timer(0.7, self.mainLoop).start()
|
||||
|
||||
# events:
|
||||
def playback_previous(self, widget): pass
|
||||
def playback_play(self, widget):# toggle playblack
|
||||
pass
|
||||
def playback_next(self, widget):
|
||||
source, name, length = self.playlist.queue.pop(0)
|
||||
|
||||
pass
|
||||
def input_submit(self, widget, value=None):
|
||||
if not value:
|
||||
value = self.input.field.get_text()
|
||||
self.input.field.set_text("")
|
||||
|
||||
title, length = get_youtube_metadata(value)
|
||||
|
||||
self.playlist.queue.append([value, title, length])
|
||||
|
||||
# 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)
|
||||
|
||||
return
|
||||
def playlist_update(self):
|
||||
#out = [['#', 'Name', "length"]]
|
||||
out = []
|
||||
for i, (source, name, length) in enumerate(self.playlist.queue):
|
||||
out.append([str(i+1), name, length])
|
||||
|
||||
return out
|
||||
|
||||
|
||||
# config must be a object with the attributes::
|
||||
# config.host: str
|
||||
# config.port: str
|
||||
# config.start_browser: bool
|
||||
# config.multiple_instance: bool
|
||||
def main(config):
|
||||
assert hasattr(config, "host")
|
||||
assert hasattr(config, "port")
|
||||
assert hasattr(config, "start_browser")
|
||||
assert hasattr(config, "multiple_instance")
|
||||
|
||||
# start the webserver:
|
||||
start(
|
||||
MyApp,
|
||||
title = "Gregorz",
|
||||
address = config.host,
|
||||
port = config.port,
|
||||
start_browser = config.start_browser,
|
||||
multiple_instance = config.multiple_instance,
|
||||
enable_file_cache = True
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not os.path.exists("config.py"):
|
||||
shutil.copy("default_config.py", "config.py")
|
||||
import config
|
||||
main(config)
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
youtube-dl
|
||||
https://github.com/dddomodossola/remi/archive/v1.0.tar.gz
|
BIN
res/logo.jpg
Normal file
BIN
res/logo.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.7 KiB |
47
res/pvv logo.svg
Normal file
47
res/pvv logo.svg
Normal file
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
||||
viewBox="0 0 800 800" style="enable-background:new 0 0 800 800;" xml:space="preserve">
|
||||
<style type="text/css">
|
||||
.st0{fill:#FFFFFF;}
|
||||
.st1{fill:none;stroke:#FFFFFF;stroke-width:2;stroke-miterlimit:10;}
|
||||
.st2{fill:none;stroke:#000000;stroke-width:2;stroke-miterlimit:10;}
|
||||
.st3{fill:none;}
|
||||
.st4{stroke:#000000;stroke-miterlimit:10;}
|
||||
.st5{font-family:'OCRAStd';}
|
||||
.st6{font-size:126px;}
|
||||
</style>
|
||||
<g id="Layer_2">
|
||||
<rect y="0" class="st0" width="800" height="800"/>
|
||||
</g>
|
||||
<g id="Layer_4">
|
||||
<path class="st1" d="M294.6,720.3"/>
|
||||
<line class="st1" x1="478.4" y1="720.3" x2="313.2" y2="720.3"/>
|
||||
<path class="st1" d="M478.4,720.3"/>
|
||||
<polyline class="st2" points="717.1,223.3 717.1,720.3 497.3,720.3 "/>
|
||||
<path class="st2" d="M498.3,720.3c0-5.6-4.5-10.1-10.1-10.1c-5.6,0-10.1,4.5-10.1,10.1H314.3c0-5.6-4.5-10.1-10.1-10.1
|
||||
c-5.6,0-10.1,4.5-10.1,10.1h0.6H76.5V79.7h640.5v120.8v-0.8h-17.3v24.8h17.3"/>
|
||||
</g>
|
||||
<g id="Layer_3">
|
||||
<circle class="st2" cx="396.8" cy="400" r="320.3"/>
|
||||
</g>
|
||||
<g id="Layer_1">
|
||||
<polyline class="st2" points="514.5,173.5 170.2,173.5 170.3,626.6 623.3,626.5 623.3,215.7 584.4,173.4 557,173.4 548,180.6
|
||||
526.5,180.7 "/>
|
||||
<path class="st1" d="M396.8,173.5"/>
|
||||
<path class="st1" d="M396.8,173.3"/>
|
||||
<path class="st2" d="M526.5,331.8c0,7.6-5.4,13.7-12,13.7H227.7c-6.6,0-12-6.1-12-13.7V187.2c0-7.6,5.4-13.7,12-13.7h286.8
|
||||
c6.6,0,12,6.1,12,13.7V331.8z"/>
|
||||
<path class="st2" d="M526.7,333.6c0,6.6-5.4,12-12,12H296.8c-6.6,0-12-5.4-12-12V185.5c0-6.6,5.4-12,12-12h217.9
|
||||
c6.6,0,12,5.4,12,12V333.6z"/>
|
||||
<path class="st2" d="M577.9,613.7c0,6.6-5.4,12-12,12H227.7c-6.6,0-12-5.4-12-12V381.1c0-6.6,5.4-12,12-12h338.2
|
||||
c6.6,0,12,5.4,12,12V613.7z"/>
|
||||
<rect x="179.9" y="590.2" class="st2" width="25.7" height="23"/>
|
||||
<rect x="587.6" y="590.2" class="st2" width="25.7" height="23"/>
|
||||
<rect x="433.6" y="193.5" class="st2" width="64.9" height="137.8"/>
|
||||
</g>
|
||||
<g id="Layer_5">
|
||||
<rect x="258" y="442.5" class="st3" width="277.5" height="109.7"/>
|
||||
<text transform="matrix(1 0 0 1 260.7021 547.998)" class="st4 st5 st6">PVV</text>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 2.2 KiB |
27
utils.py
Normal file
27
utils.py
Normal file
@ -0,0 +1,27 @@
|
||||
import threading
|
||||
import youtube_dl
|
||||
|
||||
class Namespace(object): pass
|
||||
|
||||
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)
|
||||
|
||||
# decorator:
|
||||
def call_as_thread(func):
|
||||
def new_func(*args, **kwargs):
|
||||
threading.Thread(
|
||||
target = func,
|
||||
args = args,
|
||||
kwargs = kwargs
|
||||
).start()
|
||||
return new_func
|
Loading…
Reference in New Issue
Block a user