|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import httplib2 |
| 4 | +import os |
| 5 | +import random |
| 6 | +import sys |
| 7 | + |
| 8 | +from apiclient.discovery import build |
| 9 | +from apiclient.errors import HttpError |
| 10 | +from oauth2client.client import flow_from_clientsecrets |
| 11 | +from oauth2client.file import Storage |
| 12 | +from oauth2client.tools import argparser, run_flow |
| 13 | + |
| 14 | + |
| 15 | +# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
| 16 | +# the OAuth 2.0 information for this application, including its client_id and |
| 17 | +# client_secret. You can acquire an OAuth 2.0 client ID and client secret from |
| 18 | +# the {{ Google Cloud Console }} at |
| 19 | +# {{ https://cloud.google.com/console }}. |
| 20 | +# Please ensure that you have enabled the YouTube Data API for your project. |
| 21 | +# For more information about using OAuth2 to access the YouTube Data API, see: |
| 22 | +# https://developers.google.com/youtube/v3/guides/authentication |
| 23 | +# For more information about the client_secrets.json file format, see: |
| 24 | +# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 25 | + |
| 26 | +CLIENT_SECRETS_FILE = "client_secrets.json" |
| 27 | + |
| 28 | +# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
| 29 | +# missing. |
| 30 | +MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 31 | +WARNING: Please configure OAuth 2.0 |
| 32 | +
|
| 33 | +To make this sample run you will need to populate the client_secrets.json file |
| 34 | +found at: |
| 35 | +
|
| 36 | + %s |
| 37 | +
|
| 38 | +with information from the {{ Cloud Console }} |
| 39 | +{{ https://cloud.google.com/console }} |
| 40 | +
|
| 41 | +For more information about the client_secrets.json file format, please visit: |
| 42 | +https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 43 | +""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 44 | + CLIENT_SECRETS_FILE)) |
| 45 | + |
| 46 | +# This OAuth 2.0 access scope allows for full read/write access to the |
| 47 | +# authenticated user's account. |
| 48 | +YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube" |
| 49 | +YOUTUBE_API_SERVICE_NAME = "youtube" |
| 50 | +YOUTUBE_API_VERSION = "v3" |
| 51 | + |
| 52 | +def get_authenticated_service(args): |
| 53 | + flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE, |
| 54 | + message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 55 | + |
| 56 | + storage = Storage("%s-oauth2.json" % sys.argv[0]) |
| 57 | + credentials = storage.get() |
| 58 | + |
| 59 | + if credentials is None or credentials.invalid: |
| 60 | + credentials = run_flow(flow, storage, args) |
| 61 | + |
| 62 | + return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
| 63 | + http=credentials.authorize(httplib2.Http())) |
| 64 | + |
| 65 | +def get_current_channel_sections(youtube): |
| 66 | + channel_sections_list_response = youtube.channelSections().list( |
| 67 | + part="snippet,contentDetails", |
| 68 | + mine=True |
| 69 | + ).execute() |
| 70 | + |
| 71 | + return channel_sections_list_response["items"] |
| 72 | + |
| 73 | +def shuffle_channel_sections(youtube, channel_sections): |
| 74 | + # This will randomly reorder the items in the channel_sections list. |
| 75 | + random.shuffle(channel_sections) |
| 76 | + |
| 77 | + for channel_section in channel_sections: |
| 78 | + # Each section in the list of shuffled sections is sequentially |
| 79 | + # set to position 0, i.e. the top. |
| 80 | + channel_section["snippet"]["position"] = 0 |
| 81 | + |
| 82 | + youtube.channelSections().update( |
| 83 | + part="snippet,contentDetails", |
| 84 | + body=channel_section |
| 85 | + ).execute() |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + args = argparser.parse_args() |
| 89 | + |
| 90 | + youtube = get_authenticated_service(args) |
| 91 | + try: |
| 92 | + channel_sections = get_current_channel_sections(youtube) |
| 93 | + shuffle_channel_sections(youtube, channel_sections) |
| 94 | + except HttpError, e: |
| 95 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 96 | + else: |
| 97 | + print "The existing channel sections have been randomly shuffled." |
0 commit comments