commit 650b393b6953c6b37268c8d0628138866f483461
parent 99a1be1e6c1d9a0cecb90083e694eb29a976e97c
Author: Hunter
Date:   Sun,  2 Nov 2025 19:12:58 -0500

print QR code in terminal using block characters

Diffstat:
Mhost.py | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mrequirements.txt | 1+
2 files changed, 97 insertions(+), 5 deletions(-)

diff --git a/host.py b/host.py @@ -2,6 +2,7 @@ """ Simple HTTP Server for vibe capsule MP3 player Starts server and opens browser automatically +Automatically manages a virtual environment for dependencies """ import http.server @@ -9,9 +10,55 @@ import socketserver import webbrowser import socket import sys +import os +import subprocess from pathlib import Path DEFAULT_PORT = 8000 +SCRIPT_DIR = Path(__file__).parent.absolute() +VENV_DIR = SCRIPT_DIR / "venv" +REQUIREMENTS_FILE = SCRIPT_DIR / "requirements.txt" + + +def setup_venv(): + """Create and setup virtual environment if it doesn't exist""" + if not VENV_DIR.exists(): + print("Creating virtual environment...") + try: + subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)]) + print("Virtual environment created successfully.") + except subprocess.CalledProcessError as e: + print(f"Error creating virtual environment: {e}") + sys.exit(1) + + # Determine the path to pip in the venv + if sys.platform == "win32": + pip_path = VENV_DIR / "Scripts" / "pip" + python_path = VENV_DIR / "Scripts" / "python" + else: + pip_path = VENV_DIR / "bin" / "pip" + python_path = VENV_DIR / "bin" / "python3" + + # Install requirements if requirements.txt exists + if REQUIREMENTS_FILE.exists(): + print("Installing dependencies from requirements.txt...") + try: + subprocess.check_call([str(pip_path), "install", "-q", "-r", str(REQUIREMENTS_FILE)]) + print("Dependencies installed successfully.\n") + except subprocess.CalledProcessError as e: + print(f"Error installing dependencies: {e}") + sys.exit(1) + + return python_path + + +def run_in_venv(): + """Re-run this script in the virtual environment""" + python_path = setup_venv() + + # Re-run this script with the venv Python + subprocess.check_call([str(python_path), __file__, "--in-venv"]) + sys.exit(0) def get_local_ip(): @@ -39,10 +86,42 @@ def find_available_port(start_port=DEFAULT_PORT, max_attempts=10): return None -def main(): +def print_qr_code(url): + """Generate and print a QR code using block characters""" + try: + import qrcode + + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=1, + border=1, + ) + qr.add_data(url) + qr.make(fit=True) + + # Get the QR code matrix + matrix = qr.get_matrix() + + # Print QR code using block characters + # Use full block (█) for black and space for white + print("\nScan to connect:") + for row in matrix: + line = "" + for cell in row: + line += "██" if cell else " " + print(line) + print() + except ImportError: + print("\nQR code generation unavailable (qrcode library not installed)") + except Exception as e: + print(f"\nCould not generate QR code: {e}") + + +def start_server(): + """Start the HTTP server (runs after venv is set up)""" # Change to script directory - script_dir = Path(__file__).parent.absolute() - os.chdir(script_dir) + os.chdir(SCRIPT_DIR) # Find an available port port = find_available_port(DEFAULT_PORT) @@ -81,7 +160,11 @@ def main(): print(f"\nServer running on port {port}") print(f"\nLocal access: {local_url}") print(f"Network access: {network_url}") - print("\nPress Ctrl+C to stop the server") + + # Print QR code for easy mobile access + print_qr_code(network_url) + + print("Press Ctrl+C to stop the server") print("=" * 60) # Open browser @@ -98,6 +181,14 @@ def main(): sys.exit(1) +def main(): + """Main entry point""" + # Check if we're already running in venv + if "--in-venv" not in sys.argv: + run_in_venv() + else: + start_server() + + if __name__ == "__main__": - import os main() diff --git a/requirements.txt b/requirements.txt @@ -1 +1,2 @@ mutagen==1.47.0 +qrcode