Skip to content

Tools for managing YouTube videos via the YouTube Data API

Notifications You must be signed in to change notification settings

cgranier/pyTube

Repository files navigation

pyTube

pyTube exists to make managing our large content libraries on YouTube and Dailymotion a bit easier. It's also an excuse for coding, learning and doing boring stuff quickly.

Python 3.6 YouTube Data API V3 Dailymotion Python SDK Stack Exchange reputation

What's in the project

YouTube

  • reorder_playlist.py takes a list of YouTube Playlist IDs, gathers the videos in each playlist and reorders them according to their episode number. This is highly customized to our internal needs, but with minor modifications should work with any Playlist you own.

Dailymotion

  • dm_playlists.py takes a list of Dailymotion Playlist IDs, gathers the videos in each playlist and reorders them according to their episode number. This is highly customized to our internal needs, but with minor modifications should work with any Playlist you own. After reordering the playlist, it writes out the episodes' id, order, title and description to a local .csv file.

These have been moved to the legacy folder

  • pyTubePlaylist lets you download all the video metadata for a YouTube playlist.
  • pyTubeReorderPlaylist lets you take the metadata downloaded by pyTubePlaylist and (after some minor editing in Excel or Google Sheets) re-order the videos in the playlist by their episode number. This is particularly useful when you have a playlist with 150 episodes out of order.
  • pyTube lets you download basic stats for a list of channels.

Getting Started

Clone this repository:

git clone https://github.com/cgranier/pyTube.git

Install dependencies:

  • Install the Google APIs Client Library for Python
pip install --upgrade google-api-python-client
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
pip install dailymotion
  • Install additional libraries
pip install pandas

Setup YouTube APIs

If you're on my team, contact me for API access or CMS credentials if neeeded. Otherwise, you will need to create a Project on Google Cloud Platform and add the YouTube Data API V3.

The project doesn't need an API key anymore, as it uses OAuth to authenticate you with YouTube. You will need a YouTube account with permissions to manage the channel with the Playlist. This could be the channel owner's account or a YouTube CMS account (MCN Network) with admin permissions to act on behalf of channels in the network.

  1. Create an OAuth token for your project in Google Cloud Platform, so that you can authorize modification of your videos (needed to reorder a playlist or access private videos):
  • Go to the APIs dashboard for your project.

  • Go to the Credentials page and select Create credentials | OAuth client ID.

  • Select the application type Other, give it a memorable name, and click the Create button.

  • Click the file_download (Download JSON) button to the right of the client ID.

  • Move the downloaded file to your working directory and rename it client_secret.json.

  1. Create a config.py file and add your YouTube API credentials:
api_key = '[YOUR API KEY]'
content_owner = '[YOUR CONTENT OWNER ID]' (This is for MCN/CMS accounts only)
  1. Add your Dailymotion API credentials to config.py:
CLIENT_ID = '[YOUR API KEY]'
CLIENT_SECRET = '[YOUR API SECRET]'
USERNAME = '[YOUR USERNAME]'
PASSWORD = '[YOUR PASSWORD]'
REDIRECT_URI = '[YOUR REDIRECT URI]'
BASE_URL = 'https://api.dailymotion.com'
OAUTH_AUTHORIZE_URL = 'https://www.dailymotion.com/oauth/authorize'
OAUTH_TOKEN_URL = 'https://api.dailymotion.com/oauth/token'

reorder_playlist

Description

reorder_playlist allows you to download a list of videos within a playlist and modify their order within the playlist.

It currently downloads the following data, but more data is available:

  • playlist_item_id: the video's ID within the playlist. This is different than the video's ID and is necessary for updating the video within the playlist.
  • video_id: the video's ID on YouTube.
  • video_title: the video's title.
  • video_position: the video's position within the playlist.

Arguments

For reorder_playlist you must provide a list of playlist ids:

PLAYLISTS = ['PLM21IsezPrtpSFaeQR7nvEft7S_4vsE8S',
             'PLM21IsezPrtqf4Bh6H1M3zI4zNO4KIGbX']

Run the code

python reorder_playlist.py

Functions

  1. get_authenticated_service

Uses the Google API Client Libraries to authorize the script to modify your channels. You will see a message on the command line with a URL for OAuth authentication. Copy the URL into your browser (ideally one where you've already logged in into YouTube with the account that owns the playlist you wish to reorder), follow the instructions to authorize your project to manage the YouTube account, and copy the authorization code.

Authorization code request

Paste the authentication code into your terminal and continue.

  1. get_playlist_videos

Uses youtube.playlistItems().list to request a list of all videos in a playlist, paging through the playlist until all videos have been processed (YouTube limits results to 50 per page). Returns a JSON object with all the video metadata.

  1. get_video_list

Iterates through the video metadata collected by get_playlist_videos and creates a dictionary with the data we need for reordering. Uses a regex to extract the episode number from the video title.

The resulting dictionary is converted into a Pandas dataframe and reordered by episode number.

  1. reorder_playlist_videos

Calls youtube.playlistItems().update with new position data for each episode. Episodes need to be ordered from first to last in order to keep the proper position within the playlist.

Note: By default, YouTube limits API queries to 10,000 quota points per day. Reordering one video uses up 53 quota points right now, so you will be limited to around 180 videos per day.

dm_playlists

Description

dm_playlists allows you to download a list of videos within a playlist and modify their order within the playlist.

It currently downloads the following data, but more data is available:

  • video_id: the video's ID on Dailymotion.
  • video_title: the video's title.
  • video_url: the video's url in Dailymotion.
  • video_description: the video's metadata description.

Arguments

For dm_playlists you must provide a list of playlist ids:

DAILYMOTION = ['playlist_id_1',...,'playlist_id_n']

Run the code

python dm_playlists.py

Functions

  1. get_playlist_videos

Uses the Dailymotion API to GET a list of videos belonging to a playlist. It paginates through the list until no more videos are available and returns the results in playlist_videos[].

  1. get_video_list

Goes through playlist_videos and uses a regex to determine the relevant episode number for every video in the list. Stores the new list with episode numbers in video_list.

NOTE: We could (should?) probably combine 1. and 2. into a single function.

  1. reorder_videos

Uses the Dailymotion API to POST an ordered list of videos. The playlist will have the videos in the same order as the ids posted. Uses a Pandas dataframe created from video_list and sorted by episode. The video ids (now in order) are posted via the API, thus reordering the playlists.

  1. create_ordered_csv

Writes the dataframe out to a csv file, named according to the playlist id. Currently writes it out to the active directory.

Contribution guidelines

Please take a look at our contributing guidelines if you're interested in helping!

Pending Features

  • Front end
  • Edit video metadata
  • Access metadata from a database
  • Add comments to videos
  • Download all reports every month for archiving

Twitter Follow

1x.engineer

About

Tools for managing YouTube videos via the YouTube Data API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages