12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import requests
- import sys
- """
- The class implements
- REST API for the qrserer
- """
- class QR:
- _proto : str = "http"
- _server : str = "api.qrserver.com"
- _version : str = "v1"
- _create_str : str = "create-qr-code"
- _read_str : str = "read-qr-code"
- def create(self, text : str, size=(512, 512)):
- url = f"{self._proto}://{self._server}/{self._version}/{self._create_str}"
- payload = {
- "data": text,
- "size": f"{size[0]}x{size[1]}",
- }
- r = requests.get(url, params=payload)
- return (r.content, r.request.url)
- def read_by_url(self, imgurl: str):
- url = f"{self._proto}://{self._server}/{self._version}/{self._read_str}"
- params = {
- "fileurl": imgurl,
- }
- r = requests.get(url, params=params)
- js = r.json()
- ret = []
- for v in js :
- if v["type"] != "qrcode" :
- continue
- symbol = v["symbol"]
- for seq in symbol :
- data = seq["data"]
- if "error" in symbol or data == None:
- continue
- ret.append(data)
- return ret
|