> git clone https://github.com/sujpac/youtube-api.git
> cd youtube-api
> virtualenv env
> source env/bin/activate
Go to the Google Console and create a new project. Go to the Library tab and enable the YouTube Data API v3 API. Then, go to the Credentials tab and do the following:
- Create OAuth client ID credentials (to be used by
scrape_video_comments.py
) and choose desktop as the application type. - Download the client secrets file for the newly created OAuth credentials. Rename it to
client_secret.json
, and move to theyoutube-api
project directory.
> mv [downloads]/client_secret_10927*.apps.googleusercontent.com.json youtube-api/client_secret.json
- Create API Key credentials (to be used by
get_playlist_duration.py
). - Safely store your API key by setting an environment variable in your
.zshrc
(or whatever your shell configuration file is).
> echo 'export YT_API_KEY="[insert-your-API-key-here]"' >> ~/.zshrc
> source ~/.zshrc
> pip install google-api-python-client
> pip install google-auth google-auth-oauthlib google-auth-httplib2
> python scrape_video_comments.py
Enter a keyword: async python
> cat comments.csv
VideoID,Title,Comment
sBVb4IB3O_U,"Let's Build a Fast, Modern Python API with FastAPI",Thank you for sharing
sBVb4IB3O_U,"Let's Build a Fast, Modern Python API with FastAPI",23:06 Paul Everitt has the goofiest serious face I've ever seen
sBVb4IB3O_U,"Let's Build a Fast, Modern Python API with FastAPI",20:05 that's a good idea
[...]
[output truncated]
> python get_playlist_duration.py
Enter a YouTube playlist ID: PLSQl0a2vh4HCNiooUekZDy0LEfhikAu3Z
Playlist title: Rise to world power (1890-1945) | US History | Khan Academy
Number of videos: 16
Total duration: 2hrs 11mins 59secs
Average video duration: 8mins 14secs
This project is under the MIT License. See the LICENSE file for the full license text.