Skip to content

Commit 89442fb

Browse files
committed
"Added sample: python/set_watermark.py"
1 parent 452ada3 commit 89442fb

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

python/set_watermark.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/python
2+
3+
# Usage example:
4+
# python set_watermark.py --channelid='<channel_id>' --file='<file_name>'
5+
# --metadata='{ "position": { "type": "corner", "cornerPosition": "topRight" },
6+
# "timing": { "type": "offsetFromStart", "offsetMs": 42 } }'
7+
8+
import json
9+
import os
10+
import sys
11+
import httplib2
12+
13+
from apiclient.discovery import build
14+
from apiclient.errors import HttpError
15+
from oauth2client.file import Storage
16+
from oauth2client.client import flow_from_clientsecrets
17+
from oauth2client.tools import run
18+
from optparse import OptionParser
19+
20+
21+
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
22+
23+
# the OAuth 2.0 information for this application, including its client_id and
24+
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
25+
# the {{ Google Cloud Console }} at
26+
# {{ https://cloud.google.com/console }}.
27+
# Please ensure that you have enabled the YouTube Data API for your project.
28+
# For more information about using OAuth2 to access the YouTube Data API, see:
29+
# https://developers.google.com/youtube/v3/guides/authentication
30+
# For more information about the client_secrets.json file format, see:
31+
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
32+
CLIENT_SECRETS_FILE = "client_secrets.json"
33+
34+
# This OAuth 2.0 access scope allows for full read/write access to the
35+
# authenticated user's account.
36+
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
37+
YOUTUBE_API_SERVICE_NAME = "youtube"
38+
YOUTUBE_API_VERSION = "v3"
39+
40+
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
41+
# missing.
42+
MISSING_CLIENT_SECRETS_MESSAGE = """
43+
WARNING: Please configure OAuth 2.0
44+
45+
To make this sample run you will need to populate the client_secrets.json file
46+
found at:
47+
%s
48+
with information from the APIs Console
49+
https://developers.google.com/console
50+
51+
For more information about the client_secrets.json file format, please visit:
52+
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
53+
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
54+
CLIENT_SECRETS_FILE))
55+
56+
# Authorize the request and store authorization credentials.
57+
def get_authenticated_service():
58+
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
59+
scope=YOUTUBE_READ_WRITE_SCOPE,
60+
message=MISSING_CLIENT_SECRETS_MESSAGE)
61+
62+
storage = Storage("%s-oauth2.json" % sys.argv[0])
63+
credentials = storage.get()
64+
65+
if credentials is None or credentials.invalid:
66+
credentials = run(flow, storage)
67+
68+
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
69+
http=credentials.authorize(httplib2.Http()))
70+
71+
72+
# Call the API's watermarks.set method to upload the watermark image and
73+
# associate it with the proper channel.
74+
def set_watermark(youtube, channel_id, file, metadata):
75+
try:
76+
youtube.watermarks().set(
77+
channelId=channel_id,
78+
media_body=file,
79+
body=metadata,
80+
).execute()
81+
except HttpError as e:
82+
print "Error while setting watermark: %s" % e.content
83+
raise e
84+
85+
86+
if __name__ == "__main__":
87+
parser = OptionParser()
88+
# The "channelid" option specifies the YouTube channel ID that uniquely
89+
# identifies the channel for which the watermark image is being updated.
90+
parser.add_option("--channelid", dest="channelid",
91+
help="Required; ID for channel that is having its watermark updated.")
92+
# The "file" option specifies the path to the image being uploaded.
93+
parser.add_option("--file", dest="file",
94+
help="Required; path to watermark image file.")
95+
# The "metadata" option specifies the JSON for the watermark resource
96+
# provided with the request.
97+
parser.add_option("--metadata", dest="metadata",
98+
help="Required; watermark metadata in JSON format.")
99+
(options, args) = parser.parse_args()
100+
101+
if not options.channelid:
102+
parser.print_help()
103+
exit()
104+
105+
youtube = get_authenticated_service()
106+
107+
if not options.file or not os.path.exists(options.file):
108+
exit("Please specify a valid file using the --file= parameter.")
109+
if not options.metadata:
110+
exit("Please specify watermark metadata using the --metadata= parameter.")
111+
set_watermark(youtube, options.channelid, options.file,
112+
json.loads(options.metadata))
113+
print "The watermark was successfully set."

0 commit comments

Comments
 (0)