Split playlist creation in separate file
This commit is contained in:
parent
d0ca4899a6
commit
4056887b09
2 changed files with 61 additions and 53 deletions
55
musik/youtube.py
Normal file
55
musik/youtube.py
Normal 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()
|
Loading…
Add table
Add a link
Reference in a new issue