mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 04:23:01 +00:00
break down the api blueprint into smaller blueprints
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import os
|
||||
from flask import Blueprint
|
||||
|
||||
from app import api
|
||||
|
||||
folder_bp = Blueprint("folder", __name__, url_prefix="/")
|
||||
from app import helpers
|
||||
|
||||
|
||||
@folder_bp.route("/f/<folder>")
|
||||
# @cache.cached()
|
||||
def get_folder_tree(folder: str):
|
||||
"""
|
||||
Returns a list of all the folders and tracks in the given folder.
|
||||
"""
|
||||
req_dir = folder.replace("|", "/")
|
||||
|
||||
if folder == "home":
|
||||
req_dir = helpers.home_dir
|
||||
|
||||
dir_content = os.scandir(os.path.join(helpers.home_dir, req_dir))
|
||||
|
||||
folders = []
|
||||
files = []
|
||||
|
||||
for entry in dir_content:
|
||||
if entry.is_dir() and not entry.name.startswith("."):
|
||||
files_in_dir = helpers.run_fast_scandir(entry.path, [".flac", ".mp3"])[1]
|
||||
|
||||
if len(files_in_dir) != 0:
|
||||
_dir = {
|
||||
"name": entry.name,
|
||||
"count": len(files_in_dir),
|
||||
"path": entry.path.replace(helpers.home_dir, ""),
|
||||
}
|
||||
|
||||
folders.append(_dir)
|
||||
|
||||
if entry.is_file():
|
||||
if entry.name.endswith(".flac") or entry.name.endswith(".mp3"):
|
||||
files.append(entry)
|
||||
|
||||
files.sort(key=lambda x: os.path.getmtime(x.path))
|
||||
|
||||
songs = []
|
||||
|
||||
for entry in files:
|
||||
for track in api.TRACKS:
|
||||
if track.filepath == entry.path:
|
||||
songs.append(track)
|
||||
|
||||
return {
|
||||
"files": helpers.remove_duplicates(songs),
|
||||
"folders": sorted(folders, key=lambda i: i["name"]),
|
||||
}
|
||||
Reference in New Issue
Block a user