|
|
|
@ -1,55 +1,46 @@ |
|
|
|
from flask import Flask, request, jsonify, Response |
|
|
|
import requests |
|
|
|
from flask import Flask, request, jsonify, Response, render_template_string |
|
|
|
import libsonic |
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
# Configuración de la API de Subsonic |
|
|
|
SUBSONIC_URL = 'http://navidrome.reymota.es/rest' |
|
|
|
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): |
|
|
|
url = f"{SUBSONIC_URL}/stream" |
|
|
|
params = { |
|
|
|
'u': USERNAME, |
|
|
|
'p': PASSWORD, |
|
|
|
'v': VERSION, |
|
|
|
'c': CLIENT_NAME, |
|
|
|
'id': song_id, |
|
|
|
'f': 'json' |
|
|
|
} |
|
|
|
response = requests.get(url, params=params, stream=True) |
|
|
|
return response |
|
|
|
return client.stream(song_id) |
|
|
|
|
|
|
|
def search_song(title): |
|
|
|
url = f"{SUBSONIC_URL}/search3" |
|
|
|
params = { |
|
|
|
'u': USERNAME, |
|
|
|
'p': PASSWORD, |
|
|
|
'v': VERSION, |
|
|
|
'c': CLIENT_NAME, |
|
|
|
'query': title, |
|
|
|
# 'songCount': 1, |
|
|
|
'f': 'json' |
|
|
|
} |
|
|
|
response = requests.get(url, params=params) |
|
|
|
if response.status_code == 200: |
|
|
|
result = response.json() |
|
|
|
print(result) |
|
|
|
songs = result['subsonic-response']['searchResult3']['song'] |
|
|
|
if songs: |
|
|
|
for cancion in songs: |
|
|
|
print("Canción buscada: '",title,"' - encontrada: '", cancion['title'],"'") |
|
|
|
if cancion['title'] == title: |
|
|
|
return cancion['id'] |
|
|
|
|
|
|
|
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('/play', methods=['GET']) |
|
|
|
@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.args.get('title') |
|
|
|
title = request.form.get('title') |
|
|
|
if not title: |
|
|
|
return jsonify({'error': 'Se requiere el título de la canción'}), 400 |
|
|
|
|
|
|
|
@ -58,10 +49,7 @@ def play_song(): |
|
|
|
return jsonify({'error': 'Canción no encontrada'}), 404 |
|
|
|
|
|
|
|
response = get_song_stream(song_id) |
|
|
|
if response.status_code == 200: |
|
|
|
return Response(response.iter_content(chunk_size=1024), content_type=response.headers['Content-Type']) |
|
|
|
else: |
|
|
|
return jsonify({'error': 'Error al reproducir la canción'}), response.status_code |
|
|
|
return Response(response, content_type='audio/mpeg') |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(debug=True) |
|
|
|
app.run(debug=True) |