Improve logging
This commit is contained in:
parent
0fd0c4911b
commit
c0b9924ed3
3 changed files with 105 additions and 86 deletions
|
@ -1,4 +1,5 @@
|
|||
import argparse
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from .list import generate_list, write_blacklist, write_results
|
||||
|
@ -8,6 +9,8 @@ BLACKLIST = Path("./blacklists")
|
|||
RESULTS = Path("./results")
|
||||
NUM_MUS = 2
|
||||
|
||||
|
||||
def main():
|
||||
# Lecture arguments console
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="python -m musik",
|
||||
|
@ -51,14 +54,25 @@ parser.add_argument(
|
|||
default=RESULTS,
|
||||
help="Sélectionner le dossier pour stocker les résultats",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v",
|
||||
"--verbose",
|
||||
action="store_const",
|
||||
const=logging.DEBUG,
|
||||
default=logging.INFO,
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
if not args.no_api:
|
||||
from .youtube import create_playlist
|
||||
|
||||
print("--- DÉBUT ---")
|
||||
# Configuration logs
|
||||
logger = logging.getLogger("musik")
|
||||
logging.basicConfig(
|
||||
level=args.verbose, format="%(asctime)s\t%(levelname)s\t%(message)s"
|
||||
)
|
||||
|
||||
print("> Vérification")
|
||||
logger.info("Vérification")
|
||||
assert args.lists.is_dir(), f"ROOT_PATH={ROOT_PATH} n'est pas un dossier"
|
||||
if not args.blacklists.is_dir():
|
||||
args.blacklists.mkdir()
|
||||
|
@ -69,22 +83,25 @@ 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
|
||||
logger.info("Génération de la liste de musiques")
|
||||
musik_list = generate_list(args)
|
||||
|
||||
if not args.no_api:
|
||||
create_playlist(musik_list)
|
||||
else:
|
||||
print("> Liste des musiques :")
|
||||
logger.info("Liste des musiques :")
|
||||
for _, musik in musik_list:
|
||||
print(f"> # https://www.youtube.com/watch?v={musik}")
|
||||
logger.info(f"# https://www.youtube.com/watch?v={musik}")
|
||||
|
||||
# Écriture des résultats
|
||||
print("> Écriture des résultats")
|
||||
logger.info("Écriture des résultats")
|
||||
write_results(musik_list, args)
|
||||
|
||||
# Écriture de la blacklist
|
||||
if not args.no_blacklist:
|
||||
print("> Écriture de la blacklist")
|
||||
logger.info("Écriture de la blacklist")
|
||||
write_blacklist(musik_list, args)
|
||||
|
||||
print("--- FIN ---")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import logging
|
||||
import random
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger("musik.list")
|
||||
|
||||
|
||||
def bl_path(bl, user):
|
||||
return bl.joinpath(user).with_suffix(".txt")
|
||||
|
@ -11,18 +14,17 @@ def generate_list(args):
|
|||
musik_list = []
|
||||
user_list = []
|
||||
|
||||
print("> Génération de la liste de musiques")
|
||||
for q in args.lists.iterdir():
|
||||
_u = q.stem
|
||||
print(f"> > Musiques de {_u}")
|
||||
logger.info(f"Musiques de {_u}")
|
||||
if (not args.no_blacklist) and bl_path(args.blacklists, _u).exists():
|
||||
print("> > > Blacklist")
|
||||
logger.debug("Blacklist")
|
||||
with bl_path(args.blacklists, _u).open("r") as blf:
|
||||
blacklist = blf.read().splitlines()
|
||||
else:
|
||||
blacklist = []
|
||||
|
||||
print("> > > Lecture de la liste")
|
||||
logger.debug("Lecture de la liste")
|
||||
with q.open() as f:
|
||||
_raw_musiks = [
|
||||
(re.compile(r"[\?\&]v=(\w+)").search(_musik) or ("", _musik))[1]
|
||||
|
@ -34,12 +36,12 @@ def generate_list(args):
|
|||
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")
|
||||
logger.debug("Ajout des musiques à la liste")
|
||||
musik_list += random.sample(_musiks, args.number)
|
||||
user_list += [_u] * args.number
|
||||
|
||||
# Shuffle musics
|
||||
print("> Classement aléatoire des musiques")
|
||||
logger.info("Classement aléatoire des musiques")
|
||||
user_musik_list = list(zip(user_list, musik_list))
|
||||
random.shuffle(user_musik_list)
|
||||
return user_musik_list
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
import logging
|
||||
from datetime import date
|
||||
|
||||
import google_auth_oauthlib.flow
|
||||
import googleapiclient.discovery
|
||||
import googleapiclient.errors
|
||||
|
||||
logger = logging.getLogger("musik.youtube")
|
||||
|
||||
|
||||
def create_playlist(musik_list):
|
||||
# Connexion à l'API youtube, obtention d'un jeton OAuth
|
||||
print("> Connexion à l'API Youtube")
|
||||
logger.info("Connexion à l'API Youtube")
|
||||
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
|
||||
"./secret.json", ["https://www.googleapis.com/auth/youtube.force-ssl"]
|
||||
)
|
||||
|
@ -15,7 +18,7 @@ def create_playlist(musik_list):
|
|||
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
|
||||
|
||||
# Création d'une playlist
|
||||
print("> Création de la playlist")
|
||||
logger.info("Création de la playlist")
|
||||
pl_request = youtube.playlists().insert(
|
||||
part="snippet,status",
|
||||
body={
|
||||
|
@ -28,17 +31,15 @@ def create_playlist(musik_list):
|
|||
},
|
||||
)
|
||||
pl_response = pl_request.execute()
|
||||
print(
|
||||
"> > Playlist créée :",
|
||||
logger.info(
|
||||
"Playlist créée : "
|
||||
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_list)}")
|
||||
print("> > ", end="")
|
||||
logger.info("Insertion des musiques dans la playlist")
|
||||
for _, musik in musik_list:
|
||||
print("#", end="")
|
||||
logger.debug(musik)
|
||||
request = youtube.playlistItems().insert(
|
||||
part="snippet",
|
||||
body={
|
||||
|
@ -53,4 +54,3 @@ def create_playlist(musik_list):
|
|||
},
|
||||
)
|
||||
request.execute()
|
||||
print()
|
||||
|
|
Loading…
Reference in a new issue