from flask import Flask, request, jsonify, Response
|
|
import requests
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configuración de la API de Subsonic
|
|
SUBSONIC_URL = 'http://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
|
|
|
|
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
|
|
|
|
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']
|
|
|
|
return None
|
|
|
|
@app.route('/play', methods=['GET'])
|
|
def play_song():
|
|
title = request.args.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)
|
|
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
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|