Split code in separate files
This commit is contained in:
parent
4056887b09
commit
06a9bb1cb6
2 changed files with 68 additions and 54 deletions
|
@ -8,19 +8,14 @@ from datetime import date, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from urllib import parse, request
|
from urllib import parse, request
|
||||||
|
|
||||||
|
from .list import generate_list, write_blacklist, write_results
|
||||||
|
|
||||||
|
|
||||||
ROOT_PATH = Path("./lists")
|
ROOT_PATH = Path("./lists")
|
||||||
BLACKLIST = Path("./blacklists")
|
BLACKLIST = Path("./blacklists")
|
||||||
RESULTS = Path("./results")
|
RESULTS = Path("./results")
|
||||||
NUM_MUS = 2
|
NUM_MUS = 2
|
||||||
|
|
||||||
MUSIK = []
|
|
||||||
USERS = []
|
|
||||||
|
|
||||||
|
|
||||||
def bl_path(user):
|
|
||||||
return Path(BLACKLIST, user).with_suffix(".txt")
|
|
||||||
|
|
||||||
|
|
||||||
# Lecture arguments console
|
# Lecture arguments console
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
|
@ -70,11 +65,6 @@ args = parser.parse_args()
|
||||||
NO_API = args.no_api
|
NO_API = args.no_api
|
||||||
if not NO_API:
|
if not NO_API:
|
||||||
from .youtube import create_playlist
|
from .youtube import create_playlist
|
||||||
NO_BLACKLIST = args.no_blacklist
|
|
||||||
NUM_MUS = args.number
|
|
||||||
ROOT_PATH = args.lists
|
|
||||||
BLACKLIST = args.blacklists
|
|
||||||
RESULTS = args.results
|
|
||||||
|
|
||||||
print("--- DÉBUT ---")
|
print("--- DÉBUT ---")
|
||||||
|
|
||||||
|
@ -89,38 +79,9 @@ assert NUM_MUS >= 1, "NUM_MUS < 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
|
||||||
print("> Génération de la liste de musiques")
|
USERS, MUSIK = generate_list(
|
||||||
for q in ROOT_PATH.iterdir():
|
args.lists, args.no_blacklist, args.number, args.blacklists
|
||||||
_u = q.stem
|
)
|
||||||
print(f"> > Musiques de {_u}")
|
|
||||||
if (not NO_BLACKLIST) and bl_path(_u).exists():
|
|
||||||
print("> > > Blacklist")
|
|
||||||
with bl_path(_u).open("r") as blf:
|
|
||||||
blacklist = blf.read().splitlines()
|
|
||||||
else:
|
|
||||||
blacklist = []
|
|
||||||
|
|
||||||
print("> > > Lecture de la liste")
|
|
||||||
with q.open() as f:
|
|
||||||
_raw_musiks = [
|
|
||||||
(re.compile(r"[\?\&]v=(\w+)").search(_musik) or ("", _musik))[1]
|
|
||||||
for _musik in f.read().splitlines()
|
|
||||||
]
|
|
||||||
_musiks = list(filter(lambda _m: not _m 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}"
|
|
||||||
|
|
||||||
print("> > > Ajout des musiques à la liste")
|
|
||||||
MUSIK += random.sample(_musiks, NUM_MUS)
|
|
||||||
USERS += [_u] * NUM_MUS
|
|
||||||
|
|
||||||
# Shuffle musics
|
|
||||||
print("> Classement aléatoire des musiques")
|
|
||||||
UM = list(zip(USERS, MUSIK))
|
|
||||||
random.shuffle(UM)
|
|
||||||
USERS, MUSIK = zip(*UM)
|
|
||||||
|
|
||||||
if not NO_API:
|
if not NO_API:
|
||||||
create_playlist(MUSIK)
|
create_playlist(MUSIK)
|
||||||
|
@ -131,18 +92,11 @@ else:
|
||||||
|
|
||||||
# Écriture des résultats
|
# Écriture des résultats
|
||||||
print("> Écriture des résultats")
|
print("> Écriture des résultats")
|
||||||
with RESULTS.joinpath(datetime.now().strftime("%Y%m%d %H%M%S")).with_suffix(
|
write_results(USERS, MUSIK, args.results)
|
||||||
".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)))
|
|
||||||
|
|
||||||
# Écriture de la blacklist
|
# Écriture de la blacklist
|
||||||
if not NO_BLACKLIST:
|
if not args.no_blacklist:
|
||||||
print("> Écriture de la blacklist")
|
print("> Écriture de la blacklist")
|
||||||
for user, musik in zip(USERS, MUSIK):
|
write_blacklist(args.blacklists)
|
||||||
with bl_path(user).open("a") as f:
|
|
||||||
f.write("\n")
|
|
||||||
f.write(musik)
|
|
||||||
|
|
||||||
print("--- FIN ---")
|
print("--- FIN ---")
|
||||||
|
|
60
musik/list.py
Normal file
60
musik/list.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
from datetime import datetime
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def bl_path(BLACKLIST, user):
|
||||||
|
return BLACKLIST.joinpath(user).with_suffix(".txt")
|
||||||
|
|
||||||
|
|
||||||
|
def generate_list(ROOT_PATH, NO_BLACKLIST, NUM_MUS, BLACKLIST):
|
||||||
|
MUSIK = []
|
||||||
|
USERS = []
|
||||||
|
|
||||||
|
print("> Génération de la liste de musiques")
|
||||||
|
for q in ROOT_PATH.iterdir():
|
||||||
|
_u = q.stem
|
||||||
|
print(f"> > Musiques de {_u}")
|
||||||
|
if (not NO_BLACKLIST) and bl_path(BLACKLIST, _u).exists():
|
||||||
|
print("> > > Blacklist")
|
||||||
|
with bl_path(BLACKLIST, _u).open("r") as blf:
|
||||||
|
blacklist = blf.read().splitlines()
|
||||||
|
else:
|
||||||
|
blacklist = []
|
||||||
|
|
||||||
|
print("> > > Lecture de la liste")
|
||||||
|
with q.open() as f:
|
||||||
|
_raw_musiks = [
|
||||||
|
(re.compile(r"[\?\&]v=(\w+)").search(_musik) or ("", _musik))[1]
|
||||||
|
for _musik in f.read().splitlines()
|
||||||
|
]
|
||||||
|
_musiks = list(filter(lambda _m: not _m 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}"
|
||||||
|
|
||||||
|
print("> > > Ajout des musiques à la liste")
|
||||||
|
MUSIK += random.sample(_musiks, NUM_MUS)
|
||||||
|
USERS += [_u] * NUM_MUS
|
||||||
|
|
||||||
|
# Shuffle musics
|
||||||
|
print("> Classement aléatoire des musiques")
|
||||||
|
UM = list(zip(USERS, MUSIK))
|
||||||
|
random.shuffle(UM)
|
||||||
|
return zip(*UM)
|
||||||
|
|
||||||
|
|
||||||
|
def write_blacklist(BLACKLIST):
|
||||||
|
for user, musik in zip(USERS, MUSIK):
|
||||||
|
with bl_path(BLACKLIST, 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(
|
||||||
|
".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)))
|
||||||
|
|
Loading…
Reference in a new issue