Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Last active March 21, 2018 05:29
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/4afb61a6c1ff6efd7779abbc11197c43 to your computer and use it in GitHub Desktop.
Save pandanote-info/4afb61a6c1ff6efd7779abbc11197c43 to your computer and use it in GitHub Desktop.
YouTube Data API v3を用いてYouTubeにおけるID、タイトル名、ファイル名及び再生回数をJSONファイルに保存するプログラム
#!/usr/bin/python
import httplib2
import os
import sys
import json
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 moviefilelist.py')
parser.add_argument('-o','--output-file', type=str, default='UploadedVideoDataList.json', help='File name of output.')
args = parser.parse_args()
params = vars(args)
uvdl = params['output_file'];
# Youtubeから動画のID及び元のファイル名のリスト等を取り出すための
# Pythonのテストプログラム。
# URL: https://pandanote.info/?p=1510
#
# 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()
uploadedVideoDataList = []
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"]
print("{0:s} ({1:s})".format(title, video_id))
video_list_request = youtube.videos().list(
id=video_id,
part="snippet,fileDetails,statistics",
maxResults=50
)
video_list_response = video_list_request.execute()
for video_list_item in video_list_response["items"]:
# if 'tags'in video_list_item["snippet"]:
# print(" --> タグ: {0:s}".format(','.join(video_list_item["snippet"]["tags"])))
print(" --> ファイル名: {0:s}".format(video_list_item["fileDetails"]["fileName"]))
uploadedVideoData = {}
uploadedVideoData["title"] = title
uploadedVideoData["videoid"] = video_id
uploadedVideoData["filename"] = video_list_item["fileDetails"]["fileName"]
uploadedVideoData["viewCount"] = video_list_item["statistics"]["viewCount"]
uploadedVideoDataList.append(uploadedVideoData)
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
print
#outputFile = 'UploadedVideoDataList.json'
with open(uvdl,'w') as outfile:
json.dump(uploadedVideoDataList,outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment