The code imports necessary libraries, loads credentials from a service account JSON file, and sets up a YouTube API client using the googleapiclient library. The live_stream function is defined to create a new live stream on YouTube, utilizing the API client and specifying parameters such as title, description, and scheduled start time.
npm run import -- "create a live streaming request"import os
from googleapiclient.discovery import build
from google.oauth2 import service_account
HOME_DIR = os.environ.get("HOME") or os.environ.get("USERPROFILE")
# Load credentials (Replace with your service account JSON file)
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
SERVICE_ACCOUNT_FILE = os.path.join(HOME_DIR, '.credentials', "oval-silicon-449118-t6-841e34d683cf.json") # Replace with actual JSON file
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
youtube = build("youtube", "v3", credentials=credentials)
def live_stream():
# Insert a new livestream
request = youtube.liveBroadcasts().insert(
part="snippet,status,contentDetails",
body={
"snippet": {
"title": "My Live Stream",
"description": "Live streaming via Python",
"scheduledStartTime": "2025-02-26T12:00:00Z", # Schedule in ISO format
"channelId": "UCPaZDuwY1sJOb5l-QHm9mDw"
},
"status": {"privacyStatus": "public"},
"contentDetails": {"enableAutoStart": True},
},
)
response = request.execute()
print("Live Stream Created:", response["id"])
__all__ = {
"live_stream": live_stream
}
import os
from googleapiclient.discovery import build
from google.oauth2 import service_accountThe code begins by importing the necessary libraries:
os: For interacting with the operating system and environment variables.googleapiclient.discovery: For building clients for Google APIs.google.oauth2.service_account: For authenticating with Google APIs using service accounts.It then loads credentials from a service account JSON file using environment variables. The HOME_DIR variable is set to the user's home directory, and the service account file is loaded from a specific path within that directory.
The API client is set up using the build function from googleapiclient.discovery, with the YouTube API in version 3. The credentials object is created using the service account file and scopes.
The live_stream function is defined to create a new live stream on YouTube. It uses the youtube.liveBroadcasts().insert method to create a new broadcast, specifying the title, description, scheduled start time, and channel ID. The function also sets the privacy status to public and enables auto-start. The response from the API is then printed to the console.
Finally, the __all__ variable is set to export the live_stream function as a module variable.