Initial commit: Flask internet radio player
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
import random
|
||||
from flask import Flask, render_template, send_file, jsonify, abort, Response
|
||||
from pathlib import Path
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
AUDIO_DIR = Path(__file__).parent / "audio"
|
||||
SUPPORTED_EXTENSIONS = {".mp3", ".ogg", ".wav", ".flac", ".aac", ".m4a"}
|
||||
|
||||
|
||||
def get_playlist():
|
||||
files = sorted(
|
||||
f.name for f in AUDIO_DIR.iterdir()
|
||||
if f.is_file() and f.suffix.lower() in SUPPORTED_EXTENSIONS
|
||||
)
|
||||
return files
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/api/playlist")
|
||||
def playlist():
|
||||
return jsonify(get_playlist())
|
||||
|
||||
|
||||
@app.route("/audio/<path:filename>")
|
||||
def serve_audio(filename):
|
||||
file_path = AUDIO_DIR / filename
|
||||
if not file_path.is_file() or file_path.suffix.lower() not in SUPPORTED_EXTENSIONS:
|
||||
abort(404)
|
||||
# Prevent path traversal
|
||||
if not file_path.resolve().is_relative_to(AUDIO_DIR.resolve()):
|
||||
abort(403)
|
||||
return send_file(file_path, conditional=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not AUDIO_DIR.exists():
|
||||
AUDIO_DIR.mkdir()
|
||||
print(f"Audio files directory: {AUDIO_DIR.absolute()}")
|
||||
print(f"Found {len(get_playlist())} audio file(s)")
|
||||
app.run(debug=True, host="0.0.0.0", port=5001)
|
||||
Reference in New Issue
Block a user