From e8ae74cebd26f9aed709a3e82b37bd29345ac03d Mon Sep 17 00:00:00 2001 From: Tancredi Orlando Date: Sat, 24 Jun 2017 15:35:19 +0200 Subject: [PATCH] Add AutoDJ feature. First issue #14 implementation. Updating to this commit requires human interaction: you must add a boolean AutoDJ key to the [GENERAL] config section. --- airhead/playlist.py | 19 ++++++++++++++++--- conf/airhead.ini.example | 1 + web.py | 6 ++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/airhead/playlist.py b/airhead/playlist.py index 42ed348..d629ccb 100644 --- a/airhead/playlist.py +++ b/airhead/playlist.py @@ -1,4 +1,5 @@ import queue +import random from airhead.library import TrackNotFoundError @@ -10,10 +11,17 @@ class EmptyPlaylist(Exception): pass +def random_track(library): + track = random.choice(library.query()) + return track['uuid'] + + class Playlist: - def __init__(self, library, notify=lambda: None): + def __init__(self, library, notify=lambda: None, auto_dj=False): self._library = library self._notify = notify + self._auto_dj = auto_dj + self._current = None self._queue = queue.Queue() @@ -34,8 +42,13 @@ def pop(self): item = self._queue.get(block=False) except queue.Empty as e: - self._current = None - raise EmptyPlaylist from e + if self._auto_dj: + track = random_track(self._library) + self._current = track + return track + else: + self._current = None + raise EmptyPlaylist from e else: self._current = item diff --git a/conf/airhead.ini.example b/conf/airhead.ini.example index ce545a8..6d5de71 100644 --- a/conf/airhead.ini.example +++ b/conf/airhead.ini.example @@ -3,6 +3,7 @@ Debug = True Address = 127.0.0.1 Port = 8080 Library = /tmp/airhead/library +AutoDJ = False Frontend = /tmp/airhead/frontend [INFO] diff --git a/web.py b/web.py index 16eb61f..ecc3c5a 100644 --- a/web.py +++ b/web.py @@ -174,11 +174,13 @@ def broadcaster_shutdown(app): app = web.Application() app['config'] = get_config() +app['websockets'] = list() + app['library'] = Library(app['config'].get('GENERAL', 'Library'), notify=broadcast_library_update) -app['playlist'] = Playlist(app['library'], notify=broadcast_playlist_update) +app['playlist'] = Playlist(app['library'], notify=broadcast_playlist_update, + auto_dj=app['config'].getboolean('GENERAL', 'AutoDj')) app['broadcaster'] = Broadcaster(app['config']['ICECAST'], app['playlist']) -app['websockets'] = list() app.router.add_route('GET', '/api/info', info) app.router.add_route('GET', '/api/library', library_query)