1
1
#!/usr/bin/python
2
2
3
- import httplib2
3
+ import argparse
4
4
import os
5
5
import re
6
- import sys
7
6
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
7
+ import google . oauth2 . credentials
8
+ import google_auth_oauthlib . flow
9
+ from googleapiclient . discovery import build
10
+ from googleapiclient . errors import HttpError
11
+ from google_auth_oauthlib . flow import InstalledAppFlow
13
12
14
13
15
14
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
23
22
# For more information about the client_secrets.json file format, see:
24
23
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
25
24
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 ))
25
+ CLIENT_SECRETS_FILE = 'client_secret.json'
45
26
46
27
# This OAuth 2.0 access scope allows for full read/write access to the
47
28
# 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
- SECTION_TYPES = ("allPlaylists" , "completedEvents" , "likedPlaylists" ,
53
- "likes" , "liveEvents" , "multipleChannels" , "multiplePlaylists" ,
54
- "popularUploads" , "recentActivity" , "recentPosts" , "recentUploads" ,
55
- "singlePlaylist" , "upcomingEvents" ,)
56
- SECTION_STYLES = ("horizontalRow" , "verticalList" ,)
29
+ SCOPES = ['https://www.googleapis.com/auth/youtube' ]
30
+ API_SERVICE_NAME = 'youtube'
31
+ API_VERSION = 'v3'
57
32
58
- def get_authenticated_service (args ):
59
- flow = flow_from_clientsecrets (CLIENT_SECRETS_FILE , scope = YOUTUBE_SCOPE ,
60
- message = MISSING_CLIENT_SECRETS_MESSAGE )
33
+ SECTION_TYPES = ('allPlaylists' , 'completedEvents' , 'likedPlaylists' ,
34
+ 'likes' , 'liveEvents' , 'multipleChannels' , 'multiplePlaylists' ,
35
+ 'popularUploads' , 'recentActivity' , 'recentPosts' , 'recentUploads' ,
36
+ 'singlePlaylist' , 'upcomingEvents' ,)
37
+ SECTION_STYLES = ('horizontalRow' , 'verticalList' ,)
61
38
62
- storage = Storage ("%s-oauth2.json" % sys .argv [0 ])
63
- credentials = storage .get ()
64
39
65
- if credentials is None or credentials .invalid :
66
- credentials = run_flow (flow , storage , args )
40
+ def get_authenticated_service ():
41
+ flow = InstalledAppFlow .from_client_secrets_file (CLIENT_SECRETS_FILE , SCOPES )
42
+ credentials = flow .run_console ()
43
+ return build (API_SERVICE_NAME , API_VERSION , credentials = credentials )
67
44
68
- return build ( YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
69
- http = credentials . authorize ( httplib2 . Http ()) )
45
+ def print_response ( response ):
46
+ print ( response )
70
47
71
48
def enable_browse_view (youtube ):
72
49
channels_list_response = youtube .channels ().list (
73
- part = " brandingSettings" ,
50
+ part = ' brandingSettings' ,
74
51
mine = True
75
52
).execute ()
76
53
77
- channel = channels_list_response [" items" ][0 ]
78
- channel [" brandingSettings" ][ " channel" ][ " showBrowseView" ] = True
54
+ channel = channels_list_response [' items' ][0 ]
55
+ channel [' brandingSettings' ][ ' channel' ][ ' showBrowseView' ] = True
79
56
80
57
youtube .channels ().update (
81
- part = " brandingSettings" ,
58
+ part = ' brandingSettings' ,
82
59
body = channel
83
60
).execute ()
84
61
85
62
def add_channel_section (youtube , args ):
86
63
channels = None
87
64
if args .channels :
88
- channels = re .split (" \s*,\s*" , args .channels )
65
+ channels = re .split (' \s*,\s*' , args .channels )
89
66
playlists = None
90
67
if args .playlists :
91
- playlists = re .split (" \s*,\s*" , args .playlists )
68
+ playlists = re .split (' \s*,\s*' , args .playlists )
92
69
93
70
body = dict (
94
71
snippet = dict (
@@ -104,28 +81,31 @@ def add_channel_section(youtube, args):
104
81
)
105
82
106
83
youtube .channelSections ().insert (
107
- part = " snippet,contentDetails" ,
84
+ part = ' snippet,contentDetails' ,
108
85
body = body
109
86
).execute ()
110
87
111
88
if __name__ == '__main__' :
112
- argparser .add_argument ("--type" , choices = SECTION_TYPES , required = True ,
113
- help = "The type of the section to be added." )
114
- argparser .add_argument ("--style" , choices = SECTION_STYLES , required = True ,
115
- help = "The style of the section to be added." )
116
- argparser .add_argument ("--title" ,
117
- help = ("The title to display for the new section. This is only used "
118
- "with the multiplePlaylists or multipleChannels section types." ))
119
- argparser .add_argument ("--position" , type = int ,
120
- help = ("The position of the new section. "
121
- "Use 0 for the top, or don't set a value for the bottom." ))
122
- argparser .add_argument ("--playlists" ,
123
- help = "One or more playlist ids, comma-separated (e.g. PL...)." )
124
- argparser .add_argument ("--channels" ,
125
- help = "One or more channel ids, comma-separated (e.g. UC...)." )
126
- args = argparser .parse_args ()
127
-
128
- youtube = get_authenticated_service (args )
89
+
90
+ parser = argparse .ArgumentParser (description = 'Process some integers.' )
91
+ parser .add_argument ('--type' , choices = SECTION_TYPES , required = True ,
92
+ help = 'The type of the section to be added.' )
93
+ parser .add_argument ('--style' , choices = SECTION_STYLES , required = True ,
94
+ help = 'The style of the section to be added.' )
95
+ parser .add_argument ('--title' ,
96
+ help = 'The title to display for the new section. This is only used '
97
+ 'with the multiplePlaylists or multipleChannels section types.' )
98
+ parser .add_argument ('--position' , type = int ,
99
+ help = 'The position of the new section. Use 0 for the top, '
100
+ 'or don\' t set a value for the bottom.' )
101
+ parser .add_argument ('--playlists' ,
102
+ help = 'One or more playlist ids, comma-separated (e.g. PL...).' )
103
+ parser .add_argument ('--channels' ,
104
+ help = 'One or more channel ids, comma-separated (e.g. UC...).' )
105
+
106
+ args = parser .parse_args ()
107
+
108
+ youtube = get_authenticated_service ()
129
109
try :
130
110
# Before channel shelves will appear on your channel's web page, browse
131
111
# view needs to be enabled. If you know that your channel already has
@@ -135,6 +115,6 @@ def add_channel_section(youtube, args):
135
115
136
116
add_channel_section (youtube , args )
137
117
except HttpError , e :
138
- print " An HTTP error %d occurred:\n %s" % (e .resp .status , e .content )
118
+ print ' An HTTP error %d occurred:\n %s' % (e .resp .status , e .content )
139
119
else :
140
- print " Added new channel section."
120
+ print ' Added new channel section .
0 commit comments