spotify-callback-server.py•4.54 kB
#!/usr/bin/env python3
import http.server
import socketserver
import urllib.parse
import sys
class SpotifyCallbackHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith('/callback'):
# Parse the URL to get the code
parsed = urllib.parse.urlparse(self.path)
params = urllib.parse.parse_qs(parsed.query)
if 'code' in params:
code = params['code'][0]
# Send HTML response with the code
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
html = f'''
<!DOCTYPE html>
<html>
<head>
<title>Spotify Auth Success</title>
<style>
body {{ font-family: Arial, sans-serif; text-align: center; padding: 50px; background: #191414; color: white; }}
.container {{ max-width: 600px; margin: 0 auto; }}
.success {{ color: #1db954; font-size: 24px; margin-bottom: 20px; }}
.code {{ background: #282828; padding: 15px; border-radius: 8px; font-family: monospace; word-break: break-all; border: 1px solid #404040; }}
.copy-btn {{ background: #1db954; color: white; border: none; padding: 12px 24px; border-radius: 8px; cursor: pointer; margin-top: 15px; font-size: 16px; }}
.copy-btn:hover {{ background: #1ed760; }}
.instructions {{ margin: 20px 0; color: #b3b3b3; }}
</style>
</head>
<body>
<div class="container">
<div class="success">✅ Autenticação Spotify Concluída!</div>
<div class="instructions">Copie o código abaixo e cole no terminal:</div>
<div class="code" id="code">{code}</div>
<button class="copy-btn" onclick="copyCode()">Copiar Código</button>
<div class="instructions">O código foi copiado automaticamente para a área de transferência!</div>
</div>
<script>
function copyCode() {{
const code = '{code}';
navigator.clipboard.writeText(code).then(() => {{
document.querySelector('.copy-btn').innerHTML = '✅ Código Copiado!';
setTimeout(() => {{
document.querySelector('.copy-btn').innerHTML = 'Copiar Código';
}}, 2000);
}});
}}
// Auto-copy to clipboard
window.onload = function() {{
copyCode();
}};
</script>
</body>
</html>
'''
self.wfile.write(html.encode('utf-8'))
print(f'\\n🎵 CÓDIGO SPOTIFY RECEBIDO: {code}\\n')
print('📋 Copie este código e cole no terminal para finalizar a autenticação!')
return
else:
self.send_response(400)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'No code found in callback')
else:
self.send_response(404)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'Not Found')
def log_message(self, format, *args):
pass # Suppress default logging
if __name__ == "__main__":
PORT = 8080
print(f'🎵 Servidor de callback Spotify iniciado na porta {PORT}')
print(f'📡 Aguardando callback do Spotify...')
print(f'🌐 URL: https://chasity-inert-tabularly.ngrok-free.dev/callback')
print('\\n🔗 Clique no link de autenticação para fazer login no Spotify!')
try:
with socketserver.TCPServer(('', PORT), SpotifyCallbackHandler) as httpd:
httpd.serve_forever()
except KeyboardInterrupt:
print('\\n👋 Servidor interrompido pelo usuário')
sys.exit(0)