Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Last active November 23, 2018 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandanote-info/196df40df0f8d8aba25c8227b1a4035e to your computer and use it in GitHub Desktop.
Save pandanote-info/196df40df0f8d8aba25c8227b1a4035e to your computer and use it in GitHub Desktop.
Youtubeから動画のID等のリストを取り出すためのPython3のテストプログラム。
#!/usr/bin/python
import httplib2
import os
import sys
import argparse
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
parser = argparse.ArgumentParser(description='Command line options of movielist.py')
parser.add_argument('-a','--all-file', help='Dump information of all movies, including movies whose privacyStatus is not public.', action="store_true")
parser.add_argument('--noauth-local-webserver', help="Authorization at outside of local server.", action="store_true");
parser.add_argument('--logging-level', default="INFO", help="Logging level for oauth2client.", action="store_true");
args = parser.parse_args()
# Youtubeから動画のID等のリストを取り出すためのPython3のテストプログラム。
# URL: https://pandanote.info/?p=791
#
# 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
CLIENT_SECRETS_FILE = "client_secrets.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_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
message=MISSING_CLIENT_SECRETS_MESSAGE,
scope=YOUTUBE_READONLY_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, args)
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
# Retrieve the contentDetails part of the channel resource for the
# authenticated user's channel.
channels_response = youtube.channels().list(
mine=True,
part="contentDetails"
).execute()
for channel in channels_response["items"]:
# From the API response, extract the playlist ID that identifies the list
# of videos uploaded to the authenticated user's channel.
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
print("Videos in list {0:s}".format(uploads_list_id))
# Retrieve the list of videos uploaded to the authenticated user's channel.
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part="snippet",
maxResults=50
)
while playlistitems_list_request:
playlistitems_list_response = playlistitems_list_request.execute()
# Print information about each video.
for playlist_item in playlistitems_list_response["items"]:
title = playlist_item["snippet"]["title"]
video_id = playlist_item["snippet"]["resourceId"]["videoId"]
video_list_request = youtube.videos().list(
id=video_id,
part="snippet,status",
maxResults=50
)
video_list_response = video_list_request.execute()
for video_list_item in video_list_response["items"]:
if args.all_file or video_list_item["status"]["privacyStatus"] == 'public':
if 'tags'in video_list_item["snippet"]:
print("{0:s} ({1:s})".format(title, video_id))
print(" --> タグ: {0:s}".format(','.join(video_list_item["snippet"]["tags"])))
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment