Created
September 30, 2021 15:03
-
-
Save tonio-m/b2eacc3b36c10976ceef527bb88a6da2 to your computer and use it in GitHub Desktop.
script to clone youtube playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', | |
scopes=['https://www.googleapis.com/auth/youtube']) | |
flow.run_local_server(port=8080) | |
credentials = flow.credentials | |
service = build('youtube', 'v3', credentials=credentials) | |
playlistId_Source = 'PLevJjgBUYoSOV443dd3oJefLJTnIG-MaI' | |
playlistId_Target = 'PLY49WLz4biPxLtO8-il3PtvKONGjLnnR_' | |
response = service.playlistItems().list( | |
part='contentDetails', | |
playlistId=playlistId_Source, | |
maxResults=50 | |
).execute() | |
playlistItems = response['items'] | |
nextPageToken = response.get('nextPageToken') | |
while nextPageToken: | |
response = service.playlistItems().list( | |
part='contentDetails', | |
playlistId=playlistId_Source, | |
maxResults=50, | |
pageToken=nextPageToken | |
).execute() | |
playlistItems.extend(response['items']) | |
nextPageToken = response.get('nextPageToken') | |
for video in playlistItems: | |
print(video) | |
request_body = { | |
'snippet': { | |
'playlistId': playlistId_Target, | |
'resourceId': { | |
'kind': 'youtube#video', | |
'videoId': video['contentDetails']['videoId'] | |
} | |
} | |
} | |
service.playlistItems().insert( | |
part='snippet', | |
body=request_body | |
).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment