Last active
December 31, 2020 13:03
-
-
Save pandanote-info/1c7bebb64b4384c3f38068d7f4d66dd4 to your computer and use it in GitHub Desktop.
YouTube Data API (v3)を使って、アップロードした動画についてのメタ情報を取得し、HTMLフォーマットで出力するPython3のスクリプト。
This file contains 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 | |
import json | |
import argparse | |
import re | |
import random | |
from pytz import timezone | |
from dateutil import parser | |
from datetime import datetime | |
# For an older version than google_api_python_client-1.12.8 | |
# from apiclient.discovery import build | |
# For google_api_python_client-1.12.8 or newer. | |
from googleapiclient.discovery import build | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client.tools import argparser, run_flow | |
def append_link(matchobj): | |
s = matchobj.group(0) | |
return "<a href=\"{0:s}\" style=\"color: #1b3b8a\">{1:s}</a>".format(s,s) | |
random.seed() | |
#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及び元のファイル名のリスト等を取り出して、 | |
# チャンネルリストのmarkdownファイルに変換するためのPython3の | |
# スクリプト。 | |
# URL: https://sidestory.pandanote.info/ | |
# | |
# 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, flags) | |
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),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( | |
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,status", | |
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"] | |
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"] | |
uploadedVideoDataList.append(uploadedVideoData) | |
playlistitems_list_request = youtube.playlistItems().list_next( | |
playlistitems_list_request, playlistitems_list_response) | |
#outputFile = 'UploadedVideoDataList.json' | |
#with open(uvdl,'w') as outfile: | |
#json.dump(uploadedVideoDataList,outfile) | |
templateFile = "index.md.template" | |
templateAppendFile = "index.md.append" | |
templateText = "" | |
templateAppendText = "" | |
with open(templateFile) as infile: | |
templateText = infile.read() | |
print(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=\"https://www.youtube.com/channel/UC2CV_cEjBd81csrHy24Kytg\">YouTube channel</a> to see more accurate data.</p>".format(datetime.now(timezone('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=\"https://youtu.be/{0:s}\">".format(uvd["videoid"]) | |
atop = "<a href=\"https://youtu.be/{0:s}\" style=\"color:#1b3b8a; font-size:1.5em; font-weight: bold;\">".format(uvd["videoid"]) | |
abottom = "</a>" | |
thumbnail = "<img src=\"https://i.ytimg.com/vi/{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} & {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())) | |
print("</div>") | |
pos = pos + 1 | |
with open(templateAppendFile) as infile2: | |
templateAppendText = infile2.read() | |
print(templateAppendText) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment