Add playlist loading feature and integrate YouTube API for playlist generation
This commit is contained in:
parent
95969b897b
commit
094c5c104d
9 changed files with 230 additions and 39 deletions
45
game/tasks.py
Normal file
45
game/tasks.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
import google.oauth2.credentials
|
||||
import googleapiclient.discovery
|
||||
from celery import shared_task
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
@shared_task
|
||||
def generate_playlist(creds, game_pk):
|
||||
game = models.MusikGame.objects.get(pk=game_pk)
|
||||
credentials = google.oauth2.credentials.Credentials(**creds)
|
||||
yt_api = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
|
||||
pl_request = yt_api.playlists().insert(
|
||||
part="snippet,status",
|
||||
body={
|
||||
"snippet": {
|
||||
"title": f"Musik – {game.group.name} – {game.date.strftime('%x')}",
|
||||
"description": "Playlist générée par Musik",
|
||||
},
|
||||
"status": {
|
||||
"privacyStatus": "private",
|
||||
},
|
||||
},
|
||||
)
|
||||
pl_response = pl_request.execute()
|
||||
pl_id = pl_response.get("id")
|
||||
game.playlist = pl_id
|
||||
game.save()
|
||||
for music in game.musicgameorder_set.all():
|
||||
request = yt_api.playlistItems().insert(
|
||||
part="snippet",
|
||||
body={
|
||||
"snippet": {
|
||||
"playlistId": pl_id,
|
||||
"resourceId": {
|
||||
"kind": "youtube#video",
|
||||
"videoId": music.music_video.yt_id,
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
request.execute()
|
||||
|
||||
game.playlist_loading = False
|
||||
game.save()
|
Loading…
Add table
Add a link
Reference in a new issue