Skip to content

Instantly share code, notes, and snippets.

@lokeshh
Created July 27, 2017 07:09
Show Gist options
  • Select an option

  • Save lokeshh/0bc65780c9bf55c58b2b41fc63db4aa6 to your computer and use it in GitHub Desktop.

Select an option

Save lokeshh/0bc65780c9bf55c58b2b41fc63db4aa6 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
gem 'google-api-client', '0.8'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
# This OAuth 2.0 access scope allows for read-only access to the authenticated
# user's account, but not other types of account access.
YOUTUBE_READONLY_SCOPE = 'https://www.googleapis.com/auth/youtube'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
def get_authenticated_service
client = Google::APIClient.new(
:application_name => $PROGRAM_NAME,
:application_version => '1.0.0'
)
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
file_storage = Google::APIClient::FileStorage.new("#{$PROGRAM_NAME}-oauth2.json")
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
flow = Google::APIClient::InstalledAppFlow.new(
:client_id => client_secrets.client_id,
:client_secret => client_secrets.client_secret,
:scope => [YOUTUBE_READONLY_SCOPE]
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
return client, youtube
end
# Create a liveBroadcast resource and set its title, scheduled start time,
# scheduled end time, and privacy status.
def insert_broadcast(client, youtube, options)
p options[:start_time]
insert_broadcast_response = client.execute!(
api_method: youtube.live_broadcasts.insert,
parameters: {
part: 'snippet,status'
},
body_object: {
snippet: {
title: options[:broadcast_title],
scheduledStartTime: options[:state_time],
scheduledEndTime: options[:end_time]
},
status: {
privacytatus: options[:privacy_status]
}
}
)
p "Broadcast: #{insert_broadcast_response.data.id}"
return insert_broadcast_response.id
end
# Create a liveStream resource and set its title, format, and ingestion type.
# This resource describes the content that you are transmitting to YouTube.
def insert_stream(client, youtube, options)
insert_stream_response = client.execute!(
api_method: youtube.live_streams.insert,
parameters: {
part: "snippet,cdn",
},
body_object: {
snippet: {
title: options[:stream_title]
},
cdn: {
format: "1080p",
ingestionType: "rtmp"
}
}
)
p "Stream: #{insert_stream_response.data.id}"
return insert_stream_response.data.id
end
options = {
stream_title: 'stream',
broadcast_title: 'dumdum',
start_time: '2018-01-30T00:00:00.000Z',
end_time: '2018-01-30T00:01:00.000Z',
privacy_status: 'private'
}
client, youtube = get_authenticated_service
# stream_id = insert_stream(client, youtube, options)
broadcast_id = insert_broadcast(client, youtube, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment