Buy Me a Coffee? Your support is much appreciated!

youtube.py

from google_apis import create_service

class YouTube:
    API_NAME = 'youtube'
    API_VERSION = 'v3'
    SCOPES = ['https://www.googleapis.com/auth/youtube',
              'https://www.googleapis.com/auth/youtube.force-ssl']

    def __init__(self, client_file):
        self.service = None
        self.client_file = client_file
        
    def init_service(self):        
        self.service = create_service(self.client_file, self.API_NAME, self.API_VERSION, self.SCOPES)

    def my_playlists(self):
        playlists = []
        response = self.service.playlists().list(
            part='id,contentDetails,player,snippet,status',
            mine=True,
            maxResults=50
        ).execute()

        playlists.extend(response.get('items'))
        next_page_token = response.get('nextPageToken')

        while next_page_token:
            response = self.service.playlists().list(
                part='id,contentDetails,player,snippet,status',
                mine=True,
                maxResults=50,
                pageToken=next_page_token
            ).execute()            
            playlists.extend(response.get('items'))
            next_page_token = response.get('nextPageToken')
        return playlists 

    def create_playlist(self, title, description=None, privacy_status='public'):
        """
        visit https://developers.google.com/youtube/v3/docs/playlists#resource for
        request json representation and parameters
        """
        request_body = {
            'snippet': {
                'title': title,
                'description': description,
            },
            'status': {
                'privacyStatus': privacy_status
            }
        }
        response = self.service.playlists().insert(
            part='snippet,status',
            body=request_body
        ).execute()
        return response

    def update_playlist(self, playlist_id, title, description=None, privacy_status=None):
        request_body = {
            'id': playlist_id,
            'snippet': {
                'title': title,
                'description': description
            },
            'status': {
                'privacyStatus': privacy_status
            }
        }
        print(request_body)
        response = self.service.playlists().update(
            part='snippet,status',
            body=request_body
        ).execute()
        return response

    def delete_playlist(self, playlist_id):
        self.service.playlists().delete(id=playlist_id).execute()



example1

from youtube import YouTube

client_file = 'client-secret.json'
yt = YouTube(client_file)
yt.init_service()

response_playlist = yt.create_playlist('<playlist title>', '<playlist description>', '<privacy status')
playlist_id = response_playlist.get('id')
playlist_title = response_playlist['snippet']['title']

video_ids = ['<video id1', '<video id2>', '<video id3>']
for video_id in video_ids:
    request_body = {
        'snippet': {
            'playlistId': playlist_id,
            'resourceId': {
                'kind': 'youtube#video',
                'videoId': video_id
            }
        }
    }
    response = yt.service.playlistItems().insert(
        part='snippet',
        body=request_body
    ).execute()
    video_title = response['snippet']['title']
    print(f'Video "{video_title}" inserted to {playlist_title} playlist')



example2

from youtube import YouTube

def list_playlist_items(service, playlist_id):
    response = service.playlistItems().list(
        part='contentDetails',
        playlistId=playlist_id,
        maxResults=50
    ).execute()

    playlistItems = response['items']
    nextPageToken = response.get('nextPageToken')

    while nextPageToken:
        response = service.playlistItems().list(
            part='contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=nextPageToken
        ).execute()

        playlistItems.extend(response['items'])
        nextPageToken = response.get('nextPageToken')
    return playlistItems

client_file = 'client-secret.json'
yt = YouTube(client_file)
yt.init_service()


source_playlist_ids = ['<playlist id1>', '<playlist id2>']
target_playlist_id = '<target playlist id>'

for source_playlist_id in source_playlist_ids:
    playlist_items = list_playlist_items(yt.service, source_playlist_id)
   
    for playlist_item in playlist_items:
        video_id = playlist_item['contentDetails']['videoId']
        request_body = {
            'snippet': {
                'playlistId': target_playlist_id,
                'resourceId': {
                    'kind': 'youtube#video',
                    'videoId': video_id
                }
            }
        }
        response = yt.service.playlistItems().insert(
            part='snippet',
            body=request_body
        ).execute()
        video_title = response['snippet']['title']
        print(f'Video "{video_title}" inserted to {target_playlist_id} playlist')