Skip to content

Commit

Permalink
Add AutoDJ feature. First issue #14 implementation.
Browse files Browse the repository at this point in the history
Updating to this commit requires human interaction: you must add a
boolean AutoDJ key to the [GENERAL] config section.
  • Loading branch information
turlando committed Jun 24, 2017
1 parent 8f0a98a commit e8ae74c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
19 changes: 16 additions & 3 deletions airhead/playlist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import queue
import random
from airhead.library import TrackNotFoundError


Expand All @@ -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()

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions conf/airhead.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Debug = True
Address = 127.0.0.1
Port = 8080
Library = /tmp/airhead/library
AutoDJ = False
Frontend = /tmp/airhead/frontend

[INFO]
Expand Down
6 changes: 4 additions & 2 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e8ae74c

Please sign in to comment.