Examples

Fetching Playlists from a Channel

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def channel_playlists_example():
    channel = await api.fetch_channel_from_handle("@your_channel_handle")
    playlists = await channel.fetch_playlists()
    print([playlist.title for playlist in playlists])

asyncio.run(channel_playlists_example())

Fetching Information on a Channel

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def channel_example():
    channel = await api.fetch_channel("Channel ID")
    print(channel.call_url)
    print(channel.thumbnails)
    print(channel.localised)
    print(channel.related_playlists)
    print(channel.long_upload_status)
    print(channel.keywords)
    print(channel.banner_external.url)
    print(channel.url)

loop = asyncio.new_event_loop()
loop.run_until_complete(channel_example())

Initialising using OAuth2 Authorisation Code

import ayt_api
import asyncio


async def oauth2_auth_code_example():
    api = await ayt_api.AsyncYoutubeAPI.with_authorisation_code(
        "Your Authorisation Code", "Your Client ID", "Your Client Secret", "Your Redirect URI"
    )
    resource = await api.fetch_video("Video ID", True)
    print(resource.file_name)


asyncio.run(oauth2_auth_code_example())

Initialising using OAuth2 Generator Function

import ayt_api
import asyncio


async def oauth2_generator_example():
    api = None
    async for stage in ayt_api.AsyncYoutubeAPI.with_oauth_flow_generator("Your Client ID", "Your Client Secret"):
        if isinstance(stage, str):
            # prints the oauth consent url
            print(stage)
            continue
        # stage is now an AscyncYoutubeAPI object that is assigned to api
        api = stage
    if api:
        resource = await api.fetch_video("Video ID", authorised=True)
        print(resource.file_name)


asyncio.run(oauth2_generator_example())

Initialising using OAuth2 Token

import ayt_api
import asyncio


async def oauth2_auth_code_example():
    api = ayt_api.AsyncYoutubeAPI(oauth_token="Your OAuth2 Token")
    resource = await api.fetch_video("Video ID", True)
    print(resource.file_name)


asyncio.run(oauth2_auth_code_example())

Initialising with OAuth2

import ayt_api
import asyncio


async def oauth2_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    resource = await api.fetch_video("Video ID", True)
    print(resource.file_name)


asyncio.run(oauth2_example())

Fetching Items from a Playlist

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def playlist_video_example():
    playlist_videos = await api.fetch_playlist_items("Playlist ID")
    video_data = playlist_videos[0]
    print(video_data.id)
    print(video_data.channel_id)
    print(video_data.url)
    print(video_data.title)
    print(video_data.thumbnails.default.url)
    print(video_data.visibility)
    print(video_data.published_at)
    print(video_data.description)
    print(video_data.playlist_url)
    print(video_data.added_at)

loop = asyncio.new_event_loop()
loop.run_until_complete(playlist_video_example())

Fetching Videos from a Playlist

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def playlist_video_example():
    playlist_videos = await api.fetch_playlist_videos("Playlist ID")
    video = playlist_videos[0]
    print(video.id)
    print(video.channel_id)
    print(video.url)
    print(video.title)
    print(video.thumbnails.default.url)
    print(video.visibility)
    print(video.published_at)
    print(video.description)
    print(video.duration)

loop = asyncio.new_event_loop()
loop.run_until_complete(playlist_video_example())

Fetching Information on a Playlist

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def playlist_example():
    playlist_data = await api.fetch_playlist("Playlist ID")
    print(playlist_data.id)
    print(playlist_data.channel_id)
    print(playlist_data.url)
    print(playlist_data.title)
    print(playlist_data.thumbnails.default.url)
    print(playlist_data.visibility)
    print(playlist_data.published_at)
    print(playlist_data.description)
    print(playlist_data.embed_html)
    print(playlist_data.item_count)

loop = asyncio.new_event_loop()
loop.run_until_complete(playlist_example())

Set a Channel’s Banner

import ayt_api
import asyncio


async def set_channel_banner_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret", timeout=10
    )
    channel = await api.fetch_channel_from_handle("@your_channel_handle")
    print(channel.etag)
    print(channel.banner_external.url)
    with open("Your Banner File", "rb") as banner_file:
        banner = banner_file.read()
    await channel.set_banner(banner)
    print(channel.etag)
    print(channel.banner_external.url)


asyncio.run(set_channel_banner_example())

Set a Channel’s Watermark

import datetime

import ayt_api
import asyncio


async def set_channel_watermark_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    channel = await api.fetch_channel_from_handle("@your_channel_handle")
    with open("Your Watermark File", "rb") as watermark_file:
        watermark = watermark_file.read()
    await channel.set_watermark(
        watermark, ayt_api.WatermarkTimingType.offset_from_start, datetime.timedelta(seconds=2),
        datetime.timedelta(seconds=10)
    )


asyncio.run(set_channel_watermark_example())

Set a Video’s Thumbnail

import ayt_api
import asyncio


async def set_video_thumbnail_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    video = await api.fetch_video(f"Video ID", authorised=True)
    print(video.thumbnails.highest.url)
    print(video.thumbnails.highest.resolution)
    print(video.thumbnails.etag)
    with open("Your Thumbnail File", 'rb') as image_f:
        image = image_f.read()
    await video.set_thumbnail(image)
    # Note: This replaces the files at https://i.ytimg.com/vi/Video_ID/Image_Quality.jpg so the url doesn't change.
    print(video.thumbnails.highest.url)
    print(video.thumbnails.highest.resolution)
    print(video.thumbnails.etag)


asyncio.run(set_video_thumbnail_example())

Updating Metadata for a Channel

import ayt_api
import asyncio


async def update_channel_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    original_channel = await api.fetch_channel_from_handle("@your_channel_handle")
    print(original_channel.description)
    updated_channel = await original_channel.update(
        description="New Description"
    )
    print(updated_channel.description)


asyncio.run(update_channel_example())

Updating Metadata for a Playlist Item

import ayt_api
import asyncio


async def update_playlist_item_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    items = await api.fetch_playlist_items("Your Playlist ID")
    print(items[0].position)
    updated_item = await items[0].update(position=1)
    print(updated_item.position)


asyncio.run(update_playlist_item_example())

Updating Metadata for a Playlist

import ayt_api
import asyncio


async def update_playlist_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    original_playlist = await api.fetch_playlist("Your Playlist ID")
    print(original_playlist.title)
    updated_playlist = await original_playlist.update(description="New Title")
    print(updated_playlist.title)


asyncio.run(update_playlist_example())

Updating Metadata for a Video

import ayt_api
import asyncio


async def update_video_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    original_video = await api.fetch_video("Video ID", authorised=True)
    print(original_video.title)
    updated_video = await original_video.update(
        title="New Title"
    )
    print(updated_video.title)


asyncio.run(update_video_example())

Fetching a Channel Owned by an Authenticated User

import ayt_api
import asyncio


async def user_channel_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    channel = await api.fetch_user_channel()
    print(channel.title)
    print(channel.handle)


asyncio.run(user_channel_example())

Fetching Playlists Owned by an Authenticated User

import ayt_api
import asyncio


async def user_playlists_example():
    consent_url, sock = ayt_api.AsyncYoutubeAPI.generate_url_and_socket(
        "Your Client ID"
    )
    print(consent_url)
    api = await ayt_api.AsyncYoutubeAPI.with_authcode_receiver(
        consent_url, sock, "Your Client Secret"
    )
    playlists = await api.fetch_user_playlists()
    print([playlist.title for playlist in playlists])


asyncio.run(user_playlists_example())

Fetching Captions from a Video

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def video_captions_example():
    captions = await api.fetch_video_captions("Video ID")
    print(captions[0].video_id)
    print(captions[0].language)
    print(captions[0].is_cc)

loop = asyncio.new_event_loop()
loop.run_until_complete(video_captions_example())

Fetching Comments from a Video

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def video_comments_example():
    video_comments_data = await api.fetch_video_comments("Video ID")
    print(video_comments_data[0].top_level_comment.video_id)
    print(video_comments_data[0].top_level_comment.author_display_name)
    print(video_comments_data[0].top_level_comment.text_original)
    print(video_comments_data[0].top_level_comment.id)
    print(video_comments_data[0].highlight_url)
    print(len(video_comments_data))
    print(video_comments_data[0].call_url)

loop = asyncio.new_event_loop()
loop.run_until_complete(video_comments_example())

Fetching Information on a Video

import asyncio
import ayt_api

api = ayt_api.AsyncYoutubeAPI("Your API Key")


async def video_example():
    video_data = await api.fetch_video("Video ID")
    print(video_data.id)
    print(video_data.channel_id)
    print(video_data.url)
    print(video_data.title)
    print(video_data.thumbnails.default.url)
    print(video_data.visibility)
    print(video_data.duration)
    print(video_data.view_count)
    print(video_data.like_count)
    print(video_data.embed_html)
    print(video_data.published_at)
    print(video_data.description)
    print(video_data.age_restricted)

loop = asyncio.new_event_loop()
loop.run_until_complete(video_example())