from flask import Flask, request, jsonify, Response, render_template_string
|
|
import libsonic
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configuración de la API de Subsonic
|
|
SUBSONIC_URL = 'https://navidrome.reymota.es/rest'
|
|
USERNAME = 'creylopez'
|
|
PASSWORD = 'Rey-1176'
|
|
CLIENT_NAME = 'NavidromePy' # Nombre de la aplicación cliente
|
|
VERSION = '1.16.1' # Versión de la API de Subsonic
|
|
|
|
client = libsonic.Connection(SUBSONIC_URL, USERNAME, PASSWORD, 443)
|
|
|
|
def get_song_stream(song_id):
|
|
return client.stream(song_id)
|
|
|
|
def search_song(title):
|
|
result = client.search(title)
|
|
songs = result['song']
|
|
# Filtrar canciones que coincidan exactamente con el título buscado
|
|
for song in songs:
|
|
if song['title'].lower() == title.lower():
|
|
return song['id']
|
|
return None
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template_string('''
|
|
<!doctype html>
|
|
<title>Buscar y reproducir canción</title>
|
|
<h1>Buscar y reproducir canción</h1>
|
|
<form action="/play" method="post">
|
|
<label for="title">Título de la canción:</label>
|
|
<input type="text" id="title" name="title">
|
|
<input type="submit" value="Reproducir">
|
|
</form>
|
|
''')
|
|
|
|
@app.route('/play', methods=['POST'])
|
|
def play_song():
|
|
title = request.form.get('title')
|
|
if not title:
|
|
return jsonify({'error': 'Se requiere el título de la canción'}), 400
|
|
|
|
song_id = search_song(title)
|
|
if not song_id:
|
|
return jsonify({'error': 'Canción no encontrada'}), 404
|
|
|
|
response = get_song_stream(song_id)
|
|
return Response(response, content_type='audio/mpeg')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|