|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Usage example: |
| 4 | +# python comment_threads.py --channelid='<channel_id>' --videoid='<video_id>' --text='<text>' |
| 5 | + |
| 6 | +import httplib2 |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +from apiclient.discovery import build_from_document |
| 11 | +from apiclient.errors import HttpError |
| 12 | +from oauth2client.client import flow_from_clientsecrets |
| 13 | +from oauth2client.file import Storage |
| 14 | +from oauth2client.tools import argparser, run_flow |
| 15 | + |
| 16 | + |
| 17 | +# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
| 18 | + |
| 19 | +# the OAuth 2.0 information for this application, including its client_id and |
| 20 | +# client_secret. You can acquire an OAuth 2.0 client ID and client secret from |
| 21 | +# the {{ Google Cloud Console }} at |
| 22 | +# {{ https://cloud.google.com/console }}. |
| 23 | +# Please ensure that you have enabled the YouTube Data API for your project. |
| 24 | +# For more information about using OAuth2 to access the YouTube Data API, see: |
| 25 | +# https://developers.google.com/youtube/v3/guides/authentication |
| 26 | +# For more information about the client_secrets.json file format, see: |
| 27 | +# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 28 | +CLIENT_SECRETS_FILE = "client_secrets.json" |
| 29 | + |
| 30 | +# This OAuth 2.0 access scope allows for full read/write access to the |
| 31 | +# authenticated user's account and requires requests to use an SSL connection. |
| 32 | +YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl" |
| 33 | +YOUTUBE_API_SERVICE_NAME = "youtube" |
| 34 | +YOUTUBE_API_VERSION = "v3" |
| 35 | + |
| 36 | +# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
| 37 | +# missing. |
| 38 | +MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 39 | +WARNING: Please configure OAuth 2.0 |
| 40 | +
|
| 41 | +To make this sample run you will need to populate the client_secrets.json file |
| 42 | +found at: |
| 43 | + %s |
| 44 | +with information from the APIs Console |
| 45 | +https://developers.google.com/console |
| 46 | +
|
| 47 | +For more information about the client_secrets.json file format, please visit: |
| 48 | +https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
| 49 | +""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 50 | + CLIENT_SECRETS_FILE)) |
| 51 | + |
| 52 | +# Authorize the request and store authorization credentials. |
| 53 | +def get_authenticated_service(args): |
| 54 | + flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE, |
| 55 | + message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 56 | + |
| 57 | + storage = Storage("%s-oauth2.json" % sys.argv[0]) |
| 58 | + credentials = storage.get() |
| 59 | + |
| 60 | + if credentials is None or credentials.invalid: |
| 61 | + credentials = run_flow(flow, storage, args) |
| 62 | + |
| 63 | + # Trusted testers can download this discovery document from the developers page |
| 64 | + # and it should be in the same directory with the code. |
| 65 | + with open("youtube-v3-discoverydocument.json", "r") as f: |
| 66 | + doc = f.read() |
| 67 | + return build_from_document(doc, http=credentials.authorize(httplib2.Http())) |
| 68 | + |
| 69 | + |
| 70 | +# Call the API's commentThreads.list method to list the existing comments. |
| 71 | +def get_comments(youtube, video_id, channel_id): |
| 72 | + results = youtube.commentThreads().list( |
| 73 | + part="snippet", |
| 74 | + videoId=video_id, |
| 75 | + channelId=channel_id, |
| 76 | + textFormat="plainText" |
| 77 | + ).execute() |
| 78 | + |
| 79 | + for item in results["items"]: |
| 80 | + comment = item["snippet"]["topLevelComment"] |
| 81 | + author = comment["snippet"]["authorDisplayName"] |
| 82 | + text = comment["snippet"]["textDisplay"] |
| 83 | + print "Comment by %s: %s" % (author, text) |
| 84 | + |
| 85 | + return results["items"] |
| 86 | + |
| 87 | + |
| 88 | +# Call the API's commentThreads.insert method to insert a comment. |
| 89 | +def insert_comment(youtube, channel_id, video_id, text): |
| 90 | + insert_result = youtube.commentThreads().insert( |
| 91 | + part="snippet", |
| 92 | + body=dict( |
| 93 | + snippet=dict( |
| 94 | + channelId=channel_id, |
| 95 | + videoId=video_id, |
| 96 | + topLevelComment=dict( |
| 97 | + snippet=dict( |
| 98 | + textOriginal=text |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | + ) |
| 103 | + ).execute() |
| 104 | + |
| 105 | + comment = insert_result["snippet"]["topLevelComment"] |
| 106 | + author = comment["snippet"]["authorDisplayName"] |
| 107 | + text = comment["snippet"]["textDisplay"] |
| 108 | + print "Inserted comment for %s: %s" % (author, text) |
| 109 | + |
| 110 | + |
| 111 | +# Call the API's commentThreads.update method to update an existing comment. |
| 112 | +def update_comment(youtube, comment): |
| 113 | + comment["snippet"]["topLevelComment"]["snippet"]["textOriginal"] = 'updated' |
| 114 | + update_result = youtube.commentThreads().update( |
| 115 | + part="snippet", |
| 116 | + body=comment |
| 117 | + ).execute() |
| 118 | + |
| 119 | + comment = update_result["snippet"]["topLevelComment"] |
| 120 | + author = comment["snippet"]["authorDisplayName"] |
| 121 | + text = comment["snippet"]["textDisplay"] |
| 122 | + print "Updated comment for %s: %s" % (author, text) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + # The "channelid" option specifies the YouTube channel ID that uniquely |
| 127 | + # identifies the channel for which the comment will be inserted. |
| 128 | + argparser.add_argument("--channelid", |
| 129 | + help="Required; ID for channel for which the comment will be inserted.") |
| 130 | + # The "videoid" option specifies the YouTube video ID that uniquely |
| 131 | + # identifies the video for which the comment will be inserted. |
| 132 | + argparser.add_argument("--videoid", |
| 133 | + help="Required; ID for video for which the comment will be inserted.") |
| 134 | + # The "text" option specifies the text that will be used as comment. |
| 135 | + argparser.add_argument("--text", help="Required; text that will be used as comment.") |
| 136 | + args = argparser.parse_args() |
| 137 | + |
| 138 | + if not args.channelid: |
| 139 | + exit("Please specify channelid using the --channelid= parameter.") |
| 140 | + if not args.videoid: |
| 141 | + exit("Please specify videoid using the --videoid= parameter.") |
| 142 | + if not args.text: |
| 143 | + exit("Please specify text using the --text= parameter.") |
| 144 | + |
| 145 | + youtube = get_authenticated_service(args) |
| 146 | + try: |
| 147 | + # All the available methods are used in sequence just for the sake of an example. |
| 148 | + # Insert channel comment by omitting videoId |
| 149 | + insert_comment(youtube, args.channelid, None, args.text) |
| 150 | + # Insert video comment |
| 151 | + insert_comment(youtube, args.channelid, args.videoid, args.text) |
| 152 | + video_comments = get_comments(youtube, args.videoid, None) |
| 153 | + if video_comments: |
| 154 | + update_comment(youtube, video_comments[0]) |
| 155 | + channel_comments = get_comments(youtube, None, args.channelid) |
| 156 | + if channel_comments: |
| 157 | + update_comment(youtube, channel_comments[0]) |
| 158 | + except HttpError, e: |
| 159 | + print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 160 | + else: |
| 161 | + print "Inserted, listed and updated top-level comments." |
| 162 | + |
0 commit comments