This code binds a live stream to a YouTube broadcast using the YouTube API, utilizing functions to import necessary modules and notebooks, authorize API credentials, retrieve broadcast and stream information, and execute the binding request. The bind_stream_to_broadcast function returns the stream information and broadcast ID, and is exported for use in other modules.
npm run import -- "bind youtube stream"import_notebook("list broadcasts",
"globals("))
import_notebook("list live stream",
"globals("))
from googleapiclient.discovery import build
def bind_stream_to_broadcast():
credentials = youtube_authorize()
broadcast_id = list_broadcasts()[0]["id"]
stream = list_livestream()
print(stream['cdn']['ingestionInfo']['streamName'])
# Build the YouTube API client
youtube = build("youtube", "v3", credentials=credentials)
request = youtube.liveBroadcasts().bind(
id=broadcast_id,
part="id,contentDetails",
streamId=stream["id"]
)
response = request.execute()
print("✅ Stream successfully bound to broadcast:", response)
return stream, broadcast_id
__all__ = {
"bind_stream_to_broadcast": bind_stream_to_broadcast
}
stream, broadcast_id = bind_stream_to_broadcast()import_notebook("list broadcasts", globals())
import_notebook("list live stream", globals())
from googleapiclient.discovery import build
import_notebook function is used to import contents from notebooks, and the globals() function is used to access the global scope. This is likely used to dynamically import functions from notebooks named "list broadcasts" and "list live stream".googleapiclient.discovery module is imported, specifically the build function, which is used to create a client instance for the YouTube API.def bind_stream_to_broadcast():
credentials = youtube_authorize()
youtube_authorize function is called to obtain YouTube API credentials.broadcast_id = list_broadcasts()[0]["id"]
stream = list_livestream()
print(stream['cdn']['ingestionInfo']['streamName'])
list_broadcasts function is called to retrieve a list of broadcasts, and the ID of the first broadcast is extracted.list_livestream function is called to retrieve live stream information, and the stream name is printed.youtube = build("youtube", "v3", credentials=credentials)
request = youtube.liveBroadcasts().bind(
id=broadcast_id,
part="id,contentDetails",
streamId=stream["id"]
)
response = request.execute()
print("✅ Stream successfully bound to broadcast:", response)
build function, passing the service name, API version, and credentials.bind method of the liveBroadcasts resource is used to bind the stream to the broadcast, passing the broadcast ID, the id and contentDetails parts, and the stream ID.execute method is called to execute the request, and the response is printed.return stream, broadcast_id
__all__ = {
"bind_stream_to_broadcast": bind_stream_to_broadcast
}
bind_stream_to_broadcast function is exported, making it available for import in other modules.