Skip to content

Commit

Permalink
✨ (api) getnext track api
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Jun 3, 2020
1 parent fbee74c commit 4408830
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
31 changes: 30 additions & 1 deletion klangbecken.py
Expand Up @@ -235,6 +235,25 @@ def update_data_analyzer(playlist, fileId, ext, data):
]


def next_prio_track_analyzer(data_dir):
data = {}
with open(os.path.join(data_dir, 'prio.m3u'), 'r') as f:
contents = f.read().strip()
if contents:
_, ext = os.path.splitext(contents)
changes = mutagen_tag_analyzer(
'prio.m3u',
None,
ext,
open(os.path.join(data_dir, contents), 'rb')
)
for change in changes:
if isinstance(change, MetadataChange):
data[change.key] = change.value
data['next'] = True
return data


##############
# Processors #
##############
Expand Down Expand Up @@ -464,12 +483,14 @@ def __init__(self,
secret,
upload_analyzers=DEFAULT_UPLOAD_ANALYZERS,
update_analyzers=DEFAULT_UPDATE_ANALYZERS,
next_prio_track_info=next_prio_track_analyzer,
processors=DEFAULT_PROCESSORS,
disable_auth=False):
self.data_dir = data_dir
self.secret = secret
self.upload_analyzers = upload_analyzers
self.update_analyzers = update_analyzers
self.next_prio_track_info = next_prio_track_info
self.processors = processors
self.do_auth = not disable_auth

Expand All @@ -484,7 +505,8 @@ def __init__(self,
Rule(playlist_url, methods=('POST',), endpoint='upload'),
Rule(file_url, methods=('PUT',), endpoint='update'),
Rule(file_url, methods=('DELETE',), endpoint='delete'),
Rule('/playnext/', methods=('POST',), endpoint='play_next')
Rule('/playnext/', methods=('POST',), endpoint='play_next'),
Rule('/playnext/', methods=('GET',), endpoint='get_next')
))

def __call__(self, environ, start_response):
Expand Down Expand Up @@ -653,6 +675,13 @@ def on_play_next(self, request):

return JSONResponse({'status': 'OK'})

def on_get_next(self, request):
resp = {'status': 'OK', 'next': False}

resp.update(self.next_prio_track_info(self.data_dir))

return JSONResponse(resp)


class JSONResponse(Response):
"""
Expand Down
Empty file added tests/playlists/empty/prio.m3u
Empty file.
1 change: 1 addition & 0 deletions tests/playlists/single-track/prio.m3u
@@ -0,0 +1 @@
../../audio/silence.mp3
26 changes: 26 additions & 0 deletions tests/test_api.py
Expand Up @@ -31,11 +31,14 @@ def setUp(self):
self.update_analyzer = mock.Mock(return_value=['UpdateChange'])
self.processor = mock.MagicMock()

self.next_prio_track_analyzer = mock.Mock(return_value=[])

app = KlangbeckenAPI(
'data_dir',
'secret',
upload_analyzers=[self.upload_analyzer],
update_analyzers=[self.update_analyzer],
next_prio_track_info=self.next_prio_track_analyzer,
processors=[self.processor],
disable_auth=True,
)
Expand Down Expand Up @@ -249,6 +252,11 @@ def testPlaynext(self, playnext_processor):
self.assertTrue(b'invalid UTF-8 data' in resp.data)
playnext_processor.assert_not_called()

def testGetNext(self):
resp = self.client.get('/playnext/')
self.assertEqual(resp.status_code, 200)
self.next_prio_track_analyzer.assert_called_with('data_dir')


class AuthTestCase(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -503,6 +511,24 @@ def testFFmpegAudioAnalyzer(self):
fs = FileStorage(f)
ffmpeg_audio_analyzer('music', 'id1', '.mp3', fs)

def testNextPrioTrackAnalyzer(self):
from klangbecken import next_prio_track_analyzer

# test empty file
path = os.path.join(self.current_path, 'playlists', 'empty')

data = next_prio_track_analyzer(path)

self.assertEquals(data, {})

# test file with track
path = os.path.join(self.current_path, 'playlists', 'single-track')

data = next_prio_track_analyzer(path)

self.assertEquals(data['artist'], 'Silence Artist')
self.assertEquals(data['title'], 'Silence Track')


class ProcessorsTestCase(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 4408830

Please sign in to comment.