From 9a1e02c90a54426c0d634b781393ce651a627094 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Mon, 1 Jul 2024 00:42:46 +0200 Subject: [PATCH] feat: :construction: Starting working on spotify auth + create QR code for easy sharing --- main.py | 26 +++++++++++++++++++++----- templates/create.html | 1 + utils/generateQRCode.py | 18 ++++++++++++++++++ utils/spotifyAPI.py | 18 +++++++++++------- 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 utils/generateQRCode.py diff --git a/main.py b/main.py index aee6936..76b196e 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,12 @@ import requests import os -from flask import Flask, request, render_template +from flask import Flask, request, render_template, redirect, session, url_for from flask_bootstrap import Bootstrap5 -from utils.spotifyAPI import searchSpotify +from utils.spotifyAPI import searchSpotify, create_spotify_oauth +from utils.generateQRCode import generateQRCode import sqlite3 from random import randint - app = Flask(__name__) bootstrap = Bootstrap5(app) @@ -31,7 +31,7 @@ def main(): def create_playlist(): playlistName = request.form.get('roomName') if not playlistName: - return render_template("create.html", response="Invalid playlist name", comment="Please provide a valid playlist name", type="error") + return redirect(url_for('main')) playlistID = "test" # check if the room id already exists roomid = randint(10000000, 99999999) @@ -39,7 +39,9 @@ def create_playlist(): roomid = randint(10000000, 99999999) cursor.execute("INSERT INTO rooms (roomid, spotify_id, playlist_name) VALUES (?, ?, ?)", (roomid, playlistID, playlistName)) database.commit() - return render_template("create.html", response="Playlist created successfully", comment="Enjoy the night \U0001f57a", type="success", roomid=roomid) + qrcode = generateQRCode("http://localhost:3000/search/" + str(roomid)) + print(qrcode) + return render_template("create.html", response="Playlist created successfully", comment="Enjoy the night \U0001f57a (and share this QRCode with your friends)", image=qrcode, type="success", roomid=roomid) @app.route('/search/', methods=['GET', 'POST']) def main_app(roomid): @@ -72,6 +74,20 @@ def add_to_playlist(roomid, trackid): return render_template("add.html", response='Request failed', comment=e, type="error", roomid=roomid) +@app.route('/authenticate') +def authentification(): + sp_auth = create_spotify_oauth() + auth_url = sp_auth.get_authorize_url() + return redirect(auth_url) + +@app.route('/callback') +def callback(): + sp_oauth = create_spotify_oauth() + code = requests.args.get('code') + token_info = sp_oauth.get_access_token(code) + session["TOKEN_INFO"] = token_info + return redirect(url_for('create')) + if __name__ == '__main__': if not os.getenv("client_id") or not os.getenv("client_secret") or not os.getenv("n8n_webhook"): print("Please provide client_id, client_secret and n8n_webhook in the .env file") diff --git a/templates/create.html b/templates/create.html index 9f9d553..ab6e6a2 100644 --- a/templates/create.html +++ b/templates/create.html @@ -24,6 +24,7 @@ title:"{{ response }}", text: "{{ comment }}", icon: "{{ type }}", + imageUrl: "data:image/png;base64, {{ image }}", confirmButtonText: "Add some music!", }).then(function() { window.location = "/search/{{ roomid }}"; diff --git a/utils/generateQRCode.py b/utils/generateQRCode.py new file mode 100644 index 0000000..4191572 --- /dev/null +++ b/utils/generateQRCode.py @@ -0,0 +1,18 @@ +import qrcode +from io import BytesIO +import base64 +def generateQRCode(url: str, img_name: str = 'QR_Code.png') -> str: + """ + This function generates a QR code from a given URL and saves it as an image file + :param url: URL to generate QR code from + :param img_name: Name of the image file + :return: base64 encoded image + """ + code = qrcode.QRCode(version=1, box_size=10, border=4) + code.add_data(url) + code.make(fit=True) + img = code.make_image(fill_color="black", back_color="white") + buffered = BytesIO() + img.save(buffered) + img_str = base64.b64encode(buffered.getvalue()).decode() + return img_str \ No newline at end of file diff --git a/utils/spotifyAPI.py b/utils/spotifyAPI.py index 3161b24..eba4f7c 100644 --- a/utils/spotifyAPI.py +++ b/utils/spotifyAPI.py @@ -1,7 +1,8 @@ import spotipy -from spotipy.oauth2 import SpotifyClientCredentials +from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth import os from dotenv import load_dotenv +from flask import url_for load_dotenv() @@ -25,11 +26,14 @@ def createPlaylist(playlistName): #TODO: Implement this function pass -def spotifyLogin(): +def create_spotify_oauth(): """ - This function is used to login to spotify to get the user token - :return: user token + This function creates a Spotify OAuth object + :return: SpotifyOAuth object """ - credentials = spotipy.oauth2.SpotifyClientCredentials(client_id=os.getenv("client_id"), client_secret=os.getenv("client_secret")) - spotify_token = credentials.get_access_token() - return spotify_token \ No newline at end of file + return SpotifyOAuth( + client_id=os.getenv("client_id"), + client_secret=os.getenv("client_secret"), + redirect_uri=url_for('callback', _external=True), + scope='playlist-modify-public' + ) \ No newline at end of file