Make webui more responsive, add playlist metadata using youtube-dl

This commit is contained in:
Peder Bergebakken Sundt 2018-03-04 04:08:20 +01:00
parent 68f6f8a75b
commit d2a2be60f0
3 changed files with 82 additions and 20 deletions

View File

@ -17,6 +17,7 @@ def request_post(func):
@wraps(func) @wraps(func)
def new_func(*args, **kwargs): def new_func(*args, **kwargs):
url, data = func(*args, **kwargs) url, data = func(*args, **kwargs)
if type(data) is dict: data = json.dumps(data)
response = requests.post(f"{BASE_URL}/{url}", data=data) response = requests.post(f"{BASE_URL}/{url}", data=data)
data = json.loads(response.text) data = json.loads(response.text)
if "error" not in data or data["error"] != False: if "error" not in data or data["error"] != False:
@ -38,9 +39,9 @@ def request_get(func):
# methods: # methods:
@request_post @request_post
def load_path(path:str): def load_path(path:str, data:dict=None):
args = urlencode(locals()) args = urlencode(locals())
return f"load?{args}", None return f"load?{args}", data
@request_get @request_get
def is_playing(): def is_playing():

View File

@ -8,6 +8,8 @@ from . import api
#globals: #globals:
COLOR_BLUE = "rgb(33, 150, 243)" COLOR_BLUE = "rgb(33, 150, 243)"
COLOR_BLUE_SHADOW = "rgba(33, 150, 243, 0.75)" COLOR_BLUE_SHADOW = "rgba(33, 150, 243, 0.75)"
COLOR_LIGHT_BLUE = "#e3f2fd"
COLOR_GRAY = "#212529"
WIDTH = 512 WIDTH = 512
class RemiApp(App): class RemiApp(App):
@ -110,15 +112,23 @@ class RemiApp(App):
value = self.input.field.get_text() value = self.input.field.get_text()
self.input.field.set_text("") self.input.field.set_text("")
# (TODO): self.input.field.set_enabled(False)
# title, length = utils.get_youtube_metadata(value) self.input.submit.set_enabled(False)
api.load_path(value) try:
data = get_youtube_metadata(value)
finally:
self.input.field.set_enabled(True)
self.input.submit.set_enabled(True)
api.load_path(value, data)
@call_as_thread @call_as_thread
def change_seek(self, widget, value): def change_seek(self, widget, value):
api.seek_percent(value) api.seek_percent(value)
@call_as_thread @call_as_thread
def change_volume(self, widget, value): def change_volume(self, widget, value):
api.set_volume(value) api.set_volume(value)
def on_table_row_click(self, row_widget, playlist_item):
print(playlist_item)
# playback steps: # playback steps:
@call_as_thread @call_as_thread
@ -127,10 +137,14 @@ class RemiApp(App):
self.set_playing(is_playing) self.set_playing(is_playing)
if is_playing: if is_playing:
playback_pos = api.get_playback_pos() try:
playback_pos = playback_pos["current"] / playback_pos["total"] * 100 playback_pos = api.get_playback_pos()
if self.playback.seek_slider.get_value() != playback_pos: except api.APIError:
self.playback.seek_slider.set_value(playback_pos) 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)
if times_called[0] % 5 == 0: if times_called[0] % 5 == 0:
volume = api.get_volume() volume = api.get_volume()
@ -147,15 +161,34 @@ class RemiApp(App):
table = [] table = []
for item in playlist: for item in playlist:
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"]
table.append([ table.append([
item["index"], playlist_item["index"],
item["filename"], name,
"05:00" + ("#"*("current" in item)) length,
]) ])
self.playlist.table.empty(keep_title=True) self.playlist.table.empty(keep_title=True)
self.playlist.table.append_from_list(table) self.playlist.table.append_from_list(table)
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:
row_widget.style["color"] = "#444"#COLOR_GRAY
row_widget.set_on_click_listener(self.on_table_row_click, playlist_item)
for item_widget in map(row_widget.get_child, row_widget._render_children_list):
pass
#helpers #helpers
def set_playing(self, is_playing:bool): def set_playing(self, is_playing:bool):
self.playback.play.set_text('<i class="fas fa-pause"></i>' if is_playing else '<i class="fas fa-play"></i>') self.playback.play.set_text('<i class="fas fa-pause"></i>' if is_playing else '<i class="fas fa-play"></i>')

View File

@ -1,21 +1,49 @@
from functools import wraps from functools import wraps
from urllib.parse import urlsplit, urlunsplit, parse_qs, urlencode
import threading import threading
import youtube_dl import youtube_dl
from youtube_dl.utils import DownloadError
class Namespace(object): pass class Namespace(object): pass
def get_youtube_metadata(url, ydl = youtube_dl.YoutubeDL()): def filter_query_params(url, allowed=[]):
#todo: check if url is valid split_url = urlsplit(url)
#todo, stop it from doung the whole playlist qs = parse_qs(split_url.query)
resp = ydl.extract_info(url, download=False) print(qs)
for key in list(qs.keys()):
if key not in allowed:
del qs[key]
return urlunsplit((
split_url.scheme,
split_url.netloc,
split_url.path,
urlencode(qs, doseq=True),
split_url.fragment,
))
def get_youtube_metadata(url, ydl = youtube_dl.YoutubeDL()):
if urlsplit(url).netloc.lower() in ("www.youtube.com", "youtube.com", "youtub.be"):
#Stop it from doing the whole playlist
url = filter_query_params(url, allowed=["v"])
print(url)
try:
resp = ydl.extract_info(url, download=False)
except DownloadError:
return None
#print resp.keys() #print resp.keys()
title = resp.get('title') title = resp.get('title')
length = resp.get('duration') length = resp.get('duration')
#print( title, "%i:%.2i" % (length//60, length%60)) #print( title, "%i:%.2i" % (length//60, length%60))
return title, "%i:%.2i" % (length//60, length%60) return {"title":title, "length":seconds_to_timestamp(length)}
def seconds_to_timestamp(s):
return "%i:%.2i" % (s//60, s%60)
# decorator: # decorator:
def call_as_thread(func): # This will discard any return value! def call_as_thread(func): # This will discard any return value!