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

View file

@ -3,21 +3,21 @@ import re
from datetime import datetime
def bl_path(BLACKLIST, user):
return BLACKLIST.joinpath(user).with_suffix(".txt")
def bl_path(bl, user):
return bl.joinpath(user).with_suffix(".txt")
def generate_list(ROOT_PATH, NO_BLACKLIST, NUM_MUS, BLACKLIST):
MUSIK = []
USERS = []
def generate_list(args):
musik_list = []
user_list = []
print("> Génération de la liste de musiques")
for q in ROOT_PATH.iterdir():
for q in args.lists.iterdir():
_u = q.stem
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")
with bl_path(BLACKLIST, _u).open("r") as blf:
with bl_path(args.blacklists, _u).open("r") as blf:
blacklist = blf.read().splitlines()
else:
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))
assert (
len(_musiks) >= NUM_MUS
), f"{_u} a {len(_musiks)} musique(s) non black-listée au lieu de {NUM_MUS}"
len(_musiks) >= args.number
), f"{_u} a {len(_musiks)} musique(s) non black-listée au lieu de {args.number}"
print("> > > Ajout des musiques à la liste")
MUSIK += random.sample(_musiks, NUM_MUS)
USERS += [_u] * NUM_MUS
musik_list += random.sample(_musiks, args.number)
user_list += [_u] * args.number
# Shuffle musics
print("> Classement aléatoire des musiques")
UM = list(zip(USERS, MUSIK))
random.shuffle(UM)
return zip(*UM)
user_musik_list = list(zip(user_list, musik_list))
random.shuffle(user_musik_list)
return user_musik_list
def write_blacklist(USERS, MUSIK, BLACKLIST):
for user, musik in zip(USERS, MUSIK):
with bl_path(BLACKLIST, user).open("a") as f:
def write_blacklist(musik_list, args):
for user, musik in musik_list:
with bl_path(args.blacklists, user).open("a") as f:
f.write("\n")
f.write(musik)
def write_results(USERS, MUSIK, RESULTS):
with RESULTS.joinpath(datetime.now().strftime("%Y%m%d %H%M%S")).with_suffix(
def write_results(musik_list, args):
with args.results.joinpath(datetime.now().strftime("%Y%m%d %H%M%S")).with_suffix(
".txt"
).open("a") as f:
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
def create_playlist(MUSIK):
def create_playlist(musik_list):
# Connexion à l'API youtube, obtention d'un jeton OAuth
print("> Connexion à l'API Youtube")
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
@ -30,14 +30,14 @@ def create_playlist(MUSIK):
pl_response = pl_request.execute()
print(
"> > 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
print("> Insertion des musiques dans la playlist")
print(f"> > {'_'*len(MUSIK)}")
print(f"> > {'_'*len(musik_list)}")
print("> > ", end="")
for musik in MUSIK:
for _, musik in musik_list:
print("#", end="")
request = youtube.playlistItems().insert(
part="snippet",