Split playlist creation in separate file

This commit is contained in:
Edgar P. Burkhart 2024-04-09 21:42:22 +02:00
parent d0ca4899a6
commit 4056887b09
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 61 additions and 53 deletions

View file

@ -24,8 +24,9 @@ def bl_path(user):
# Lecture arguments console
parser = argparse.ArgumentParser(
prog="python -m musik", description="Lancer une partie de Musik",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
prog="python -m musik",
description="Lancer une partie de Musik",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-a",
@ -67,6 +68,8 @@ parser.add_argument(
args = parser.parse_args()
NO_API = args.no_api
if not NO_API:
from .youtube import create_playlist
NO_BLACKLIST = args.no_blacklist
NUM_MUS = args.number
ROOT_PATH = args.lists
@ -120,57 +123,7 @@ random.shuffle(UM)
USERS, MUSIK = zip(*UM)
if not NO_API:
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
# Connexion à l'API youtube, obtention d'un jeton OAuth
print("> Connexion à l'API Youtube")
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
"./secret.json", ["https://www.googleapis.com/auth/youtube.force-ssl"]
)
credentials = flow.run_local_server(port=0)
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
# Création d'une playlist
print("> Création de la playlist")
pl_request = youtube.playlists().insert(
part="snippet,status",
body={
"snippet": {
"title": f"Musik {date.today().strftime('%x')}",
},
"status": {
"privacyStatus": "private",
},
},
)
pl_response = pl_request.execute()
print(
f"> > Playlist créée : 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("> > ", end="")
for musik in MUSIK:
print("#", end="")
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": pl_response.get("id"),
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": musik,
},
}
},
)
response = request.execute()
print()
create_playlist(MUSIK)
else:
print("> Liste des musiques :")
for musik in MUSIK:

55
musik/youtube.py Normal file
View file

@ -0,0 +1,55 @@
from datetime import date
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
def create_playlist(MUSIK):
# Connexion à l'API youtube, obtention d'un jeton OAuth
print("> Connexion à l'API Youtube")
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
"./secret.json", ["https://www.googleapis.com/auth/youtube.force-ssl"]
)
credentials = flow.run_local_server(port=0)
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
# Création d'une playlist
print("> Création de la playlist")
pl_request = youtube.playlists().insert(
part="snippet,status",
body={
"snippet": {
"title": f"Musik {date.today().strftime('%x')}",
},
"status": {
"privacyStatus": "private",
},
},
)
pl_response = pl_request.execute()
print(
f"> > Playlist créée : 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("> > ", end="")
for musik in MUSIK:
print("#", end="")
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": pl_response.get("id"),
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": musik,
},
}
},
)
response = request.execute()
print()