This time, I will try to display a list of videos of my account on the console.
http://silver-shell.org/blog/?p=227 http://qiita.com/ottati/items/f7782bc834847470d4c8
Control is done in the Google API Console.
To use the Google API, you need to create a "project". Select "Library" from the left menu [https://console.developers.google.com/apis/library) and select the YouTube Data API. Select "Create Project". (There are other ways to create a "project") Create a project with a suitable name. Please refer to here to delete the project.
Select "Library" from the left menu, Select YouTube Data API. Enable the button next to "Youtube API Data v3". If you enable it, there will be a button called "Create Credentials", so click on it.
The same screen can also be displayed by selecting "Authentication Information"-> "Authentication Information"-> "Select with Wizard".
"Determine the type of credentials required" to "YouTube Data API v3" "Place to call API" to "Web browser (Javascript)" Set "Type of data to be accessed" to "User data" Click "Required Credentials".
Give an appropriate name in English for identification.
The approved redirect URI is "http: // localhost: 8080 /" To (This part may change depending on the environment. It may be 8000 or 8090, and it may not work without the last "/".)
Click the "Download" button to download the json file that describes the settings to be used on Python. You can download it after completing the settings here, but the file name may be different from when you downloaded it here. (Same content except for the file name) (This time it was a file called client_id.json.)
Build a new Python virtual environment for operation in the terminal (command prompt for windows). This time I named it youtube.
command
conda create -n youtube python=3.5.3
#When creating with a set of Anaconda
# conda create -n youtube python=3.5.3 anaconda
#When checking the created environment
# conda info -e
# conda list -n youtube
#When deleting the created virtual environment
# conda remove -n youtube --all
Subsequent work is performed in the virtual environment.
command
# Windows
activate youtube
#Deactivate
# deactivate
# Mac
source activate youtube
#Deactivate
# source deactivate
cmd
conda install -c https://conda.anaconda.org/anaconda django
cmd
pip install --upgrade google-api-python-client
https://github.com/google/oauth2client
cmd
pip install --upgrade oauth2client
http://mikegrouchy.com/blog/2014/06/pro-tip-pip-upgrade-all-python-packages.html
cmd
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -U
Create a directory to save the Python file and move the file you downloaded earlier to it.
This time, we will get a list of uploaded files. Some modifications have been made based on the "Get My Uploaded Video" script at the URL below.
https://developers.google.com/youtube/v3/code_samples/python?hl=ja#retrieve_my_uploads
Create a Python file that describes the script below. (This time named main.py)
main.py
#! env python
# -*- coding: utf-8 -*-
import httplib2
import os
import sys
import glob
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
if __name__ == '__main__':
#Change the current directory to the location of the executable file
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
CLIENT_SECRETS_FILE = "client_id.json" #Downloaded file name
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))
YOUTUBE_READONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
CLIENT_SECRETS_FILE_PATH = glob.glob(os.path.join(os.getcwd(), CLIENT_SECRETS_FILE))[0]
flow = flow_from_clientsecrets(os.path.join(os.getcwd(), CLIENT_SECRETS_FILE_PATH),
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()))
channels_response = youtube.channels().list(
mine=True,
part="contentDetails"
).execute()
for channel in channels_response["items"]:
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
print("Videos in list %s" % uploads_list_id)
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()
for playlist_item in playlistitems_list_response["items"]:
title = playlist_item["snippet"]["title"]
video_id = playlist_item["snippet"]["resourceId"]["videoId"]
print("%s (%s)" % (title, video_id))
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
When executing in a terminal (command prompt for windows) on a virtual environment
cmd
cd (Python project directory)
python main.py
The first time you run it, the browser will start "(The name of the credential) is requesting the following permission." Will be displayed, so give permission.
After executing the above, the list of videos uploaded so far will be displayed on the console.
Various controls other than list display are possible. Of course, when deleting a video, make sure to thoroughly verify it before doing so at your own risk.
https://developers.google.com/youtube/1.0/developers_guide_python
Recommended Posts