Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
YouTube Data API (v3)を使って、アップロードした動画についてのメタ情報を取得し、HTMLフォーマットで出力するPython3のスクリプト。
# $Id: 495 2018-11-03 12:04:49Z panda $
import httplib2
import os
import sys
import json
import argparse
import re
import random
from pytz import timezone
from dateutil import parser
from datetime import datetime
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from import argparser, run_flow
def append_link(matchobj):
s =
return "<a href=\"{0:s}\" style=\"color: #1b3b8a\">{1:s}</a>".format(s,s)
#parser = argparse.ArgumentParser(description='Command line options of')
#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及び元のファイル名のリスト等を取り出して、
# チャンネルリストのmarkdownファイルに変換するためのPython3の
# スクリプト。
# URL:
# 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
# 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:
# For more information about the client_secrets.json file format, see:
CLIENT_SECRETS_FILE = "client_secrets.json"
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
with information from the Developers Console
For more information about the client_secrets.json file format, please visit:
""" % os.path.abspath(os.path.join(os.path.dirname(__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.
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
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)
# Retrieve the contentDetails part of the channel resource for the
# authenticated user's channel.
channels_response = youtube.channels().list(
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),file=sys.stderr)
# Retrieve the list of videos uploaded to the authenticated user's channel.
# You can get the information of 50 videos at most.
playlistitems_list_request = youtube.playlistItems().list(
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(
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"]
uploadedVideoData["likeCount"] = video_list_item["statistics"]["likeCount"]
uploadedVideoData["description"] = video_list_item["snippet"]["description"]
uploadedVideoData["publishedAt"] = video_list_item["snippet"]["publishedAt"]
uploadedVideoData["privacyStatus"] = video_list_item["status"]["privacyStatus"]
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
#outputFile = 'UploadedVideoDataList.json'
#with open(uvdl,'w') as outfile:
templateFile = ""
templateAppendFile = ""
templateText = ""
templateAppendText = ""
with open(templateFile) as infile:
templateText =
pattern = re.compile('\n{2,}')
pattern2 = re.compile('\n')
urlpattern = re.compile('https?://[\w/:%#\$&\?\(\)~\.=\+\-]+')
admod = random.randint(2,4)
pos = 0
print("<p class=\"stats\">Count data is as of {0:s} with delay. Please check my <a href=\"\">YouTube channel</a> to see more accurate data.</p>".format('Asia/Tokyo')).isoformat()))
for uvd in uploadedVideoDataList:
if uvd["privacyStatus"] == 'public':
description = urlpattern.sub(append_link,pattern2.sub('<br/>',pattern.sub('</p><p>',uvd["description"])))
attop = "<a href=\"{0:s}\">".format(uvd["videoid"])
atop = "<a href=\"{0:s}\" style=\"color:#1b3b8a; font-size:1.5em; font-weight: bold;\">".format(uvd["videoid"])
abottom = "</a>"
thumbnail = "<img src=\"{0:s}/default.jpg\" align=\"left\" style=\"border:0;margin-right:0.5em;\"/>".format(uvd["videoid"])
viewCount = "{0:s} view".format(uvd["viewCount"])
if int(uvd["viewCount"]) > 1:
viewCount = viewCount + "s"
likeCount = "{0:s} like".format(uvd["likeCount"])
if int(uvd["likeCount"]) > 1:
likeCount = likeCount + "s"
tt = parser.parse(uvd["publishedAt"])
print("<div style=\"border-top: solid 1px #1b3b8a; \">")
print("<div style=\"border: 0pt; margin-right:10px;\">{0:s}{1:s}{2:s}</div><div><p>{3:s}{4:s}{5:s}</p><p>{6:s}</p><p class=\"stats\">{7:s} &amp; {8:s}, Published at: {9:s}</p><div style=\"clear:both;\"></div></div>".format(attop,thumbnail,abottom,atop,uvd["title"],abottom,description,viewCount,likeCount,tt.astimezone(timezone('Asia/Tokyo')).isoformat()))
pos = pos + 1
with open(templateAppendFile) as infile2:
templateAppendText =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.