Skip to content

Instantly share code, notes, and snippets.

@FaltoGH
Created April 3, 2021 17:55
Show Gist options
  • Save FaltoGH/1d5617db29083e5c172f4d9f56630185 to your computer and use it in GitHub Desktop.
Save FaltoGH/1d5617db29083e5c172f4d9f56630185 to your computer and use it in GitHub Desktop.
update title of video using youtube api
# original code: https://developers.google.com/youtube/v3/docs/videos/update
import datetime
import sys
import httplib2
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
message='MISSING_CLIENT_SECRETS_MESSAGE')
storage = Storage("%s-oauth2.json" % sys.argv[0])
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 update_video(youtube, options):
now = datetime.datetime.now()
a = {}
a['title'] = f'{now}'
a['categoryId'] = '20'
videos_update_response = youtube.videos().update(
part='snippet',
body=dict(
snippet=a,
id=options.video_id
)).execute()
if __name__ == "__main__":
argparser.add_argument("--video-id", help="ID of video to update.",
required=True)
args = argparser.parse_args()
youtube = get_authenticated_service(args)
try:
update_video(youtube, args)
except HttpError as e:
print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
else:
print("Something was added to video id '%s'." % (args.video_id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment