Skip to content

Commit

Permalink
display folder content in webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
Groovylein committed Jan 31, 2024
1 parent e5ae214 commit 3edfa67
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/jukebox/components/player/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,12 @@ def get_song_by_url(self, song_url):
pass

@abstractmethod
def get_folder_content(self):
def get_folder_content(self, folder):
"""
Get the folder content as content list with meta-information. Depth is always 1.
Call repeatedly to descend in hierarchy
:param folder: Folder path relative to music library path
"""
pass
57 changes: 55 additions & 2 deletions src/jukebox/components/player/backends/mpd/interfacing_mpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import logging
import os.path
import re
from typing import Optional

import jukebox.plugs as plugin
import jukebox.cfghandler
import jukebox.playlistgenerator as playlistgenerator

from mpd.asyncio import MPDClient
from components.player.backends import BackendPlayer


logger = logging.getLogger('jb.mpd')
cfg = jukebox.cfghandler.get_handler('player')

Expand Down Expand Up @@ -294,8 +297,11 @@ def list_dirs(self):
def get_song_by_url(self, song_url):
pass

def get_folder_content(self):
pass
def get_folder_content(self, folder):
logger.debug(f"get_folder_content param: {folder}")
plc = playlistgenerator.PlaylistCollector(get_music_library_path())
plc.get_directory_content(folder)
return plc.playlist

# ----------------------------------
# Get podcasts / livestreams
Expand Down Expand Up @@ -344,3 +350,50 @@ def _restore_state(self):
Restore the configuration state and last played status for current active URI
"""
pass


#ToDo: refactor code
def _get_music_library_path(conf_file):
"""Parse the music directory from the mpd.conf file"""
pattern = re.compile(r'^\s*music_directory\s*"(.*)"', re.I)
directory = None
with open(conf_file, 'r') as f:
for line in f:
res = pattern.match(line)
if res:
directory = res.group(1)
break
else:
logger.error(f"Could not find music library path in {conf_file}")
logger.debug(f"MPD music lib path = {directory}; from {conf_file}")
return directory


class MusicLibPath:
"""Extract the music directory from the mpd.conf file"""
def __init__(self):
self._music_library_path = None
mpd_conf_file = cfg.setndefault('playermpd', 'mpd_conf', value='~/.config/mpd/mpd.conf')
try:
self._music_library_path = _get_music_library_path(os.path.expanduser(mpd_conf_file))
except Exception as e:
logger.error(f"Could not determine music library directory from '{mpd_conf_file}'")
logger.error(f"Reason: {e.__class__.__name__}: {e}")

@property
def music_library_path(self):
return self._music_library_path


# ---------------------------------------------------------------------------


_MUSIC_LIBRARY_PATH: Optional[MusicLibPath] = None


def get_music_library_path():
"""Get the music library path"""
global _MUSIC_LIBRARY_PATH
if _MUSIC_LIBRARY_PATH is None:
_MUSIC_LIBRARY_PATH = MusicLibPath()
return _MUSIC_LIBRARY_PATH.music_library_path
4 changes: 2 additions & 2 deletions src/jukebox/components/player/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def get_song_by_url(self, song_url):
return self._active.get_song_by_url(song_url)

@plugin.tag
def get_folder_content(self):
return self._active.get_folder_content()
def get_folder_content(self, folder):
return self._active.get_folder_content(folder)


def _save_state(self):
Expand Down
5 changes: 3 additions & 2 deletions src/jukebox/jukebox/playlistgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def get_directory_content(self, path='.'):
"""
self.playlist = []
self._folder = os.path.abspath(os.path.join(self._music_library_base_path, path))
logger.debug(self._folder)
try:
content = self._get_directory_content(self._folder)
except NotADirectoryError as e:
Expand All @@ -278,10 +279,10 @@ def get_directory_content(self, path='.'):
else:
logger.debug(f"Playlist Content: {content}")
for m in content:
self.playlist.append({'type': TYPE_DECODE[m.filetype], 'name': m.name, 'path': m.path, 'uri': m.uri})
self.playlist.append({'type': TYPE_DECODE[m.filetype], 'name': m.name, 'path': m.uri})

def _parse_nonrecusive(self, path='.'):
return [x.path for x in self._get_directory_content(path) if x.filetype != TYPE_DIR]
return [x.uri for x in self._get_directory_content(path) if x.filetype != TYPE_DIR]

def _parse_recursive(self, path='.'):
# This can certainly be optimized, as os.walk is called on all
Expand Down

0 comments on commit 3edfa67

Please sign in to comment.