/copytag.py Secret
Last active
December 31, 2017 09:38
YouTubeにアップロードされている動画についているタグを別の動画のタグとしてコピーするためのPythonのテストプログラム。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import httplib2 | |
import os | |
import sys | |
from apiclient.discovery import build | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client.tools import argparser, run_flow | |
# Youtubeからある動画についているタグを別の動画のタグとしてコピーするための | |
# Pythonのテストプログラム。 | |
# URL: https://pandanote.info/?p=1454 | |
# | |
# 使い方: | |
# copytag.py <コピー元の動画のID> <コピー先の動画のID> | |
# | |
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains | |
# the OAuth 2.0 information for this application, including its client_id and | |
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from | |
# the Google Developers Console at | |
# https://console.developers.google.com/. | |
# Please ensure that you have enabled the YouTube Data API for your project. | |
# For more information about using OAuth2 to access the YouTube Data API, see: | |
# https://developers.google.com/youtube/v3/guides/authentication | |
# For more information about the client_secrets.json file format, see: | |
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets | |
if len(sys.argv) < 3: | |
print('Usage: python3 {0:s} <コピー元の動画のID> <コピー先の動画のID>'.format(sys.argv[0])) | |
quit() | |
source_id = sys.argv[1] | |
target_id = sys.argv[2] | |
del sys.argv[2] | |
del sys.argv[1] | |
CLIENT_SECRETS_FILE = "client_secret_copytag.json" | |
# This variable defines a message to display if the CLIENT_SECRETS_FILE is | |
# missing. | |
MISSING_CLIENT_SECRETS_MESSAGE = """ | |
WARNING: Please configure OAuth 2.0 | |
To make this sample run you will need to populate the client_secrets.json file | |
found at: | |
%s | |
with information from the Developers Console | |
https://console.developers.google.com/ | |
For more information about the client_secrets.json file format, please visit: | |
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets | |
""" % os.path.abspath(os.path.join(os.path.dirname(__file__), | |
CLIENT_SECRETS_FILE)) | |
# This OAuth 2.0 access scope allows for read-only access to the authenticated | |
# user's account, but not other types of account access. | |
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" | |
YOUTUBE_API_SERVICE_NAME = "youtube" | |
YOUTUBE_API_VERSION = "v3" | |
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, | |
message=MISSING_CLIENT_SECRETS_MESSAGE, | |
scope=YOUTUBE_READ_WRITE_SCOPE) | |
storage = Storage("%s-oauth2.json" % sys.argv[0]) | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
flags = argparser.parse_args() | |
credentials = run_flow(flow, storage, flags) | |
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, | |
http=credentials.authorize(httplib2.Http())) | |
# コピー元の動画のタグを抜き出して、コピー先の動画のタグとしてコピーします。 | |
# まず、コピー元の動画からタグを抜き出す処理を定義します。 | |
video_list_response = youtube.videos().list( | |
id=source_id, | |
part="snippet", | |
maxResults=50 | |
).execute() | |
tags = [] | |
if not video_list_response["items"]: | |
print("Video {0:s} was not found.".format(source_id)) | |
quit() | |
for video_list_item in video_list_response["items"]: | |
if 'tags' in video_list_item["snippet"]: | |
tags.extend(video_list_item["snippet"]["tags"]) | |
video_list_response = youtube.videos().list( | |
id=target_id, | |
part="snippet", | |
maxResults=50 | |
).execute() | |
if not video_list_response["items"]: | |
print("Video {0:s} was not found.".format(target_id)) | |
quit() | |
target_snippet = video_list_response["items"][0]["snippet"] | |
if "tags" not in target_snippet: | |
target_snippet["tags"] = [] | |
target_snippet["tags"].extend(tags) | |
insert_tag_request = youtube.videos().update( | |
part='snippet', | |
body=dict( | |
snippet=target_snippet, | |
id=target_id | |
) | |
).execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment