qr.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import requests
  2. import sys
  3. """
  4. The class implements
  5. REST API for the qrserer
  6. """
  7. class QR:
  8. _proto : str = "http"
  9. _server : str = "api.qrserver.com"
  10. _version : str = "v1"
  11. _create_str : str = "create-qr-code"
  12. _read_str : str = "read-qr-code"
  13. def create(self, text : str, size=(512, 512)):
  14. url = f"{self._proto}://{self._server}/{self._version}/{self._create_str}"
  15. payload = {
  16. "data": text,
  17. "size": f"{size[0]}x{size[1]}",
  18. }
  19. r = requests.get(url, params=payload)
  20. return (r.content, r.request.url)
  21. def read_by_url(self, imgurl: str):
  22. url = f"{self._proto}://{self._server}/{self._version}/{self._read_str}"
  23. params = {
  24. "fileurl": imgurl,
  25. }
  26. r = requests.get(url, params=params)
  27. js = r.json()
  28. ret = []
  29. for v in js :
  30. if v["type"] != "qrcode" :
  31. continue
  32. symbol = v["symbol"]
  33. for seq in symbol :
  34. data = seq["data"]
  35. if "error" in symbol or data == None:
  36. continue
  37. ret.append(data)
  38. return ret