Update app.py
Browse files
app.py
CHANGED
|
@@ -6,49 +6,38 @@ app = Flask(__name__)
|
|
| 6 |
app.config['SECRET_KEY'] = 'server_direct_save_2026'
|
| 7 |
socketio = SocketIO(app, cors_allowed_origins="*")
|
| 8 |
|
| 9 |
-
# Create a permanent folder on the server for the music
|
| 10 |
MUSIC_FOLDER = 'saved_music'
|
| 11 |
if not os.path.exists(MUSIC_FOLDER):
|
| 12 |
os.makedirs(MUSIC_FOLDER)
|
| 13 |
|
| 14 |
-
# Map to link taskIds to User Session IDs (for the callback)
|
| 15 |
task_to_sid = {}
|
| 16 |
|
| 17 |
@app.route('/')
|
| 18 |
def index():
|
| 19 |
return render_template('index.html')
|
| 20 |
|
| 21 |
-
# 1. THE CALLBACK (Saves the file to server storage)
|
| 22 |
@app.route('/callback', methods=['POST'])
|
| 23 |
def callback():
|
| 24 |
data = request.json
|
|
|
|
|
|
|
|
|
|
| 25 |
task_id = data.get('taskId') or data.get('task_id')
|
| 26 |
audio_url = data.get('audio_url')
|
| 27 |
|
| 28 |
if audio_url and task_id:
|
| 29 |
-
# Download the file to the server immediately
|
| 30 |
filename = f"{task_id}.mp3"
|
| 31 |
filepath = os.path.join(MUSIC_FOLDER, filename)
|
| 32 |
-
|
| 33 |
try:
|
| 34 |
r = requests.get(audio_url)
|
| 35 |
with open(filepath, 'wb') as f:
|
| 36 |
f.write(r.content)
|
| 37 |
-
|
| 38 |
-
# Find the user who owns this task and tell them
|
| 39 |
if task_id in task_to_sid:
|
| 40 |
-
|
| 41 |
-
socketio.emit('status', {
|
| 42 |
-
'msg': 'β
Saved to Server!',
|
| 43 |
-
'file_name': filename,
|
| 44 |
-
'done': True
|
| 45 |
-
}, room=user_sid)
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"Callback
|
| 48 |
-
|
| 49 |
return jsonify({"status": "ok"}), 200
|
| 50 |
|
| 51 |
-
# 2. THE DIRECT DOWNLOAD (Serves the file from your server)
|
| 52 |
@app.route('/get_music/<filename>')
|
| 53 |
def get_music(filename):
|
| 54 |
return send_from_directory(MUSIC_FOLDER, filename, as_attachment=True)
|
|
@@ -61,21 +50,30 @@ def save_token(data):
|
|
| 61 |
@socketio.on('start_gen')
|
| 62 |
def handle_gen(payload):
|
| 63 |
token = session.get('api_token')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
| 65 |
-
|
| 66 |
-
# Set the callback URL dynamically
|
| 67 |
payload['callBackUrl'] = request.host_url + "callback"
|
| 68 |
|
| 69 |
try:
|
| 70 |
res = requests.post("https://api.sunoapi.org/api/v1/generate/upload-cover", json=payload, headers=headers)
|
| 71 |
-
|
| 72 |
-
task_id = res_data.get('data', {}).get('taskId')
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
-
emit('status', {'msg': f'β Error: {str(e)}'})
|
| 79 |
|
| 80 |
if __name__ == '__main__':
|
| 81 |
-
socketio.run(app, host="0.0.0.0")
|
|
|
|
| 6 |
app.config['SECRET_KEY'] = 'server_direct_save_2026'
|
| 7 |
socketio = SocketIO(app, cors_allowed_origins="*")
|
| 8 |
|
|
|
|
| 9 |
MUSIC_FOLDER = 'saved_music'
|
| 10 |
if not os.path.exists(MUSIC_FOLDER):
|
| 11 |
os.makedirs(MUSIC_FOLDER)
|
| 12 |
|
|
|
|
| 13 |
task_to_sid = {}
|
| 14 |
|
| 15 |
@app.route('/')
|
| 16 |
def index():
|
| 17 |
return render_template('index.html')
|
| 18 |
|
|
|
|
| 19 |
@app.route('/callback', methods=['POST'])
|
| 20 |
def callback():
|
| 21 |
data = request.json
|
| 22 |
+
if not data: return jsonify({"status": "no data"}), 400
|
| 23 |
+
|
| 24 |
+
# Extract taskId and audio_url carefully
|
| 25 |
task_id = data.get('taskId') or data.get('task_id')
|
| 26 |
audio_url = data.get('audio_url')
|
| 27 |
|
| 28 |
if audio_url and task_id:
|
|
|
|
| 29 |
filename = f"{task_id}.mp3"
|
| 30 |
filepath = os.path.join(MUSIC_FOLDER, filename)
|
|
|
|
| 31 |
try:
|
| 32 |
r = requests.get(audio_url)
|
| 33 |
with open(filepath, 'wb') as f:
|
| 34 |
f.write(r.content)
|
|
|
|
|
|
|
| 35 |
if task_id in task_to_sid:
|
| 36 |
+
socketio.emit('status', {'msg': 'β
Saved to Server!', 'file_name': filename, 'done': True}, room=task_to_sid[task_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"Callback error: {e}")
|
|
|
|
| 39 |
return jsonify({"status": "ok"}), 200
|
| 40 |
|
|
|
|
| 41 |
@app.route('/get_music/<filename>')
|
| 42 |
def get_music(filename):
|
| 43 |
return send_from_directory(MUSIC_FOLDER, filename, as_attachment=True)
|
|
|
|
| 50 |
@socketio.on('start_gen')
|
| 51 |
def handle_gen(payload):
|
| 52 |
token = session.get('api_token')
|
| 53 |
+
if not token:
|
| 54 |
+
emit('status', {'msg': 'β Error: Set Token first.'})
|
| 55 |
+
return
|
| 56 |
+
|
| 57 |
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
|
|
|
|
|
|
| 58 |
payload['callBackUrl'] = request.host_url + "callback"
|
| 59 |
|
| 60 |
try:
|
| 61 |
res = requests.post("https://api.sunoapi.org/api/v1/generate/upload-cover", json=payload, headers=headers)
|
| 62 |
+
res_json = res.json()
|
|
|
|
| 63 |
|
| 64 |
+
# SAFETY CHECK: Verify 'data' exists before calling .get()
|
| 65 |
+
api_data = res_json.get('data')
|
| 66 |
+
if api_data:
|
| 67 |
+
task_id = api_data.get('taskId')
|
| 68 |
+
task_to_sid[task_id] = request.sid
|
| 69 |
+
emit('status', {'msg': f'π Task {task_id} started...'})
|
| 70 |
+
else:
|
| 71 |
+
# If 'data' is missing, show the actual error from Suno
|
| 72 |
+
error_msg = res_json.get('msg') or "Unknown API Error"
|
| 73 |
+
emit('status', {'msg': f'β Suno API says: {error_msg}'})
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
+
emit('status', {'msg': f'β Script Error: {str(e)}'})
|
| 77 |
|
| 78 |
if __name__ == '__main__':
|
| 79 |
+
socketio.run(app, host="0.0.0.0", port=5000)
|