print QR code in terminal using block characters
Hunter
Sunday, November 2nd, 2025 at 7:12 PM
2 """
3 Simple HTTP Server for vibe capsule MP3 player
4 Starts server and opens browser automatically
5 +Automatically manages a virtual environment for dependencies
6 """
7
8 import http.server
10 import webbrowser
11 import socket
12 import sys
13 +import os
14 +import subprocess
15 from pathlib import Path
16
17 DEFAULT_PORT = 8000
18 +SCRIPT_DIR = Path(__file__).parent.absolute()
19 +VENV_DIR = SCRIPT_DIR / "venv"
20 +REQUIREMENTS_FILE = SCRIPT_DIR / "requirements.txt"
21 +
22 +
23 +def setup_venv():
24 + """Create and setup virtual environment if it doesn't exist"""
25 + if not VENV_DIR.exists():
26 + print("Creating virtual environment...")
27 + try:
28 + subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)])
29 + print("Virtual environment created successfully.")
30 + except subprocess.CalledProcessError as e:
31 + print(f"Error creating virtual environment: {e}")
32 + sys.exit(1)
33 +
34 + # Determine the path to pip in the venv
35 + if sys.platform == "win32":
36 + pip_path = VENV_DIR / "Scripts" / "pip"
37 + python_path = VENV_DIR / "Scripts" / "python"
38 + else:
39 + pip_path = VENV_DIR / "bin" / "pip"
40 + python_path = VENV_DIR / "bin" / "python3"
41 +
42 + # Install requirements if requirements.txt exists
43 + if REQUIREMENTS_FILE.exists():
44 + print("Installing dependencies from requirements.txt...")
45 + try:
46 + subprocess.check_call([str(pip_path), "install", "-q", "-r", str(REQUIREMENTS_FILE)])
47 + print("Dependencies installed successfully.\n")
48 + except subprocess.CalledProcessError as e:
49 + print(f"Error installing dependencies: {e}")
50 + sys.exit(1)
51 +
52 + return python_path
53 +
54 +
55 +def run_in_venv():
56 + """Re-run this script in the virtual environment"""
57 + python_path = setup_venv()
58 +
59 + # Re-run this script with the venv Python
60 + subprocess.check_call([str(python_path), __file__, "--in-venv"])
61 + sys.exit(0)
62
63
64 def get_local_ip():
86 return None
87
88
-def main():
89 +def print_qr_code(url):
90 + """Generate and print a QR code using block characters"""
91 + try:
92 + import qrcode
93 +
94 + qr = qrcode.QRCode(
95 + version=1,
96 + error_correction=qrcode.constants.ERROR_CORRECT_L,
97 + box_size=1,
98 + border=1,
99 + )
100 + qr.add_data(url)
101 + qr.make(fit=True)
102 +
103 + # Get the QR code matrix
104 + matrix = qr.get_matrix()
105 +
106 + # Print QR code using block characters
107 + # Use full block (█) for black and space for white
108 + print("\nScan to connect:")
109 + for row in matrix:
110 + line = ""
111 + for cell in row:
112 + line += "██" if cell else " "
113 + print(line)
114 + print()
115 + except ImportError:
116 + print("\nQR code generation unavailable (qrcode library not installed)")
117 + except Exception as e:
118 + print(f"\nCould not generate QR code: {e}")
119 +
120 +
121 +def start_server():
122 + """Start the HTTP server (runs after venv is set up)"""
123 # Change to script directory
- script_dir = Path(__file__).parent.absolute()
- os.chdir(script_dir)
124 + os.chdir(SCRIPT_DIR)
125
126 # Find an available port
127 port = find_available_port(DEFAULT_PORT)
160 print(f"\nServer running on port {port}")
161 print(f"\nLocal access: {local_url}")
162 print(f"Network access: {network_url}")
- print("\nPress Ctrl+C to stop the server")
163 +
164 + # Print QR code for easy mobile access
165 + print_qr_code(network_url)
166 +
167 + print("Press Ctrl+C to stop the server")
168 print("=" * 60)
169
170 # Open browser
181 sys.exit(1)
182
183
184 +def main():
185 + """Main entry point"""
186 + # Check if we're already running in venv
187 + if "--in-venv" not in sys.argv:
188 + run_in_venv()
189 + else:
190 + start_server()
191 +
192 +
193 if __name__ == "__main__":
- import os
194 main()
1 mutagen==1.47.0
2 +qrcode