Improve code formatting

This commit is contained in:
Edgar P. Burkhart 2024-04-10 12:49:33 +02:00
parent 295a44fbaa
commit 0fd0c4911b
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 38 additions and 42 deletions

View file

@ -8,7 +8,6 @@ BLACKLIST = Path("./blacklists")
RESULTS = Path("./results") RESULTS = Path("./results")
NUM_MUS = 2 NUM_MUS = 2
# Lecture arguments console # Lecture arguments console
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="python -m musik", prog="python -m musik",
@ -54,41 +53,38 @@ parser.add_argument(
) )
args = parser.parse_args() args = parser.parse_args()
NO_API = args.no_api if not args.no_api:
if not NO_API:
from .youtube import create_playlist from .youtube import create_playlist
print("--- DÉBUT ---") print("--- DÉBUT ---")
print("> Vérification") print("> Vérification")
assert ROOT_PATH.is_dir(), f"ROOT_PATH={ROOT_PATH} n'est pas un dossier" assert args.lists.is_dir(), f"ROOT_PATH={ROOT_PATH} n'est pas un dossier"
if not BLACKLIST.is_dir(): if not args.blacklists.is_dir():
BLACKLIST.mkdir() args.blacklists.mkdir()
if not RESULTS.is_dir(): if not args.results.is_dir():
RESULTS.mkdir() args.results.mkdir()
assert NUM_MUS >= 1, "NUM_MUS < 1" assert args.number >= 1, "Nombre de musiques < 1"
# Lecture des fichiers musique dans ROOT_PATH # Lecture des fichiers musique dans ROOT_PATH
# Faire un dossier différent pour les gens qui ne jouent pas # Faire un dossier différent pour les gens qui ne jouent pas
USERS, MUSIK = generate_list( musik_list = generate_list(args)
args.lists, args.no_blacklist, args.number, args.blacklists
)
if not NO_API: if not args.no_api:
create_playlist(MUSIK) create_playlist(musik_list)
else: else:
print("> Liste des musiques :") print("> Liste des musiques :")
for musik in MUSIK: for _, musik in musik_list:
print(f"> # https://www.youtube.com/watch?v={musik}") print(f"> # https://www.youtube.com/watch?v={musik}")
# Écriture des résultats # Écriture des résultats
print("> Écriture des résultats") print("> Écriture des résultats")
write_results(USERS, MUSIK, args.results) write_results(musik_list, args)
# Écriture de la blacklist # Écriture de la blacklist
if not args.no_blacklist: if not args.no_blacklist:
print("> Écriture de la blacklist") print("> Écriture de la blacklist")
write_blacklist(USERS, MUSIK, args.blacklists) write_blacklist(musik_list, args)
print("--- FIN ---") print("--- FIN ---")

View file

@ -3,21 +3,21 @@ import re
from datetime import datetime from datetime import datetime
def bl_path(BLACKLIST, user): def bl_path(bl, user):
return BLACKLIST.joinpath(user).with_suffix(".txt") return bl.joinpath(user).with_suffix(".txt")
def generate_list(ROOT_PATH, NO_BLACKLIST, NUM_MUS, BLACKLIST): def generate_list(args):
MUSIK = [] musik_list = []
USERS = [] user_list = []
print("> Génération de la liste de musiques") print("> Génération de la liste de musiques")
for q in ROOT_PATH.iterdir(): for q in args.lists.iterdir():
_u = q.stem _u = q.stem
print(f"> > Musiques de {_u}") print(f"> > Musiques de {_u}")
if (not NO_BLACKLIST) and bl_path(BLACKLIST, _u).exists(): if (not args.no_blacklist) and bl_path(args.blacklists, _u).exists():
print("> > > Blacklist") print("> > > Blacklist")
with bl_path(BLACKLIST, _u).open("r") as blf: with bl_path(args.blacklists, _u).open("r") as blf:
blacklist = blf.read().splitlines() blacklist = blf.read().splitlines()
else: else:
blacklist = [] blacklist = []
@ -31,30 +31,30 @@ def generate_list(ROOT_PATH, NO_BLACKLIST, NUM_MUS, BLACKLIST):
_musiks = list(filter(lambda _m: _m not in blacklist, _raw_musiks)) _musiks = list(filter(lambda _m: _m not in blacklist, _raw_musiks))
assert ( assert (
len(_musiks) >= NUM_MUS len(_musiks) >= args.number
), f"{_u} a {len(_musiks)} musique(s) non black-listée au lieu de {NUM_MUS}" ), f"{_u} a {len(_musiks)} musique(s) non black-listée au lieu de {args.number}"
print("> > > Ajout des musiques à la liste") print("> > > Ajout des musiques à la liste")
MUSIK += random.sample(_musiks, NUM_MUS) musik_list += random.sample(_musiks, args.number)
USERS += [_u] * NUM_MUS user_list += [_u] * args.number
# Shuffle musics # Shuffle musics
print("> Classement aléatoire des musiques") print("> Classement aléatoire des musiques")
UM = list(zip(USERS, MUSIK)) user_musik_list = list(zip(user_list, musik_list))
random.shuffle(UM) random.shuffle(user_musik_list)
return zip(*UM) return user_musik_list
def write_blacklist(USERS, MUSIK, BLACKLIST): def write_blacklist(musik_list, args):
for user, musik in zip(USERS, MUSIK): for user, musik in musik_list:
with bl_path(BLACKLIST, user).open("a") as f: with bl_path(args.blacklists, user).open("a") as f:
f.write("\n") f.write("\n")
f.write(musik) f.write(musik)
def write_results(USERS, MUSIK, RESULTS): def write_results(musik_list, args):
with RESULTS.joinpath(datetime.now().strftime("%Y%m%d %H%M%S")).with_suffix( with args.results.joinpath(datetime.now().strftime("%Y%m%d %H%M%S")).with_suffix(
".txt" ".txt"
).open("a") as f: ).open("a") as f:
f.write(f"Résultats {datetime.now()}\n\n") f.write(f"Résultats {datetime.now()}\n\n")
f.write("\n".join("\t".join(um) for um in zip(USERS, MUSIK))) f.write("\n".join("\t".join(um) for um in musik_list))

View file

@ -5,7 +5,7 @@ import googleapiclient.discovery
import googleapiclient.errors import googleapiclient.errors
def create_playlist(MUSIK): def create_playlist(musik_list):
# Connexion à l'API youtube, obtention d'un jeton OAuth # Connexion à l'API youtube, obtention d'un jeton OAuth
print("> Connexion à l'API Youtube") print("> Connexion à l'API Youtube")
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file( flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
@ -30,14 +30,14 @@ def create_playlist(MUSIK):
pl_response = pl_request.execute() pl_response = pl_request.execute()
print( print(
"> > Playlist créée :", "> > Playlist créée :",
f"https://www.youtube.com/playlist?list={pl_response['id']}" f"https://www.youtube.com/playlist?list={pl_response['id']}",
) )
# Insertion des musiques dans la playlist # Insertion des musiques dans la playlist
print("> Insertion des musiques dans la playlist") print("> Insertion des musiques dans la playlist")
print(f"> > {'_'*len(MUSIK)}") print(f"> > {'_'*len(musik_list)}")
print("> > ", end="") print("> > ", end="")
for musik in MUSIK: for _, musik in musik_list:
print("#", end="") print("#", end="")
request = youtube.playlistItems().insert( request = youtube.playlistItems().insert(
part="snippet", part="snippet",