The code imports necessary libraries, defines constants and two functions (create_livestream
and sanitize_filename
), and exports the create_livestream
function for use. The create_livestream
function creates a new live stream on YouTube by authorizing the user, building a YouTube API client, and inserting the live stream details, while the sanitize_filename
function replaces invalid characters in a string with underscores.
npm run import -- "create live stream"
import os
import re
import json
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import_notebook("list live stream",
"globals("))
SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
HOME_DIR = os.environ.get("HOME") or os.environ.get("USERPROFILE")
def create_livestream(youtube):
credentials = youtube_authorize()
# Build the YouTube API client
youtube = build("youtube", "v3", credentials=credentials)
print(youtube)
request = youtube.liveStreams().insert(
part="snippet,cdn,status",
body={
"snippet": {
"title": "My Live Stream",
"description": "Streaming via API"
},
"cdn": {
"frameRate": "30fps",
"resolution": "1080p",
"ingestionType": "rtmp"
},
"status": {
"streamStatus": "active"
}
}
)
response = request.execute()
return response['cdn']['ingestionInfo']['streamName']
def sanitize_filename(name):
"""Sanitize a string to be a valid filename."""
return re.sub(r"[^\w.-]", "_", name) # Replace invalid characters with "_"
__all__ = {
"create_livestream": create_livestream
}
import os
import re
import json
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import google.auth
import google.auth.transport.requests
def create_livestream(youtube):
"""
Creates a new live stream on YouTube using the provided YouTube API credentials.
Args:
youtube (googleapiclient.discovery.Resource): The YouTube API client.
Returns:
str: The stream name of the newly created live stream.
"""
credentials = youtube_authorize()
try:
youtube = build("youtube", "v3", credentials=credentials)
request = youtube.liveStreams().insert(
part="snippet,cdn,status",
body={
"snippet": {
"title": "My Live Stream",
"description": "Streaming via API"
},
"cdn": {
"frameRate": "30fps",
"resolution": "1080p",
"ingestionType": "rtmp"
},
"status": {
"streamStatus": "active"
}
}
)
response = request.execute()
return response['cdn']['ingestionInfo']['streamName']
except Exception as e:
print(f"Error creating live stream: {e}")
return None
The code starts by importing necessary libraries:
os
: for interacting with the operating systemre
: for regular expression matching operationsjson
: not used in this code snippet, but imported for potential usegoogle.oauth2.credentials
: for handling Google OAuth credentialsgoogleapiclient.discovery
: for building Google API clientsimport_notebook
: a custom function for importing code from a Jupyter notebook (likely used for loading the list live stream
function)The code defines two constants:
SCOPES
: a list of Google API scopes, specifically for the YouTube APIHOME_DIR
: the home directory of the user, determined by checking environment variables HOME
and USERPROFILE
The code defines two functions:
create_livestream
This function takes a youtube
object as input and returns the stream name of the newly created live stream.
youtube_authorize
function (not shown in this snippet) and gets the credentials.build
function from googleapiclient.discovery
.liveStreams().insert
method, specifying the stream details (title, description, frame rate, resolution, and ingestion type).sanitize_filename
This function takes a string as input and returns a sanitized version of it, replacing any invalid characters with underscores.
The code exports the create_livestream
function as part of the __all__
dictionary, making it available for import.
The youtube_authorize
function is not shown in this snippet and is assumed to handle the authorization process for the YouTube API.