-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (101 loc) · 3.01 KB
/
Copy pathapp.py
File metadata and controls
139 lines (101 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os.path
import glob
import heapq
from flask import Flask, jsonify, send_file
from flask import render_template, abort, request
from flask_cors import CORS
from werkzeug.utils import secure_filename
from werkzeug.exceptions import HTTPException
from mylib.metaflac import MetaFlac
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config["UPLOAD_FOLDER"] = "song_folder"
app.config.from_object(__name__)
CORS(app)
ALLOWED_EXTENSIONS = {'flac'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def sort_function(filename):
return os.path.getctime(filename)
@app.errorhandler(405)
def return_not_found(error):
return jsonify(
{
"status": 405,
"error_msg": "method not allow"
}
), 405
@app.route("/jukebox")
def run_jukebox():
return render_template("test.html")
@app.route("/getsong")
def get_songs():
return jsonify(
{
"song_lists": [
"time.flac",
"one_last_kiss.flac"
]
}
)
@app.route("/")
def get_hello():
return "Hello world"
@app.route("/my_song_folder", methods=["GET", "POST"])
@app.route("/my_song_folder/<song_name>")
def get_song_file(song_name=None):
if song_name:
return send_file(
f"song_folder/{song_name}.flac", mimetype="audio/flac"
)
if request.method == "POST":
upload_file = request.files["file"]
if upload_file and allowed_file(upload_file.filename):
filename = secure_filename(upload_file.filename)
filename_with_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if not os.path.exists(filename_with_path):
upload_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify(
{
"status": 200,
"message": "Upload Successful"
}
)
return jsonify(
{
"status": 514,
"message": "Duplicate Filename"
}
)
return jsonify(
{
"status": 513,
"message": "Incorrect Filetype"
}
)
return jsonify(
{
"song_list": [
os.path.basename(my_file)
for my_file in sorted(
[
file_name
for file_name in glob.glob("song_folder/*.flac")
], key=lambda x: os.path.getctime(x)
)
]
}
)
@app.route("/metadata/<song_title>")
def get_song_metadata(song_title):
return jsonify(
MetaFlac(f"song_folder/{song_title}.flac").get_vorbis_comment()
)
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=8088,
debug=True,
threaded=True
)