Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created April 25, 2024 04:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/1b640fb39130f862d28bac1e390cb3c0 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/1b640fb39130f862d28bac1e390cb3c0 to your computer and use it in GitHub Desktop.
Generate QR codes from a URL
#!/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()
@pythoninthegrass
Copy link
Author

git clone git@gist.github.com:1b640fb39130f862d28bac1e390cb3c0.git qr_gen
ln -s $(pwd)/qr_gen.py ~/.local/bin/qr
qr https://gist.github.com/pythoninthegrass/1b640fb39130f862d28bac1e390cb3c0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment