Unofficial YouTube API client
Use this library to do things the real YouTube API does not let you do. DO NOT USE THIS FOR ANY ACCOUNT OTHER THAN YOUR OWN!
This library supports Python 3.6+.
Use a netrc file
Please note for the time being, logging in is not working. For more details, see this youtube-dl issue.
Every command can take a --username
and --password
argument.
You should consider using a netrc file for your login. Example at ~/.netrc
:
machine youtube login LOGIN password YOUR_PASSWORD
You can specify a custom netrc file with the --netrc
argument.
Usage
Command line
youtube-clear-history
- Clear your Watch Historyyoutube-clear-search-history
- Clear your Search Historyyoutube-clear-watch-later
- Clear your Watch Later queueyoutube-print-history-ids
- Print Watch History video IDsyoutube-print-playlist-ids
- Print video IDs from a specific playlistyoutube-print-watch-later-ids
- Print Watch Later video IDsyoutube-remove-history-entries
- Remove videos from your Watch Historyyoutube-remove-videoid
- Remove a video from a playlistyoutube-remove-watch-later-videoid
- Remove a video from your Watch Later queueyoutube-toggle-search-history
- Turn on/off Search Historyyoutube-toggle-watch-history
- Turn on/off Watch History
Every command takes a --debug
argument.
You can use exported cookies in Netscape format with the --cookies COOKIES_FILE
argument. By default, cookies are read from ~/.local/share/cookies/youtube.txt
.
Some commands accept a -j
/--json
argument to print machine-readable output.
Download commands
ytdl-history
- Use youtube-dl to download your historyytdl-liked
- Use youtube-dl to download your liked videosytdl-playlist
- Use youtube-dl to download a playlistytdl-watch-later
- Use youtube-dl to download your Watch Later playlist
For downloads to work, youtube-dl
must be in PATH
. To pass arguments to youtube-dl
, specify --
before those arguments. Example with arguments to youtube-dl:
ytdl-history --output-dir ~/Downloads --delete-after -- --extract-audio --audio-format m4a --audio-quality 0
Each download command has a -D
/--delete-after
option, which makes the script delete the entry from the set of videos after a successful download.
In Python
from os.path import expanduser
from youtube_unofficial import YouTube
yt = YouTube(cookies_path=expanduser('~/my-cookies-file.txt'), logged_in=True)
# Clear watch history
yt.clear_watch_history()
# Remove a single video ID from Watch Later queue
yt.remove_video_id_from_playlist('WL', video_id)
# Clear entire Watch Later queue
yt.clear_watch_later()
Contributing
For a new feature to be accepted, it must be something that cannot be achieved with Google’s official API.
Code must run through mypy
and pylint
based on the project settings.