หมายเหตุ: Content ID API ของ YouTube มีไว้เพื่อการใช้งานโดยพาร์ทเนอร์เนื้อหาของ YouTube และนักพัฒนาซอฟต์แวร์บางรายหรือผู้ใช้ YouTube บางส่วนอาจเข้าถึงไม่ได้ หากคุณไม่เห็น API ของ Content ID ของ YouTube เป็นหนึ่งในบริการที่ระบุไว้ในคอนโซล Google API โปรดดูศูนย์ช่วยเหลือของ YouTube เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับโปรแกรมพาร์ทเนอร์ YouTube
ตัวอย่างโค้ดนี้แสดงวิธีอัปโหลดวิดีโอ YouTube และใช้นโยบายการสร้างรายได้กับวิดีโอดังกล่าว หากต้องการสร้างรายได้จากวิดีโอ คุณต้องอ้างสิทธิ์วิดีโอด้วย asset ในระบบการจัดการสิทธิ์ของ YouTube ตัวอย่างจะอัปโหลดวิดีโอ สร้างเนื้อหาใหม่ อ้างสิทธิ์วิดีโอโดยใช้เนื้อหา และนำนโยบายการสร้างรายได้ไปใช้กับวิดีโอ
ตัวอย่างนี้แสดงเป็นชุดขั้นตอนที่เกี่ยวข้องพร้อมด้วยส่วนที่เกี่ยวข้องของโค้ด คุณดูสคริปต์ทั้งหมดได้ที่ส่วนท้ายของหน้านี้ ซึ่งเขียนด้วย Python นอกจากนี้ยังมีไลบรารีไคลเอ็นต์สำหรับภาษาโปรแกรมยอดนิยมอื่นๆ ให้บริการด้วย
ข้อกำหนด
- Python 2.5 ขึ้นไป
- google-api-python-client
ขั้นตอนที่ 1: ฟังก์ชันทั่วไปในการทำความสะอาด
ส่วนแรกของตัวอย่างโค้ดจะทำหน้าที่ทำความสะอาดพื้นฐานทั่วไปในสคริปต์ต่างๆ ได้แก่ แยกวิเคราะห์บรรทัดคำสั่ง ตรวจสอบสิทธิ์ผู้ใช้ และรับบริการ API ที่จำเป็น
แยกวิเคราะห์บรรทัดคำสั่ง
เมธอด parse_options ใช้ OptionParser จากไลบรารีของไคลเอ็นต์ Python เพื่อสร้างออบเจ็กต์ options ที่มีอาร์กิวเมนต์บรรทัดคำสั่งแต่ละรายการเป็นพร็อพเพอร์ตี้ เมธอดต่อมาจะดึงค่าจากออบเจ็กต์ options ตามความจำเป็น
อาร์กิวเมนต์บรรทัดคำสั่งของสคริปต์ตัวอย่างแสดงอยู่ในรายการด้านล่าง ต้องระบุ 2 รายการแรก (file และ channelId) ส่วนที่เหลือเป็นตัวเลือก
-
file: ชื่อและตำแหน่งของไฟล์วิดีโอที่จะอัปโหลดExample: --file="/home/path/to/file.mov"
-
channelId: ช่อง YouTube ที่คุณต้องการอัปโหลดวิดีโอ ช่องต้องได้รับการจัดการโดยบัญชีระบบจัดการเนื้อหาของ YouTube ของผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์ คุณเรียกดูรหัสช่องได้ในการตั้งค่าบัญชี YouTube สำหรับผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว หรือโดยใช้เมธอดchannels.listExample: --channelId="UC_x5XG1OV2P6uZZ5FSM9Ttw"
-
title: ชื่อของวิดีโอที่คุณกำลังอัปโหลด ค่าเริ่มต้นคือTest titleExample: --title="Summer vacation in California"
-
description: คำอธิบายของวิดีโอที่คุณกำลังอัปโหลด ค่าเริ่มต้นคือTest descriptionExample: --description="Had a great time surfing in Santa Cruz"
-
category: รหัสหมวดหมู่สำหรับหมวดหมู่วิดีโอ YouTube ที่เกี่ยวข้องกับวิดีโอ ค่าเริ่มต้นคือ22ซึ่งอ้างถึงหมวดหมู่People & BlogsExample: --category=22
-
keywords: รายการคีย์เวิร์ดที่คั่นด้วยคอมมาซึ่งเชื่อมโยงกับวิดีโอ ค่าเริ่มต้นคือสตริงว่างเปล่าExample: --keywords="surfing, beach volleyball"
-
privacyStatus: สถานะความเป็นส่วนตัวของวิดีโอ ลักษณะการทำงานเริ่มต้นกำหนดให้วิดีโอที่อัปโหลดแสดงต่อสาธารณะ (public) เมื่ออัปโหลดวิดีโอทดสอบ คุณอาจต้องการระบุค่าอาร์กิวเมนต์--privacyStatusเพื่อให้แน่ใจว่าวิดีโอเหล่านั้นเป็นแบบส่วนตัวหรือไม่เป็นสาธารณะ ค่าที่ถูกต้องคือpublic,privateและunlistedExample: --privacyStatus="private"
-
policyId: นโยบายการสร้างรายได้ที่จะนำไปใช้กับวิดีโอที่อัปโหลด นโยบายต้องเชื่อมโยงกับบัญชีระบบจัดการเนื้อหาของ YouTube ของผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว ค่าเริ่มต้นคือ "สร้างรายได้" แบบมาตรฐานของ YouTubeExample: --policyId="S309961703555739"
def parse_options(): parser = OptionParser() parser.add_option("--file", dest="file", help="Video file to upload") parser.add_option("--title", dest="title", help="Video title", default="Test Title") parser.add_option("--description", dest="description", help="Video description", default="Test Description") parser.add_option("--category", dest="category", help="Numeric video category. " + "See https://developers.google.com/youtube/v3/docs/videoCategories/list", default="22") parser.add_option("--keywords", dest="keywords", help="Video keywords, comma separated", default="") parser.add_option("--privacyStatus", dest="privacyStatus", help="Video privacy status: public, private or unlisted", default="public") parser.add_option("--policyId", dest="policyId", help="Optional id of a saved claim policy") parser.add_option("--channelId", dest="channelId", help="Id of the channel to upload to. Must be managed by your CMS account") (options, args) = parser.parse_args() return options
ให้สิทธิ์คำขอ
ในขั้นตอนนี้ เราได้รวมการให้สิทธิ์ OAuth 2.0 ไว้ในสคริปต์ วิธีนี้ทำให้ผู้ใช้สามารถเรียกใช้สคริปต์เพื่อให้สิทธิ์สคริปต์ในการส่งคำขอ API ที่มาจากบัญชีของผู้ใช้
สร้างไฟล์ client_secrets.json
ประเภทการให้สิทธิ์ที่แสดงในตัวอย่างต้องการไฟล์ client_secrets.json ซึ่งมีข้อมูลจากคอนโซล Google API เพื่อดำเนินการให้สิทธิ์ และจะต้องลงทะเบียนแอปพลิเคชันด้วย สำหรับคำอธิบายที่สมบูรณ์ยิ่งขึ้นเกี่ยวกับวิธีการทำงานของการให้สิทธิ์ โปรดดูที่คู่มือการให้สิทธิ์ โปรดทราบว่าตัวอย่างนี้กำหนดให้ต้องกำหนดค่าบริการ API ข้อมูลของ YouTube V3 และบริการ API สำหรับ Content ID ของ YouTube ในคอนโซล API สำหรับโปรเจ็กต์ของคุณ
{ "web": { "client_id": "INSERT CLIENT ID HERE", "client_secret": "INSERT CLIENT SECRET HERE", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
รหัสการให้สิทธิ์ในสคริปต์
สคริปต์มีคำสั่ง import ต่อไปนี้เพื่อเปิดใช้การตรวจสอบสิทธิ์และการให้สิทธิ์ผู้ใช้
from oauth2client.file import Storage from oauth2client.client import flow_from_clientsecrets from oauth2client.tools import run
ถัดไป เมธอด get_authenticated_services จะสร้างออบเจ็กต์ FLOW โดยใช้ข้อมูลจากไฟล์ client_secrets.json ที่กําหนดค่าไว้ในขั้นตอนก่อนหน้า หากผู้ใช้อนุญาตให้แอปพลิเคชันของเราส่งคำขอ API ในนามของผู้ใช้ ข้อมูลเข้าสู่ระบบที่ได้จะเก็บอยู่ในออบเจ็กต์ Storage เพื่อใช้ในภายหลัง ผู้ใช้ต้องให้สิทธิ์แอปพลิเคชันของเราอีกครั้งหากข้อมูลเข้าสู่ระบบหมดอายุ
YOUTUBE_SCOPES = ( # An OAuth 2 access scope that allows for full read/write access. "https://www.googleapis.com/auth/youtube", # A scope that grants access to YouTube Partner API functionality. "https://www.googleapis.com/auth/youtubepartner") flow = flow_from_clientsecrets( CLIENT_SECRETS_FILE, scope=" ".join(YOUTUBE_SCOPES), message=MISSING_CLIENT_SECRETS_MESSAGE ) storage = Storage(CACHED_CREDENTIALS_FILE) credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(flow, storage)
รับบริการ
หลังจากการให้สิทธิ์เสร็จสมบูรณ์แล้ว เราจะได้รับบริการที่จำเป็นสำหรับการดำเนินการที่ต้องการ ตัวอย่างดังกล่าวใช้ API ข้อมูลของ YouTube ในการอัปโหลดวิดีโอและใช้ API สำหรับ Content ID ของ YouTube เพื่อสร้างเนื้อหาและอ้างสิทธิ์วิดีโอ เราสร้างบริการแยกต่างหากเพื่อให้มีสิทธิ์เข้าถึงฟังก์ชันการทำงานของ API ทั้งสองแบบที่ได้รับอนุญาต
from googleapiclient.discovery import build import httplib2 YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" YOUTUBE_CONTENT_ID_API_SERVICE_NAME = "youtubePartner" YOUTUBE_CONTENT_ID_API_VERSION = "v1" youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http())) youtube_partner = build(YOUTUBE_CONTENT_ID_API_SERVICE_NAME, YOUTUBE_CONTENT_ID_API_VERSION, http=credentials.authorize(httplib2.Http()), static_discovery=False) return (youtube, youtube_partner)
ขั้นตอนที่ 2: ระบุเจ้าของเนื้อหา
หากต้องการสร้างเนื้อหาและอ้างสิทธิ์ ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วต้องมีบัญชีระบบจัดการเนื้อหาของ YouTube บัญชีระบบจัดการเนื้อหามีออบเจ็กต์การจัดการสิทธิ์สำหรับเจ้าของเนื้อหาอย่างน้อย 1 ราย โดยเจ้าของเนื้อหาจะเป็นผู้ถือครองลิขสิทธิ์ที่มีสิทธิตัดสินใจว่าจะสร้างรายได้ ติดตาม หรือบล็อกวิดีโอ
เมธอด get_content_owner จะเรียกรหัสของเจ้าของเนื้อหาในบัญชีระบบจัดการเนื้อหาของผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว บัญชีส่วนใหญ่มีเจ้าของเนื้อหารายเดียว (ผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว) แต่หากบัญชีมีเจ้าของเนื้อหาหลายราย เมธอดจะแสดงรายการแรก
def get_content_owner_id(youtube_partner): try: content_owners_list_response = youtube_partner.contentOwners().list( fetchMine=True ).execute() except HttpError, e: if INVALID_CREDENTIALS in e.content: logging.error("The request is not authorized by a Google Account that " "is linked to a YouTube content owner. Please delete '%s' and " "re-authenticate with a YouTube content owner account." % CACHED_CREDENTIALS_FILE) exit(1) else: raise # This returns the CMS user id of the first entry returned # by youtubePartner.contentOwners.list() # See https://developers.google.com/youtube/partner/reference/rest/v1/contentOwners/list # Normally this is what you want, but if you authorize with a Google Account # that has access to multiple YouTube content owner accounts, you need to # iterate through the results. return content_owners_list_response["items"][0]["id"]
ขั้นตอนที่ 3: อัปโหลดวิดีโอ
ในการอัปโหลดวิดีโอ เราจะสร้างทรัพยากร JSON บางส่วนที่แสดงถึงวิดีโอและส่งไปยังเมธอด videos.insert เราตั้งค่าข้อมูลเมตาของวิดีโอโดยใช้ค่าจากออบเจ็กต์ options ที่สร้างขึ้นเมื่อเราแยกวิเคราะห์บรรทัดคำสั่ง สำหรับไฟล์สื่อนั้น เราจะใช้ MediaFileUpload เพื่อให้ใช้การอัปโหลดที่ดำเนินการต่อได้ ดูรายละเอียดเพิ่มเติมได้ที่การอัปโหลดวิดีโอ
เมธอด upload จะแสดงรหัสวิดีโอสำหรับวิดีโอใหม่ และสคริปต์จะต้องส่งค่าดังกล่าวไปยังวิธีอื่นๆ ในขั้นตอนถัดไป
def upload(youtube, content_owner_id, options): if options.keywords: tags = options.keywords.split(",") else: tags = None insert_request = youtube.videos().insert( onBehalfOfContentOwner=content_owner_id, onBehalfOfContentOwnerChannel=options.channelId, part="snippet,status", body=dict( snippet=dict( title=options.title, description=options.description, tags=tags, categoryId=options.category ), status=dict( privacyStatus=options.privacyStatus ) ), # chunksize=-1 means that the entire file will be uploaded in a single # HTTP request. (If the upload fails, it will still be retried where it # left off.) This is usually a best practice, but if you're using Python # older than 2.6 or if you're running on App Engine, you should set the # chunksize to something like 1024 * 1024 (1 megabyte). media_body=MediaFileUpload(options.file, chunksize=-1, resumable=True) ) response = None error = None retry = 0 duration_seconds=0 while response is None: try: logging.debug("Uploading file...") start_seconds = time.time() status, response = insert_request.next_chunk() delta_seconds = time.time() - start_seconds duration_seconds += delta_seconds if "id" in response: return (response["id"], duration_seconds) else: logging.error("The upload failed with an unexpected response: %s" % response) exit(1) except HttpError, e: if e.resp.status in RETRIABLE_STATUS_CODES: error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status, e.content) else: raise except RETRIABLE_EXCEPTIONS, e: error = "A retriable error occurred: %s" % e if error is not None: logging.error(error) retry += 1 if retry > MAX_RETRIES: logging.error("No longer attempting to retry.") exit(1) max_sleep = 2 ** retry sleep_seconds = random.random() * max_sleep logging.debug("Sleeping %f seconds and then retrying..." % sleep_seconds) time.sleep(sleep_seconds)
ขั้นตอนที่ 4: สร้างเนื้อหา
หากต้องการสร้างรายได้จากวิดีโอ YouTube คุณต้องเชื่อมโยงวิดีโอนั้นกับเนื้อหาก่อน เมธอด create_asset จะสร้างชิ้นงานใหม่สำหรับวิดีโอที่อัปโหลดใหม่
เช่นเดียวกับวิดีโอที่ทำกับวิดีโอ เราสร้างทรัพยากร JSON บางส่วนที่ระบุประเภทของเนื้อหาที่จะสร้าง (วิดีโอบนเว็บ) และระบุชื่อและคำอธิบายสำหรับเนื้อหาใหม่ เราส่งทรัพยากร JSON ไปยังเมธอด assets.insert ซึ่งจะสร้างเนื้อหาและแสดงรหัสที่ไม่ซ้ำกัน ขอย้ำอีกครั้งว่าสคริปต์จะต้องส่งค่าดังกล่าวไปยังวิธีอื่นในขั้นตอนถัดไป
def create_asset(youtube_partner, content_owner_id, title, description): # This creates a new asset corresponding to a video on the web. # The asset is linked to the corresponding YouTube video via a # claim that will be created later. body = dict( type="web", metadata=dict( title=title, description=description ) ) assets_insert_response = youtube_partner.assets().insert( onBehalfOfContentOwner=content_owner_id, body=body ).execute() return assets_insert_response["id"]
ขั้นตอนที่ 5: อัปเดตการเป็นเจ้าของ
ทั้งนี้ YouTube ต้องทราบว่าใครเป็นเจ้าของเนื้อหาที่เกี่ยวข้องก่อนจึงจะสร้างรายได้จากวิดีโอได้ ดังนั้นด้วยเนื้อหาที่สร้างขึ้น ตอนนี้เราจึงกำหนดค่า ownership ของเนื้อหา ในตัวอย่าง เราระบุว่าเจ้าของเนื้อหาเป็นเจ้าของเนื้อหาทั่วโลก
def set_asset_ownership(youtube_partner, content_owner_id, asset_id): # This specifies that content_owner_id owns 100% of the asset worldwide. # Adjust as needed. body = dict( general=[dict( owner=content_owner_id, ratio=100, type="exclude", territories=[] )] ) youtube_partner.ownership().update( onBehalfOfContentOwner=content_owner_id, assetId=asset_id, body=body ).execute()
ขั้นตอนที่ 6: อ้างสิทธิ์วิดีโอ
ขั้นตอนถัดไปคือการเชื่อมโยงวิดีโอที่อัปโหลดกับเนื้อหาที่เกี่ยวข้องโดยการอ้างสิทธิ์วิดีโอ การอ้างสิทธิ์ให้ความเชื่อมโยงระหว่างวิดีโอกับระบบจัดการสิทธิ์ของ YouTube ซึ่งจะระบุการเป็นเจ้าของวิดีโอและช่วยให้เจ้าของสามารถกำหนดนโยบายการสร้างรายได้
เมธอด claim_video จะอ้างสิทธิ์ภาพและเสียง หากคุณใส่พารามิเตอร์ policyId ในบรรทัดคำสั่ง เมธอดจะใช้นโยบายที่ระบุกับวิดีโอ หากคุณไม่ได้ใส่พารามิเตอร์ ระบบจะใช้ "สร้างรายได้" แบบมาตรฐาน
def claim_video(youtube_partner, content_owner_id, asset_id, video_id, policy_id): # policy_id can be set to the id of an existing policy, which can be obtained # via youtubePartner.policies.list() # See https://developers.google.com/youtube/partner/reference/rest/v1/policies/list # If you later update that existing policy, the claim will also be updated. if policy_id: policy = dict( id=policy_id ) # If policy_id is not provided, a new inline policy is created. else: policy = dict( rules=[dict( action="monetize" )] ) body = dict( assetId=asset_id, videoId=video_id, policy=policy, contentType="audiovisual" ) youtube_partner.claims().insert( onBehalfOfContentOwner=content_owner_id, body=body ).execute()
ขั้นตอนที่ 7: ตั้งค่าตัวเลือกการโฆษณา
เราได้อ้างสิทธิ์วิดีโอและใช้นโยบายการสร้างรายได้กับวิดีโอนั้นแล้ว ขั้นตอนสุดท้ายคือการระบุประเภทโฆษณาที่จะแสดงในวิดีโอ เมื่อใดก็ตามที่ "สร้างรายได้" ตามนโยบายแล้ว YouTube จะตรวจสอบตัวเลือกการโฆษณา และแสดงโฆษณาประเภทที่สร้างรายได้สูงสุดที่มี
ตัวอย่างนี้บอกให้ YouTube แสดงโฆษณาในสตรีมของ TrueView กับวิดีโอนี้
def set_advertising_options(youtube_partner, content_owner_id, video_id): # This enables the TrueView ad format for the given video. # Adjust as needed. body = dict( adFormats=["trueview_instream"] ) youtube_partner.videoAdvertisingOptions().update( videoId=video_id, onBehalfOfContentOwner=content_owner_id, body=body ).execute()
ตัวอย่างโค้ดที่สมบูรณ์
ตัวอย่าง upload_monetize_video_example.py ที่ใช้งานได้ที่สมบูรณ์แสดงอยู่ด้านล่าง
#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (C) 2013 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Simple command-line sample for Youtube Partner API. Command-line application that creates an asset, uploads and claims a video for that asset. Usage: $ python upload_monetize_video_example.py --file=VIDEO_FILE --channelID=CHANNEL_ID \ [--title=VIDEO_TITLE] [--description=VIDEO_DESCRIPTION] [--category=CATEGORY_ID] \ [--keywords=KEYWORDS] [--privacyStatus=PRIVACY_STATUS] [--policyId=POLICY_ID] You can also get help on all the command-line flags the program understands by running: $ python upload_monetize_video_example.py --help """ __author__ = 'jeffy+pub@google.com (Jeffrey Posnick)' import httplib import httplib2 import logging import os import random import sys import time from apiclient.discovery import build from apiclient.errors import HttpError from apiclient.http import MediaFileUpload from oauth2client.file import Storage from oauth2client.client import flow_from_clientsecrets from oauth2client.tools import run from optparse import OptionParser # Explicitly tell the underlying HTTP transport library not to retry, since # we are handling retry logic ourselves. httplib2.RETRIES = 1 # Maximum number of times to retry before giving up. MAX_RETRIES = 10 # Always retry when these exceptions are raised. RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected, httplib.IncompleteRead, httplib.ImproperConnectionState, httplib.CannotSendRequest, httplib.CannotSendHeader, httplib.ResponseNotReady, httplib.BadStatusLine,) # Always retry when an apiclient.errors.HttpError with one of these status # codes is raised. RETRIABLE_STATUS_CODES = (500, 502, 503, 504,) # The message associated with the HTTP 401 error that's returned when a request # is authorized by a user whose account is not associated with a YouTube # content owner. INVALID_CREDENTIALS = "Invalid Credentials" # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains # the OAuth 2.0 information for this application, including its client_id and # client_secret. You can acquire an OAuth 2.0 client ID and client secret from # the Google API Console at # https://console.cloud.google.com/. # See the "Registering your application" instructions for an explanation # of how to find these values: # https://developers.google.com/youtube/partner/guides/registering_an_application # For more information about using OAuth2 to access Google APIs, please visit: # https://developers.google.com/accounts/docs/OAuth2 # For more information about the client_secrets.json file format, please visit: # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets CLIENT_SECRETS_FILE = "client_secrets.json" # The local file used to store the cached OAuth 2 credentials after going # through a one-time browser-based login. CACHED_CREDENTIALS_FILE = "%s-oauth2.json" % sys.argv[0] YOUTUBE_SCOPES = ( # An OAuth 2 access scope that allows for full read/write access. "https://www.googleapis.com/auth/youtube", # A scope that grants access to YouTube Partner API functionality. "https://www.googleapis.com/auth/youtubepartner",) YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" YOUTUBE_CONTENT_ID_API_SERVICE_NAME = "youtubePartner" YOUTUBE_CONTENT_ID_API_VERSION = "v1" # Helpful message to display if the CLIENT_SECRETS_FILE is missing. MISSING_CLIENT_SECRETS_MESSAGE = """ WARNING: Please configure OAuth 2.0 To make this sample run you need to populate the client_secrets.json file at: %s with information from the API Console https://console.cloud.google.com/ For more information about the client_secrets.json file format, please visit: https://developers.google.com/api-client-library/python/guide/aaa_client_secrets """ % os.path.abspath(os.path.join(os.path.dirname(__file__), CLIENT_SECRETS_FILE)) def parse_options(): parser = OptionParser() parser.add_option("--file", dest="file", help="Video file to upload") parser.add_option("--title", dest="title", help="Video title", default="Test Title") parser.add_option("--description", dest="description", help="Video description", default="Test Description") parser.add_option("--category", dest="category", help="Numeric video category. " + "See https://developers.google.com/youtube/v3/docs/videoCategories/list", default="22") parser.add_option("--keywords", dest="keywords", help="Video keywords, comma separated", default="") parser.add_option("--privacyStatus", dest="privacyStatus", help="Video privacy status: public, private or unlisted", default="public") parser.add_option("--policyId", dest="policyId", help="Optional id of a saved claim policy") parser.add_option("--channelId", dest="channelId", help="Id of the channel to upload to. Must be managed by your CMS account") (options, args) = parser.parse_args() return options def get_authenticated_services(): flow = flow_from_clientsecrets( CLIENT_SECRETS_FILE, scope=" ".join(YOUTUBE_SCOPES), message=MISSING_CLIENT_SECRETS_MESSAGE ) storage = Storage(CACHED_CREDENTIALS_FILE) credentials = storage.get() if credentials is None or credentials.invalid: credentials = run(flow, storage) youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http())) youtube_partner = build(YOUTUBE_CONTENT_ID_API_SERVICE_NAME, YOUTUBE_CONTENT_ID_API_VERSION, http=credentials.authorize(httplib2.Http()), static_discovery=False) return (youtube, youtube_partner) def get_content_owner_id(youtube_partner): try: content_owners_list_response = youtube_partner.contentOwners().list( fetchMine=True ).execute() except HttpError, e: if INVALID_CREDENTIALS in e.content: logging.error("Your request is not authorized by a Google Account that " "is associated with a YouTube content owner. Please delete '%s' and " "re-authenticate with an account that is associated " "with a content owner." % CACHED_CREDENTIALS_FILE) exit(1) else: raise # This returns the CMS user id of the first entry returned # by youtubePartner.contentOwners.list() # See https://developers.google.com/youtube/partner/reference/rest/v1/contentOwners/list # Normally this is what you want, but if you authorize with a Google Account # that has access to multiple YouTube content owner accounts, you need to # iterate through the results. return content_owners_list_response["items"][0]["id"] def upload(youtube, content_owner_id, options): if options.keywords: tags = options.keywords.split(",") else: tags = None insert_request = youtube.videos().insert( onBehalfOfContentOwner=content_owner_id, onBehalfOfContentOwnerChannel=options.channelId, part="snippet,status", body=dict( snippet=dict( title=options.title, description=options.description, tags=tags, categoryId=options.category ), status=dict( privacyStatus=options.privacyStatus ) ), # chunksize=-1 means that the entire file will be uploaded in a single # HTTP request. (If the upload fails, it will still be retried where it # left off.) This is usually a best practice, but if you're using Python # older than 2.6 or if you're running on App Engine, you should set the # chunksize to something like 1024 * 1024 (1 megabyte). media_body=MediaFileUpload(options.file, chunksize=-1, resumable=True) ) response = None error = None retry = 0 duration_seconds=0 while response is None: try: logging.debug("Uploading file...") start_seconds = time.time() status, response = insert_request.next_chunk() delta_seconds = time.time() - start_seconds duration_seconds += delta_seconds if "id" in response: return (response["id"], duration_seconds) else: logging.error("The upload failed with an unexpected response: %s" % response) exit(1) except HttpError, e: if e.resp.status in RETRIABLE_STATUS_CODES: error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status, e.content) else: raise except RETRIABLE_EXCEPTIONS, e: error = "A retriable error occurred: %s" % e if error is not None: logging.error(error) retry += 1 if retry > MAX_RETRIES: logging.error("No longer attempting to retry.") exit(1) max_sleep = 2 ** retry sleep_seconds = random.random() * max_sleep logging.debug("Sleeping %f seconds and then retrying..." % sleep_seconds) time.sleep(sleep_seconds) def create_asset(youtube_partner, content_owner_id, title, description): # This creates a new asset corresponding to a video on the web. # The asset is linked to the corresponding YouTube video via a # claim that will be created later. body = dict( type="web", metadata=dict( title=title, description=description ) ) assets_insert_response = youtube_partner.assets().insert( onBehalfOfContentOwner=content_owner_id, body=body ).execute() return assets_insert_response["id"] def set_asset_ownership(youtube_partner, content_owner_id, asset_id): # This specifies that content_owner_id owns 100% of the asset worldwide. # Adjust as needed. body = dict( general=[dict( owner=content_owner_id, ratio=100, type="exclude", territories=[] )] ) youtube_partner.ownership().update( onBehalfOfContentOwner=content_owner_id, assetId=asset_id, body=body ).execute() def claim_video(youtube_partner, content_owner_id, asset_id, video_id, policy_id): # policy_id can be set to the id of an existing policy, which can be obtained # via youtubePartner.policies.list() # See https://developers.google.com/youtube/partner/reference/rest/v1/policies/list # If you later update that existing policy, the claim will also be updated. if policy_id: policy = dict( id=policy_id ) # If policy_id is not provided, a new inline policy is created. else: policy = dict( rules=[dict( action="monetize" )] ) body = dict( assetId=asset_id, videoId=video_id, policy=policy, contentType="audiovisual" ) claims_insert_response = youtube_partner.claims().insert( onBehalfOfContentOwner=content_owner_id, body=body ).execute() return claims_insert_response["id"] def set_advertising_options(youtube_partner, content_owner_id, video_id): # This enables the true view ad format for the given video. # Adjust as needed. body = dict( adFormats=["trueview_instream"] ) youtube_partner.videoAdvertisingOptions().update( videoId=video_id, onBehalfOfContentOwner=content_owner_id, body=body ).execute() if __name__ == '__main__': logging.basicConfig( level=logging.DEBUG, format="%(asctime)s [%(name)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) options = parse_options() if options.file is None or not os.path.exists(options.file): logging.error("Please specify a valid file using the --file= parameter.") exit(1) # The channel ID looks something like "UC..." and needs to correspond to a # channel managed by the YouTube content owner authorizing the request. # youtube.channels.list(part="snippet", managedByMe=true, # onBehalfOfContentOwner=*CONTENT_OWNER_ID*) # can be used to retrieve a list of managed channels and their channel IDs. # See https://developers.google.com/youtube/v3/docs/channels/list if options.channelId is None: logging.error("Please specify a channel ID via the --channelId= parameter.") exit(1) (youtube, youtube_partner) = get_authenticated_services() content_owner_id = get_content_owner_id(youtube_partner) logging.info("Authorized by content owner ID '%s'." % content_owner_id) (video_id, duration_seconds) = upload(youtube, content_owner_id, options) logging.info("Successfully uploaded video ID '%s'." % video_id) file_size_bytes = os.path.getsize(options.file) logging.debug("Uploaded %d bytes in %0.2f seconds (%0.2f megabytes/second)." % (file_size_bytes, duration_seconds, (file_size_bytes / (1024 * 1024)) / duration_seconds)) asset_id = create_asset(youtube_partner, content_owner_id, options.title, options.description) logging.info("Created new asset ID '%s'." % asset_id) set_asset_ownership(youtube_partner, content_owner_id, asset_id) logging.info("Successfully set asset ownership.") claim_id = claim_video(youtube_partner, content_owner_id, asset_id, video_id, options.policyId) logging.info("Successfully claimed video.") set_advertising_options(youtube_partner, content_owner_id, video_id) logging.info("Successfully set advertising options.") logging.info("All done!")