This repository has been archived by the owner on Dec 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-metadata
executable file
·225 lines (185 loc) · 7.74 KB
/
update-metadata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
import re
import auth
from common import logger
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.videos.html#list
def fetch_video_metadata(youtube_client, video_id):
list_request = youtube_client.videos().list(
part='snippet',
id=video_id,
)
list_response = list_request.execute()
items = list_response['items']
if not items:
raise ValueError('video_id does not exist')
else:
return items[0]
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.videos.html#update
def update_title(youtube_client, video_id, pattern, repl, *, dry_run=False):
metadata = fetch_video_metadata(youtube_client, video_id)
snippet = metadata['snippet']
if pattern:
title = snippet['title']
m = re.match(pattern, title)
if m:
# Make sure the entire title is matched in order to prevent accidental loss of info.
if not m.group(0) == title:
logger.warning(f'{video_id} title only partially matches {pattern}: {title}')
return
new_title = m.expand(repl)
else:
logger.warning(f'{video_id} title unchanged: {title}')
return
else:
# Replace title with repl, verbatim, without consulting the existing title
new_title = repl
if not dry_run:
snippet['title'] = new_title
update_request = youtube_client.videos().update(
part='id,snippet',
body=dict(
id=video_id,
snippet=snippet,
),
)
update_request.execute()
logger.info(f'{video_id} title updated: {new_title}')
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.videos.html#update
def update_description(youtube_client, video_id, description):
metadata = fetch_video_metadata(youtube_client, video_id)
snippet = metadata['snippet']
snippet['description'] = description
update_request = youtube_client.videos().update(
part='id,snippet',
body=dict(
id=video_id,
snippet=snippet,
),
)
update_request.execute()
logger.info(f'{video_id} description updated')
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.videos.html#update
def tag(youtube_client, video_id, tags, *, overwrite=False):
metadata = fetch_video_metadata(youtube_client, video_id)
snippet = metadata['snippet']
if overwrite:
snippet['tags'] = tags
else:
for tag_ in tags:
if tag_ not in snippet['tags']:
snippet['tags'].append(tag_)
update_request = youtube_client.videos().update(
part='id,snippet',
body=dict(
id=video_id,
snippet=snippet,
),
)
update_request.execute()
logger.info(f'{video_id} tags updated: {",".join(snippet["tags"])}')
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.videos.html#update
def publish(youtube_client, video_id):
update_request = youtube_client.videos().update(
part='id,status',
body=dict(
id=video_id,
status=dict(
privacyStatus='public',
),
),
)
update_request.execute()
logger.info(f'{video_id} published')
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.playlists.html#list
def playlist_length(youtube_client, playlist_id):
list_request = youtube_client.playlists().list(
part='contentDetails',
id=playlist_id,
)
list_response = list_request.execute()
items = list_response['items']
if not items:
raise ValueError('video_id does not exist')
else:
return items[0]['contentDetails']['itemCount']
# A negative position is counted from the end of the playlist; e.g., -1
# is means the video is to be inserted at the bottom.
#
# https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/youtube_v3.playlistItems.html#insert
def insert_playlist(youtube_client, video_id, playlist_id, *, position=0):
if position < 0:
length = playlist_length(youtube_client, playlist_id)
position += length + 1
insert_request = youtube_client.playlistItems().insert(
part='snippet',
body=dict(
snippet=dict(
playlistId=playlist_id,
resourceId=dict(
kind='youtube#video',
videoId=video_id,
),
position=position,
),
),
)
insert_request.execute()
logger.info(f'{video_id} inserted into playlist {playlist_id}')
def update_title_handler(args):
for video_id in args.video_ids.split(','):
update_title(args.youtube_client, video_id, args.pattern, args.repl, dry_run=args.test)
def update_description_handler(args):
for video_id in args.video_ids.split(','):
update_description(args.youtube_client, video_id, args.description)
def tag_handler(args):
for video_id in args.video_ids.split(','):
tag(args.youtube_client, video_id, args.tags.split(','), overwrite=args.overwrite)
def publish_handler(args):
for video_id in args.video_ids.split(','):
publish(args.youtube_client, video_id)
def insert_playlist_handler(args):
for video_id in args.video_ids.split(','):
for playlist_id in args.playlist_ids.split(','):
insert_playlist(args.youtube_client, video_id, playlist_id,
position=-1 if args.bottom else 0)
def main():
parser = auth.ArgumentParser()
subparsers = parser.add_subparsers()
desc = '''If pattern is nonempty, the title is replaced with
re.search(pattern, current_title).expand(repl); otherwise, the title
is directly replaced with repl.'''
parser_update_title = subparsers.add_parser('update-title', description=desc)
add = parser_update_title.add_argument
add('-t', '--test', action='store_true',
help='show expected renames but do not commit to them')
add('video_ids', help='comma-delimited video IDs')
add('pattern')
add('repl')
parser_update_title.set_defaults(handler=update_title_handler)
parser_update_description = subparsers.add_parser('update-description')
add = parser_update_description.add_argument
add('video_ids', help='comma-delimited video IDs')
add('description')
parser_update_description.set_defaults(handler=update_description_handler)
parser_tag = subparsers.add_parser('tag')
add = parser_tag.add_argument
add('-o', '--overwrite', action='store_true', help='overwrite existing tags')
add('video_ids', help='comma-delimited video IDs')
add('tags', help='comma-delimited new tags')
parser_tag.set_defaults(handler=tag_handler)
parser_publish = subparsers.add_parser('publish')
add = parser_publish.add_argument
add('video_ids', help='comma-delimited video IDs')
parser_publish.set_defaults(handler=publish_handler)
parser_insert_playlist = subparsers.add_parser('insert-playlist')
add = parser_insert_playlist.add_argument
add('-b', '--bottom', action='store_true',
help='insert to the bottom of the playlist(s) instead of top')
add('video_ids', help='comma-delimited video IDs')
add('playlist_ids', help='comma-delimited playlist IDs')
parser_insert_playlist.set_defaults(handler=insert_playlist_handler)
args = parser.parse_args()
args.youtube_client = auth.get_youtube_client(args, 'youtube')
args.handler(args)
if __name__ == '__main__':
main()