Save youtube credentials

This commit is contained in:
Edgar P. Burkhart 2024-04-10 13:33:52 +02:00
parent 6f2726f423
commit 96c6bfa8fa
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 16 additions and 3 deletions

View file

@ -1,8 +1,8 @@
import logging import logging
import random import random
import re import re
from datetime import datetime
import sys import sys
from datetime import datetime
logger = logging.getLogger("musik.list") logger = logging.getLogger("musik.list")

View file

@ -1,5 +1,7 @@
import logging import logging
from datetime import date from datetime import date
from pathlib import Path
import pickle
import google_auth_oauthlib.flow import google_auth_oauthlib.flow
import googleapiclient.discovery import googleapiclient.discovery
@ -9,13 +11,24 @@ logger = logging.getLogger("musik.youtube")
def create_playlist(musik_list): def create_playlist(musik_list):
pickle_path = Path("./youtube.pickle")
# Connexion à l'API youtube, obtention d'un jeton OAuth # Connexion à l'API youtube, obtention d'un jeton OAuth
logger.info("Connexion à l'API Youtube") logger.info("Connexion à l'API Youtube")
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file( flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
"./secret.json", ["https://www.googleapis.com/auth/youtube.force-ssl"] "./secret.json", ["https://www.googleapis.com/auth/youtube.force-ssl"]
) )
if pickle_path.is_file():
with pickle_path.open("rb") as f:
credentials = pickle.load(f)
else:
credentials = flow.run_local_server(port=0) credentials = flow.run_local_server(port=0)
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials) with pickle_path.open("wb") as f:
pickle.dump(credentials, f)
youtube = googleapiclient.discovery.build(
"youtube", "v3", credentials=credentials
)
# Création d'une playlist # Création d'une playlist
logger.info("Création de la playlist") logger.info("Création de la playlist")