Created
April 25, 2024 04:33
-
-
Save pythoninthegrass/1b640fb39130f862d28bac1e390cb3c0 to your computer and use it in GitHub Desktop.
Generate QR codes from a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""SOURCE: https://github.com/pythoninthegrass/python_template/blob/main/boilerplate/qr_gen.py""" | |
import sys | |
from datetime import datetime | |
from pathlib import Path | |
try: | |
from pyqrcode import QRCode | |
except ImportError: | |
print("pyqrcode module not found. Install it with 'python -m pip install pyqrcode pypng'") | |
sys.exit(1) | |
home = str(Path.home()) | |
downloads = Path(f"{home}/Downloads") | |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") | |
if len(sys.argv) == 2 and sys.argv[1].startswith("http"): | |
url = QRCode(sys.argv[1]) | |
else: | |
url = QRCode("https://github.com/pythoninthegrass/python_template") | |
def check_file(): | |
""" | |
Check if QR code already exists in ~/Downloads folder. | |
If it does, rename it with a timestamp. | |
""" | |
if Path(f"{home}/Downloads/qr.png").exists(): | |
print("QR code already exists in ~/Downloads") | |
new_file_name = Path(f"{downloads}/qr_{timestamp}.png") | |
Path(f"{downloads}/qr.png").rename(new_file_name) | |
print(f"QR code copied to Downloads folder as 'qr_{timestamp}.png'") | |
def main(): | |
check_file() | |
url.png(Path(f"{home}/Downloads/qr.png"), scale=8) | |
print("QR code saved to Downloads folder as 'qr.png'") | |
if __name__ == "__main__": | |
main() |
Author
pythoninthegrass
commented
Apr 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment