Python YouTube Music Web API Client - No longer under active development, instead checkout innertube
Libraries ytm
depends on
Library | Install | Import | Functionality |
---|---|---|---|
Requests | pip install requests | import requests | Send HTTP/HTTPS requests |
Library | Install | Import | Functionality |
---|---|---|---|
YoutubeDL | pip install youtube-dl | import youtube_dl | Download YouTube videos |
Mutagen | pip install mutagen | import mutagen | Read and write audio metadata |
Pillow (PIL) | pip install Pillow | import PIL | Python imaging library |
Install ytm
onto your system.
Firstly, clone or download this repository.
No YouTubeMusicDL support
user@host:~$ pip install . # You may need to use: python3 -m pip install .
Successfully installed ytm
YouTubeMusicDL support
user@host:~$ pip install .[dl] # You may need to use: python3 -m pip install .[dl]
Successfully installed ytm
Create an API instance
>>> import ytm
>>>
>>> api = ytm.YouTubeMusic()
>>> api
<YouTubeMusic()>
>>>
View available methods
>>> from pprint import pprint
>>>
>>> pprint(list(api._methods))
['album',
'artist',
'artist_albums',
'artist_singles',
'guide',
'home',
'hotlist',
'playlist',
'queue',
'search',
'search_albums',
'search_artists',
'search_playlists',
'search_songs',
'search_suggestions',
'search_videos',
'song',
'watch',
'watch_radio',
'watch_shuffle']
>>>
API methods available to retrieve data from YouTube Music:
- Method - Album
- Method - Artist
- Method - Artist Albums
- Method - Artist Singles
- Method - Guide
- Method - Home
- Method - Hotlist
- Method - Playlist
- Method - Queue
- Method - Search
- Method - Search Albums
- Method - Search Artists
- Method - Search Playlists
- Method - Search Songs
- Method - Search Videos
- Method - Search Suggestions
- Method - Song
- Method - Watch
- Method - Watch Radio
- Method - Watch Shuffle
Retrieve information about an album.
View Example
>>> album = api.album('MPREb_ctJ5HEJw8pg') # Band Of Horses - Everything All The Time
>>>
>>> list(album)
['name', 'id', 'total_tracks', 'radio', 'shuffle', 'explicit', 'duration', 'date', 'type', 'thumbnail', 'description', 'artists', 'tracks', 'variants']
>>>
>>> album['name']
'Everything All The Time'
>>> album['total_tracks']
10
>>> album['explicit']
False
>>> album['duration']
2169367
>>> album['date']
{'year': 2006, 'month': 3, 'day': 21}
Retrieve information about an artist.
View Example
>>> artist = api.artist('UC8Yu1_yfN5qPh601Y4btsYw') # Arctic Monkeys
>>>
>>> list(artist)
['name', 'id', 'subscribers', 'views', 'description', 'albums', 'singles', 'videos', 'songs', 'playlists', 'similar_artists']
>>>
>>> artist['name']
'Arctic Monkeys'
>>> artist['subscribers']
'4.34M'
>>> artist['views']
2082236489
>>> artist['description']
'For tour dates, visit the website www.arcticmonkeys.com'
>>>
Retrieve information about an artist's albums.
View Example
>>> artist = api.artist('UC8Yu1_yfN5qPh601Y4btsYw') # Arctic Monkeys
>>>
>>> artist_id = artist['id']
>>> params = artist['albums']['params']
>>>
>>> albums = api.artist_albums(artist_id, params)
>>>
>>> for album in albums:
print(album['name'])
Tranquility Base Hotel & Casino
AM
Suck It and See
My Propeller
Cornerstone
Humbug
Teddy Picker
Fluorescent Adolescent
Favourite Worst Nightmare
Brianstorm
Who The Fuck Are Arctic Monkeys?
Whatever People Say I Am, That's What I Am Not
>>>
Retrieve information about an artist's singles.
View Example
>>> artist = api.artist('UC8Yu1_yfN5qPh601Y4btsYw') # Arctic Monkeys
>>>
>>> artist_id = artist['id']
>>> params = artist['singles']['params']
>>>
>>> singles = api.artist_singles(artist_id, params)
>>>
>>> for single in singles:
print(single['name'])
Tranquility Base Hotel & Casino
Why'd You Only Call Me When You're High?
Do I Wanna Know?
One For The Road
R U Mine? / Electricity
Black Treacle
Suck It And See
The Hellcat Spangled Shalalala
Don't Sit Down 'Cause I've Moved Your Chair
Crying Lightning
Da Frame 2R / Matador
Leave Before The Lights Come On
When The Sun Goes Down
I Bet You Look Good On The Dancefloor
>>>
Retrieve information about available tabs.
View Example
>>> guide = api.guide()
>>>
>>> guide
{'Home': 'FEmusic_home', 'Hotlist': 'FEmusic_trending', 'Library': 'FEmusic_liked'}
>>>
Retrieve information about shelves displayed on the home page.
View Example
>>> home = api.home()
>>>
>>> list(home)
['continuation', 'shelves']
>>>
>>> for shelf in home['shelves']:
print(shelf['name'])
Top charts
Relaxing to Country
>>>
>>> # Continue Data
>>> home2 = api.home(home['continuation'])
>>>
>>> for shelf in home2['shelves']:
print(shelf['name'])
Office radio
Today's hits
Cool vibes
>>>
Retrieve information about songs in the hotlist.
View Example
>>> hotlist = api.hotlist()
>>>
>>> for song in hotlist:
print(' & '.join(song['artist']['names']), '-', song['name'])
Ariana Grande & Justin Bieber - Stuck with U
Migos - Racks 2 Skinny
ManBetterKnow - Nang
Dua Lipa - Break My Heart
Little Mix - Break Up Song (Acoustic Version)
Grm Daily - S1mba - Rover (Remix) (ft. Poundz, ZieZie & Ivorian Doll) [Music Video] | GRM Daily
Lil Durk - Doin Too Much
Black Pepper - Black Pepper - Dublue
Hayley Williams - Dead Horse
slowthai - slowthai - ENEMY
GRM Daily - Offica - Face Reveal [Music Video] | GRM Daily
Example - Example - 'Erin' (Official Video) (OUT NOW)
Mumford & Sons - Forever
Headie One - HEADIE ONE - ROSE GOLD
Jaykae - Jaykae - Novocaine [feat. Remtrex] (Official Video)
NAV - No Debate
The 1975 - Me & You Together Song
SBK - SBK - Numbers (Official Video)
Sean Paul - Back It up Deh
Baauer - AETHER
>>>
>>>
Retrieve information about a playlist.
View Example
>>> playlist = api.playlist('RDCLAK5uy_lXWhlJsihey6xq1b50d7Uv93NLqle8TSc')
>>>
>>> list(playlist)
['name', 'type', 'year', 'thumbnail', 'duration', 'id', 'total_tracks', 'artist', 'radio', 'shuffle', 'tracks', 'continuation']
>>>
>>> playlist['name']
'Take It Easy Indie'
>>> playlist['year']
2020
>>> playlist['duration']
'6+ hours'
>>> playlist['total_tracks']
163
>>>
>>> for track in playlist['tracks'][:5]: # First 5 Tracks
print(track['artist']['name'], '-', track['name'])
Lord Huron - The Night We Met (feat. Phoebe Bridgers)
The Lumineers - Cleopatra
The xx - I Dare You
Hozier - Work Song
girl in red - we fell in love in october
>>>
>>> # Continue Data
>>> playlist2 = api.playlist(continuation = playlist['continuation'])
>>>
>>> for track in playlist2['tracks'][:5]: # First 5 Tracks
print(track['artist']['name'], '-', track['name'])
Maddie Jay - Mood Swings
Cloud Control - Dojo Rising
Broken Bells - The Angel and the Fool
George Glew - Bittersweet
Cold War Kids - First
>>>
Retrieve information about queued songs.
View Example
>>> queue = api.queue('Gz3-4UuMWjQ', 'Ye8Er8MtiLk')
>>>
>>> for song in queue:
print(song['artist']['name'], '-', song['name'])
Amber Run - Amen
Amber Run - Kites
>>>
>>> queue2 = api.queue(playlist_id = 'RDCLAK5uy_k5DxhF_vXgbHHoaQ_d2hRBlY11Qgr3xpY')
>>>
>>> for song in queue2[:5]: # First 5 Songs
print(song['artist']['name'], '-', song['name'])
Justin Bieber & N≠ - Sorry
Mark Ronson - Uptown Funk (feat. Bruno Mars)
Calvin Harris - This Is What You Came For (feat. Rihanna)
Fifth Harmony - Work from Home (feat. Ty Dolla $ign)
Katy Perry - Roar
>>>
Retrieve information about a specific search.
View Example
>>> results = api.search('alt-j')
>>>
>>> list(results)
['albums', 'playlists', 'videos', 'artists', 'songs', 'top_results']
>>>
>>> results['top_results']
'artists'
>>>
>>> results['artists'][0]['name']
'alt-J'
>>>
>>> results['albums'][0]['name']
'An Awesome Wave'
>>>
>>> results[results['top_results']][0]['name']
'alt-J'
>>>
Retrieve information about albums related to a specific search query.
View Example
>>> albums = api.search_albums('nevermind')
>>>
>>> for album in albums['items'][:5]: # First 5 Albums
print(album['artist']['name'], '-', album['name'])
Nirvana - Nevermind
Alexander Jean - Nevermind
Sex Pistols - Never Mind The Bollocks, Here's The Sex Pistols (40th Anniversary Deluxe Edition)
Poyo Spirit - Nevermind
Distruction Boyz, Zhao - Nevermind (Radio Edit)
>>>
>>> # Continue Data
>>> albums2 = api.search_albums(continuation = albums['continuation'])
>>>
>>> for album in albums2['items'][:5]: # First 5 Albums
print(album['artist']['name'], '-', album['name'])
Soriya - Nevermind
Wisp X - Nevermind
P_frmdatribe - Nevermind
Renel - Nevermind
Jacob Lee - Nevermind
>>>
Retrieve information about artists related to a specific search query.
View Example
>>> artists = api.search_artists('john')
>>>
>>> for artist in artists['items'][:5]: # First 5 Artists
print(artist['name'])
John Legend
John Mayer
John Newman
Elton John
John Lennon
>>>
>>> # Continue Data
>>> artists2 = api.search_artists(continuation = artists['continuation'])
>>>
>>> for artist in artists2['items'][:5]: # First 5 Artists
print(artist['name'])
John Mellencamp
John Carpenter
John Williams
John Coltrane
John Barry
>>>
Retrieve information about playlists related to a specific search query.
View Example
>>> playlists = api.search_playlists('indie')
>>>
>>> for playlist in playlists['items'][:5]: # First 5 Playlists
print(playlist['name'])
Coffee Shop Indie
Upbeat Indie Pop
Indie Folk Favorites
Indie Rock Chasers
Take It Easy Indie
>>>
>>> # Continue Data
>>> playlists2 = api.search_playlists(continuation = playlists['continuation'])
>>>
>>> for playlist in playlists2['items'][:5]: # First 5 Playlists
print(playlist['name'])
A Decade of Easy Indie
Indie Disco Overload
Sunshine Indie
Pop & Indie Easy Listening
00s Indie Guitar Anthems
>>>
Retrieve information about songs related to a specific search query.
View Example
>>> songs = api.search_songs('cry')
>>>
>>> for song in songs['items'][:5]: # First 5 Songs
print(song['artists'][0]['name'], '-', song['name'])
Rihanna - Cry
KAZKA - CRY (English Version)
James Blunt - Cry
Carly Rae Jepsen - Cry
System F - Cry (Original Extended)
>>>
>>> # Continue Data
>>> songs2 = api.search_songs(continuation = songs['continuation'])
>>>
>>> for song in songs2['items'][:5]: # First 5 Songs
print(song['artists'][0]['name'], '-', song['name'])
Marilyn Manson - Cry Little Sister
Jorja Smith - Don't Watch Me Cry
Oasis - Stop Crying Your Heart Out
Volbeat - I'm So Lonesome I Could Cry
Coldplay - Cry Cry Cry
>>>
Retrieve information about videos related to a specific search query.
View Example
>>> videos = api.search_videos('time')
>>>
>>> for video in videos['items'][:5]: # First 5 Videos
print(video['artist']['name'], '-', video['name'])
Lizzy Capri - Lizzy Capri - TIME (Official Lyric Video)
NF - Time
Pink Floyd Remasted Songs - Pink Floyd - Time (2011 Remastered)
Chase & Status - Time
Lil Baby - Time
>>>
>>> # Continue Data
>>> videos2 = api.search_videos(continuation = videos['continuation'])
>>>
>>> for video in videos2['items'][:5]: # First 5 Videos
print(video['artist']['name'], '-', video['name'])
Anjunadeep - Simon Doty feat. Forrest - This Time
Alesso - TIME
Imagine Dragons - It's Time
Culture Club - Time (Clock of the Heart)
Freddie Mercury - Time Waits For No One
>>>
Retrieve a list of search suggestions.
View Example
>>> suggestions = api.search_suggestions('foo fight')
>>>
>>> from pprint import pprint
>>>
>>> pprint(suggestions)
['foo fighters',
'foo fighters everlong',
'foo fighters times like these',
'foo fighters learn to fly',
'foo fighters best of you',
'foo fighters live',
'foo fighters pretender']
>>>
Retrieve information about a song/video.
View Example
>>> song = api.song('pPt_FZ9m2bM')
>>>
>>> list(song)
['rating', 'duration', 'description', 'thumbnail', 'name', 'id', 'views', 'dislikes', 'likes', 'explicit', 'recommended', 'date', 'artist']
>>>
>>> song['name']
'The Key to Life on Earth'
>>> song['artist']['name']
'Declan McKenna'
>>> song['views']
53130
>>> song['rating']
4.9008608
>>> song['duration']
247
>>> song['explicit']
False
>>> song['date']
{'year': 2020, 'month': 4, 'day': 14}
>>>
>>> for track in song['recommended'][:5]: # First 5 Tracks
print(track['artist']['name'], '-', track['name'])
Declan McKenna - British Bombs
None - Mix - The Key to Life on Earth
Declan McKenna - Beautiful Faces
Declan McKenna - Declan McKenna - The Key to Life on Earth (Official Video)
Will Joseph Cook - Will Joseph Cook - Girls Like Me
>>>
Retrieve information about a song/playlist being watched.
View Example
>>> watch = api.watch('dIwwjy4slI8', 'RDCLAK5uy_mgonaF5RiN90kqT-edkFB53N81dZ9XHp8')
>>>
>>> list(watch)
['id', 'name', 'continuation', 'total', 'tracks', 'radio', 'current']
>>>
>>> watch['name']
'Celestial Instrumentals'
>>> watch['total']
52
>>> watch['radio']
False
>>>
>>> for track in watch['tracks'][:5]: # First 5 tracks
print(track['name'])
Says
The Light
Sun Drugs
Dark Lights
Together Alone
>>>
>>> # Continue data
>>> watch2 = api.watch(continuation = watch['continuation'])
>>>
>>> for track in watch2['tracks'][:5]: # First 5 tracks
print(track['name'])
Goodnight
Indoor Swimming At The Space Station
Hoppípolla
Sleeping on the Roof
Zero Gravity
Retrieve information about a song/playlist being watched in radio mode.
View Example
>>> song_radio = api.watch_radio(song_id = '8A9_1hGmtj0')
>>>
>>> list(song_radio)
['id', 'name', 'continuation', 'total', 'tracks', 'radio', 'current']
>>>
>>> song_radio['id']
'RDAMVM8A9_1hGmtj0'
>>>
>>> for track in song_radio['tracks'][:5]: # First 5 Tracks
print(track['name'])
Travel Is Dangerous
A Gallant Gentleman
Motion Picture Soundtrack
Don't Stay Here
Everything In Its Right Place
>>>
>>> playlist_radio = api.watch_radio(playlist_id = 'RDCLAK5uy_kNj0whsN9sFy3dqiTCfu34HoOdeZIjfyw')
>>>
>>> list(playlist_radio)
['id', 'name', 'continuation', 'total', 'tracks', 'radio', 'current']
>>>
>>> playlist_radio['id']
'RDAMPLRDCLAK5uy_kNj0whsN9sFy3dqiTCfu34HoOdeZIjfyw'
>>> playlist_radio['name']
'Ambient Post Rock'
>>>
>>> for track in playlist_radio['tracks'][:5]: # First 5 Tracks
print(track['name'])
Only The Winds
The Winter
The Kindness In Letting Go
Aldgate Patterns
I'm Not
>>>
Retrieve information about a playlist being watched in shuffle mode.
View Example
>>> watch = api.watch_shuffle('RDCLAK5uy_l6Wg_lE2_Wx7GdOE21bKJvYPIif8n1fAQ')
>>>
>>> list(watch)
['id', 'name', 'continuation', 'total', 'tracks', 'radio', 'current']
>>>
>>> watch['name']
'Peaceful Indie Dreams'
>>>
>>> for track in watch['tracks'][:5]: # First 5 Tracks
print(track['name'])
Make It Holy
Bible Belt (Acoustic)
Take Care
Atlas Hands
Sweetheart, What Have You Done to Us
>>>