Skip to content

Commit

Permalink
implemented authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
retrohacking committed Sep 28, 2023
1 parent 5f0b246 commit 0727f29
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.venv
.venv
tokens.json
activate.bat
.cache*
__pycache__
9 changes: 9 additions & 0 deletions gensort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from utils.authorization_manager import have_tokens,configure_tokens, read_tokens, generate_user_token

def check_tokens():
if not have_tokens():
configure_tokens()

def authorization():
username, client_id, client_secret = read_tokens()
return generate_user_token(username, client_id, client_secret)
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gensort import check_tokens, authorization

def main():
check_tokens()
token=authorization()


if __name__=="__main__":
main()
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
certifi==2023.7.22
charset-normalizer==3.2.0
idna==3.4
redis==5.0.1
requests==2.31.0
six==1.16.0
spotipy==2.23.0
urllib3==2.0.5
webencodings==0.5.1
4 changes: 4 additions & 0 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import spotipy
REDIRECT="http://localhost/"
SCOPE="playlist-modify-private playlist-modify-public"
TOKENS_FILE="tokens.json"
48 changes: 48 additions & 0 deletions utils/authorization_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from . import *
from utils.file_manager import create_json, check_json, load_json
from os import system
from webbrowser import open


LOGIN_PAGE="https://accounts.spotify.com/it/login"
CREATE_APP_PAGE="https://developer.spotify.com/dashboard/create"

def have_tokens():
return check_json(TOKENS_FILE)

def configure_tokens():
tokens={}
own_credentials=input("Do you already own the CLIENT ID and the CLIENT SECRET? (Y/N) ")
if not own_credentials:
print("This process will guide you through the configuration of your tokens.")
system("pause")
print("First of all register or login on Spotify\n"+
"If the program does not automatically redirect you to the page, navigate to:\n"+
LOGIN_PAGE)
open(LOGIN_PAGE, new=0)
system("pause")
print()
print("Now, fill the form:\n"+
"\tApp name: Genres Sorter\n"+
"\tApp description: Sort out your messy playlists!\n"+
"\tWebsite: https://github.com/retrohacking/spotify_gensort \n"+
"\tRedirect URIs: http://localhost/\n"+
"If the program does not automatically redirect you to the page, navigate to:\n"+
CREATE_APP_PAGE)
open(CREATE_APP_PAGE, new=0)
system("pause")
print()
print("We're almost done:\n"+
"Select the app that you have just created -> Settings\n")

tokens["client_id"]=input("Insert here the Client ID: ")
tokens["client_secret"]=input("Insert here the Client Secret: ")
tokens["username"]=input("Insert your Spotify username (find it at https://www.spotify.com/us/account/profile/): ")
create_json(TOKENS_FILE, tokens)

def read_tokens():
client_tokens=load_json(TOKENS_FILE)
return client_tokens["username"], client_tokens["client_id"], client_tokens["client_secret"]

def generate_user_token(uname, c_id, c_secret):
return spotipy.util.prompt_for_user_token(username=uname, scope=SCOPE, client_id=c_id, client_secret=c_secret, redirect_uri=REDIRECT)
22 changes: 22 additions & 0 deletions utils/file_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from json import dumps, load
from os.path import isfile

def check_json(file):
if isfile(file):
return True
else:
return False

def create_json(filename, data):
if not ".json" in filename:
filename+=".json"
jsondata=dumps(data, indent=4)
file=open(filename, "w")
file.write(jsondata)
file.close()

def load_json(file):
f=open(file, "r")
jsonfile=load(f)
f.close()
return jsonfile
5 changes: 5 additions & 0 deletions utils/input_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def check_yesno(answer):
if answer.lowercase()=="y":
return True
else:
return False

0 comments on commit 0727f29

Please sign in to comment.