feat: 🚧 Starting working on spotify auth + create QR code for easy sharing

This commit is contained in:
Louis Gallet 2024-07-01 00:42:46 +02:00
parent 708c8beac6
commit 9a1e02c90a
Signed by: lgallet
GPG Key ID: 84D3DF1528A84511
4 changed files with 51 additions and 12 deletions

26
main.py
View File

@ -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/<string:roomid>', 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")

View File

@ -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 }}";

18
utils/generateQRCode.py Normal file
View File

@ -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

View File

@ -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
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'
)