import requests
import time
import os

# Reemplaza con tu clave API de Last.fm
API_KEY = 'c21b81ac4e50adcd49d36caad2c4bcf2'
USER = 'terafl0p5'

def get_now_playing(api_key, user):
    url = 'http://ws.audioscrobbler.com/2.0/'
    params = {
        'method': 'user.getrecenttracks',
        'user': user,
        'api_key': api_key,
        'format': 'json',
        'limit': 1
    }
    
    response = requests.get(url, params=params)
    data = response.json()
    
    if 'recenttracks' in data and 'track' in data['recenttracks']:
        tracks = data['recenttracks']['track']
        if len(tracks) > 0:
            now_playing = tracks[0]
            # Verifica si la pista está siendo reproducida actualmente
            if '@attr' in now_playing and 'nowplaying' in now_playing['@attr']:
                artist = now_playing['artist']['#text']
                track = now_playing['name']
                return f"Now Playing: {artist} - {track}"
            else:
                return "No track is currently being played."
        else:
            return "No recent tracks found."
    else:
        return "Error fetching data from Last.fm."

def display_scrolling_text(text):
    # Agrega espacios al final del texto para dar un respiro entre repeticiones
    text += ' ' * 20
    while True:
        # Limpia la pantalla para actualizar la posición del texto
        os.system('cls' if os.name == 'nt' else 'clear')
        print(text)
        # Mueve la primera letra al final para el efecto de desplazamiento
        text = text[1:] + text[0]
        time.sleep(0.2)  # Ajusta la velocidad del desplazamiento

# Llama a la función para obtener la pista actual
now_playing_text = get_now_playing(API_KEY, USER)

# Muestra el texto con efecto de desplazamiento
display_scrolling_text(now_playing_text)

