Add qrcode generator endpoint
This commit is contained in:
parent
bc9bf71824
commit
3b927522e3
3 changed files with 53 additions and 1 deletions
|
@ -8,6 +8,7 @@ from .connect import get_connection
|
|||
from .room import Room
|
||||
from .song import Song, init_db, get_song_by_title_artist, add_song_in_db
|
||||
from .song_fetch import lastfm_query_search, download_song_mp3
|
||||
from .qrcode_gen import generate_qr
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
@ -174,5 +175,22 @@ def add_song():
|
|||
return {"artist": info.artist, "title": info.title, "tags": info.tags, "image_id": info.img_id}
|
||||
|
||||
|
||||
@app.get("/api/room/qrcode")
|
||||
def room_qrcode():
|
||||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (pin := request.args.get("pin")) is not None:
|
||||
pin = int(pin)
|
||||
|
||||
stream = generate_qr(
|
||||
base_uri="https://chillbox.leoinvents.com",
|
||||
room_id=int(room_id),
|
||||
pin=pin,
|
||||
)
|
||||
|
||||
return Response(stream, content_type="image/jpeg")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
socketio.run(app, debug=True)
|
||||
|
|
32
backend/src/qrcode_gen.py
Normal file
32
backend/src/qrcode_gen.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import qrcode
|
||||
import urllib.parse
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
def create_login_url(base_uri: str, room_id: int, pin: int | None) -> str:
|
||||
parsed = urllib.parse.urlparse(base_uri)
|
||||
|
||||
params = {
|
||||
"room": room_id,
|
||||
}
|
||||
|
||||
if pin is not None:
|
||||
params["pin"] = pin
|
||||
|
||||
parsed = parsed._replace(path="join", query=urllib.parse.urlencode(params))
|
||||
|
||||
return urllib.parse.urlunparse(parsed)
|
||||
|
||||
|
||||
def generate_qr(base_uri: str, room_id: int, pin: int | None) -> BytesIO:
|
||||
url = create_login_url(base_uri, room_id, pin)
|
||||
|
||||
qr = qrcode.make(url)
|
||||
|
||||
out = BytesIO()
|
||||
|
||||
qr.save(out, format="jpeg")
|
||||
|
||||
out.seek(0)
|
||||
|
||||
return out
|
Loading…
Add table
Add a link
Reference in a new issue