~hristoast/mousikofidi

9562dad2fac3629548d5c9341ee6bb788f3f0e99 — Daniil Baturin 1 year, 2 days ago f5b5ae9
Refactor the file extension checks to be more extensible and consice.

This also enables making them configurable in the future.
1 files changed, 23 insertions(+), 14 deletions(-)

M mousikofidi/mousikofidi.py
M mousikofidi/mousikofidi.py => mousikofidi/mousikofidi.py +23 -14
@@ 60,6 60,10 @@ THEMES = {
    "terminal-solarized": "/terminal-solarized",
}

AUDIO_FILE_EXTENSIONS = [".mp3", ".ogg", ".flac"]

VIDEO_FILE_EXTENSIONS = [".mp4", ".webm"]

try:
    local_config_file = os.path.join(
        os.path.abspath(os.path.dirname(os.getenv("FLASK_APP"))), "fidi.yml"


@@ 73,20 77,31 @@ err = app.logger.error
wrn = app.logger.warning


def has_extension(filename: str, extensions) -> bool:
    # Support a single extension
    if type(extensions) != list:
        extensions = [extensions]

    _, extension = os.path.splitext(filename)
    extension = extension.lower()

    return extension in extensions


def quote(string: str) -> str:
    return urllib.parse.quote(string, safe="")


def file_metadata(file_path: str) -> dict:
    ft = None
    if file_path.lower().endswith(".flac"):
    if has_extension(file_path, ".flac"):
        try:
            data_string = FLAC(file_path).pprint()
            ft = "flac"
        except FLACNoHeaderError:
            wrn("This file is not a valid flac file: " + file_path)
            return {}
    elif file_path.lower().endswith(".mp3"):
    elif has_extension(file_path, ".mp3"):
        try:
            data_string = MP3(file_path).pprint()
            ft = "mp3"


@@ 94,7 109,7 @@ def file_metadata(file_path: str) -> dict:
            wrn("This file is not a valid mp3 file: " + file_path)
            return {}

    elif file_path.lower().endswith(".ogg"):
    elif has_extension(file_path, ".ogg"):
        try:
            data_string = OggVorbis(file_path).tags.pprint()
            ft = "ogg"


@@ 102,7 117,7 @@ def file_metadata(file_path: str) -> dict:
            wrn("This file is not a valid ogg vorbis file: " + file_path)
            return {}

    elif file_path.lower().endswith(".mp4"):
    elif has_extension(file_path, ".mp4"):
        try:
            data_string = MP4(file_path).pprint().strip("\xa9").rstrip("\xa9")
            ft = "mp4"


@@ 934,17 949,11 @@ this ticket</a> for more information about how this will be fixed.</p>"""


def is_audio_file(file_path: str) -> bool:
    return os.path.isfile(file_path) and (
        file_path.endswith(".flac")
        or file_path.endswith(".mp3")
        or file_path.endswith(".ogg")
    )
    return os.path.isfile(file_path) and has_extension(file_path, AUDIO_FILE_EXTENSIONS)


def is_video_file(file_path: str) -> bool:
    return os.path.isfile(file_path) and (
        file_path.endswith(".mp4") or file_path.endswith(".webm")
    )
    return os.path.isfile(file_path) and has_extension(file_path, VIDEO_FILE_EXTENSIONS)


def is_valid_path(request_context: dict, path: str) -> bool:


@@ 1000,8 1009,8 @@ def paths_list(music_dirs: list) -> list:
def search_files(q: str, only_audio: bool, only_video: bool) -> list:
    hits = []
    mdirs = app.fidiConfig["config"]["music_dirs"]
    audio_exts = ("flac", "mp3", "ogg")
    video_exts = ("mp4", "webm")
    audio_exts = AUDIO_FILE_EXTENSIONS
    video_exts = VIDEO_FILE_EXTENSIONS
    _exts = audio_exts + video_exts
    for topdir in mdirs:
        for dirpath, dirnames, files in os.walk(topdir):