Skip to content

Instantly share code, notes, and snippets.

@lbrealdev
Created October 18, 2022 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrealdev/9e0b7064cc07b372b46ee505cb6171ba to your computer and use it in GitHub Desktop.
Save lbrealdev/9e0b7064cc07b372b46ee505cb6171ba to your computer and use it in GitHub Desktop.
Python CLI
import argparse
from dataclasses import dataclass, field, asdict
import requests
import shutil
from datetime import datetime
import logging
class ValidationException(Exception):
pass
@dataclass
class Vaccination:
manufacturer: str
date: str
def __init__(self):
try:
date = datetime.strptime(self.date, "%Y-%m-%d")
except Exception:
raise ValidationException("Vaccination date should be in format YYYY-MM-DD.")
if date > datetime.today():
raise ValidationException("Vaccination date should not be a future date.")
if self.manufacturer.lower() not in ["pfizer","moderna","astrazeneca","janssen","sinovac"]:
raise ValidationException("Your vaccine manufacturer is not approved.")
@dataclass
class QRCode:
name: str
birth: str
vaccine: list[Vaccination] = field(default_factory=list)
def generate_qr_code(qr_code):
code = asdict(qr_code)
res = requests.get(f"http://api.qrserver.com/v1/create-qr-code/?data={code}", stream=True)
if res.status_code == 200:
with open("qr_code.png", "wb") as f:
res.raw.decode_content = True
shutil.copyfileobj(res.raw, f)
return "QR code has been generated."
else:
raise Exception("QR code cannot be generated by QR Code Generator API.")
def main():
parser = argparse.ArgumentParser(description="Generate your vaccination QR code.")
parser.add_argument("-n", "--name", type=str, help="Your name", required=True)
parser.add_argument("-b", "--birth", type=str, help="Your birthday in YYYY-MM-DD format", required=True)
parser.add_argument("-m", "--manufacturer", type=str, nargs="+", help="The vaccine manufacturer", required=True, choices=[
"pfizer","moderna","astrazeneca","janssen","sinovac"])
parser.add_argument("-d", "--date", type=str, nargs="+", help="The date of vaccination", required=True)
args = parser.parse_args()
if len(args.manufacturer) != len(args.date):
logging.error(
"The number of vaccine manufacturer doesn't match with the number of vaccine dates."
)
exit(1)
qr_code = QRCode(name=args.name, birth=args.birth, vaccine=[Vaccination(args.manufacturer[i], args.date[i]) for i in range(len(args.date))])
generate_qr_code(qr_code)
if __name__ == "__main__":
try:
main()
except ValidationException as e:
logging.error(e)
except Exception as e:
logging.exception(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment