|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import httplib2 |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +from apiclient.discovery import build |
| 8 | +from apiclient.errors import HttpError |
| 9 | +from oauth2client.client import flow_from_clientsecrets |
| 10 | +from oauth2client.file import Storage |
| 11 | +from oauth2client.tools import argparser, run_flow |
| 12 | + |
| 13 | +# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
| 14 | +# the OAuth 2.0 information for this application, including its client_id and |
| 15 | +# client_secret. You can acquire an OAuth 2.0 client ID and client secret from |
| 16 | +# the {{ Google Cloud Console }} at |
| 17 | +# {{ https://cloud.google.com/console }}. |
| 18 | +# Please ensure that you have enabled the YouTube Data API for your project. |
| 19 | +# For more information about using OAuth2 to access the YouTube Data API, see: |
| 20 | +# https://developers.google.com/youtube/v3/guides/authentication |
| 21 | +# For more information about the client_secrets.json file format, see: |
| 22 | +# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 23 | +CLIENT_SECRETS_FILE = "client_secrets.json" |
| 24 | + |
| 25 | +# This OAuth 2.0 access scope allows for full read/write access to the |
| 26 | +# authenticated user's account. |
| 27 | +YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" |
| 28 | +YOUTUBE_API_SERVICE_NAME = "youtube" |
| 29 | +YOUTUBE_API_VERSION = "v3" |
| 30 | + |
| 31 | +# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
| 32 | +# missing. |
| 33 | +MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 34 | +WARNING: Please configure OAuth 2.0 |
| 35 | +
|
| 36 | +To make this sample run you will need to populate the client_secrets.json file |
| 37 | +found at: |
| 38 | +
|
| 39 | + %s |
| 40 | +
|
| 41 | +with information from the {{ Cloud Console }} |
| 42 | +{{ https://cloud.google.com/console }} |
| 43 | +
|
| 44 | +For more information about the client_secrets.json file format, please visit: |
| 45 | +https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 46 | +""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 47 | + CLIENT_SECRETS_FILE)) |
| 48 | + |
| 49 | +def get_authenticated_service(args): |
| 50 | + flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, |
| 51 | + scope=YOUTUBE_READ_WRITE_SCOPE, |
| 52 | + message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 53 | + |
| 54 | + storage = Storage("%s-oauth2.json" % sys.argv[0]) |
| 55 | + credentials = storage.get() |
| 56 | + |
| 57 | + if credentials is None or credentials.invalid: |
| 58 | + credentials = run_flow(flow, storage, args) |
| 59 | + |
| 60 | + return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
| 61 | + http=credentials.authorize(httplib2.Http())) |
| 62 | + |
| 63 | +# Create a liveBroadcast resource and set its title, scheduled start time, |
| 64 | +# scheduled end time, and privacy status. |
| 65 | +def insert_broadcast(youtube, options): |
| 66 | + insert_broadcast_response = youtube.liveBroadcasts().insert( |
| 67 | + part="snippet,status", |
| 68 | + body=dict( |
| 69 | + snippet=dict( |
| 70 | + title=options.broadcast_title, |
| 71 | + scheduledStartTime=options.start_time, |
| 72 | + scheduledEndTime=options.end_time |
| 73 | + ), |
| 74 | + status=dict( |
| 75 | + privacyStatus=options.privacy_status |
| 76 | + ) |
| 77 | + ) |
| 78 | + ).execute() |
| 79 | + |
| 80 | + snippet = insert_broadcast_response["snippet"] |
| 81 | + |
| 82 | + print "Broadcast '%s' with title '%s' was published at '%s'." % ( |
| 83 | + insert_broadcast_response["id"], snippet["title"], snippet["publishedAt"]) |
| 84 | + return insert_broadcast_response["id"] |
| 85 | + |
| 86 | +# Create a liveStream resource and set its title, format, and ingestion type. |
| 87 | +# This resource describes the content that you are transmitting to YouTube. |
| 88 | +def insert_stream(youtube, options): |
| 89 | + insert_stream_response = youtube.liveStreams().insert( |
| 90 | + part="snippet,cdn", |
| 91 | + body=dict( |
| 92 | + snippet=dict( |
| 93 | + title=options.stream_title |
| 94 | + ), |
| 95 | + cdn=dict( |
| 96 | + format="1080p", |
| 97 | + ingestionType="rtmp" |
| 98 | + ) |
| 99 | + ) |
| 100 | + ).execute() |
| 101 | + |
| 102 | + snippet = insert_stream_response["snippet"] |
| 103 | + |
| 104 | + print "Stream '%s' with title '%s' was inserted." % ( |
| 105 | + insert_stream_response["id"], snippet["title"]) |
| 106 | + return insert_stream_response["id"] |
| 107 | + |
| 108 | +# Bind the broadcast to the video stream. By doing so, you link the video that |
| 109 | +# you will transmit to YouTube to the broadcast that the video is for. |
| 110 | +def bind_broadcast(youtube, broadcast_id, stream_id): |
| 111 | + bind_broadcast_response = youtube.liveBroadcasts().bind( |
| 112 | + part="id,contentDetails", |
| 113 | + id=broadcast_id, |
| 114 | + streamId=stream_id |
| 115 | + ).execute() |
| 116 | + |
| 117 | + print "Broadcast '%s' was bound to stream '%s'." % ( |
| 118 | + bind_broadcast_response["id"], |
| 119 | + bind_broadcast_response["contentDetails"]["boundStreamId"]) |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + argparser.add_argument("--broadcast-title", help="Broadcast title", |
| 123 | + default="New Broadcast") |
| 124 | + argparser.add_argument("--privacy-status", help="Broadcast privacy status", |
| 125 | + default="private") |
| 126 | + argparser.add_argument("--start-time", help="Scheduled start time", |
| 127 | + default='2014-01-30T00:00:00.000Z') |
| 128 | + argparser.add_argument("--end-time", help="Scheduled end time", |
| 129 | + default='2014-01-31T00:00:00.000Z') |
| 130 | + argparser.add_argument("--stream-title", help="Stream title", |
| 131 | + default="New Stream") |
| 132 | + args = argparser.parse_args() |
| 133 | + |
| 134 | + youtube = get_authenticated_service(args) |
| 135 | + try: |
| 136 | + broadcast_id = insert_broadcast(youtube, args) |
| 137 | + stream_id = insert_stream(youtube, args) |
| 138 | + bind_broadcast(youtube, broadcast_id, stream_id) |
| 139 | + except HttpError, e: |
| 140 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
0 commit comments