Skip to content

Instantly share code, notes, and snippets.

@omar2205
Created August 17, 2022 21:33
Show Gist options
  • Save omar2205/5d9a428c7474523514a74d8feabb860f to your computer and use it in GitHub Desktop.
Save omar2205/5d9a428c7474523514a74d8feabb860f to your computer and use it in GitHub Desktop.
Upload to YouTube - Python 3
# Updated from YouTube Example
# ref: https://developers.google.com/youtube/v3/guides/uploading_a_video
# pip install google-api-python-client oauth2client
import http.client
import httplib2
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.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# 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, http.client.NotConnected,
http.client.IncompleteRead, http.client.ImproperConnectionState,
http.client.CannotSendRequest, http.client.CannotSendHeader,
http.client.ResponseNotReady, http.client.BadStatusLine)
# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
# 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.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "client_secrets.json"
# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")
def get_auth(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_UPLOAD_SCOPE,
message="No client_secrets.json")
storage = Storage(f'{sys.argv[0]}-oauth2.json')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def init_uploader(yt, opts):
tags = None
if opts.keywords:
tags = opts.keywords.split(',')
body = dict(
snippet=dict(
title=opts.title,
description=opts.description,
tags=tags,
categoryId=opts.category
),
status=dict(
privacyStatus=opts.privacyStatus
)
)
insert_req = yt.videos().insert(
part=','.join(body.keys()),
body=body,
media_body=MediaFileUpload(opts.file, chunksize=-1, resumable=True)
)
resumable_upload(insert_req)
def resumable_upload(insert_req):
res = None
err = None
retry = 0
while res is None:
try:
print('Uploading file...')
status, res = insert_req.next_chunk()
if res is not None:
if 'id' in res:
print(f'Video id {res["id"]} was successfully uploaded')
print(f'URL: https://www.youtube.com/watch?v={res["id"]}')
else:
exit(f'The upload failed with an unexpected response:\n{res}')
except HttpError as e:
if e.resp.status in RETRIABLE_STATUS_CODES:
err = f'A retriable HTTP error {e.resp.status} occurred:\n{e.content}'
else:
raise
except RETRIABLE_EXCEPTIONS as e:
err = f'A retriable error occurred: {e}'
if err is not None:
print(err)
retry += 1
if retry > MAX_RETRIES:
exit('No longer attempting to retry')
max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep
print(f'Sleeping {sleep_seconds} seconds and then retrying...')
time.sleep(sleep_seconds)
if __name__ == '__main__':
argparser.add_argument('--file', required=True,
help='Video file to upload')
argparser.add_argument('--title', help='Video title',
default='Test title')
argparser.add_argument('--description', help='Video description',
default='Test description')
argparser.add_argument('--category', default='22',
help='Numeric video category. ' +
'See https://developers.google.com/youtube/v3/docs/videoCategories/list')
argparser.add_argument('--keywords', help='Video keywords, comma separated',
default='')
argparser.add_argument('--privacyStatus', choices=VALID_PRIVACY_STATUSES,
default=VALID_PRIVACY_STATUSES[0], help='Video privacy status.')
args = argparser.parse_args()
yt = get_auth(args)
try:
init_uploader(yt, args)
except HttpError as e:
print(f'An HTTP error {e.resp.status} occurred:\n{e.content}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment