Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created September 3, 2023 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/fc3d7170c535f188bf7bc446b19b157a to your computer and use it in GitHub Desktop.
Save ehzawad/fc3d7170c535f188bf7bc446b19b157a to your computer and use it in GitHub Desktop.
from ytmusicapi import YTMusic
# Initialize YTMusic using your OAuth credentials
ytmusic = YTMusic("oauth.json")
# Search for a song
search_results = ytmusic.search('Blank Space By Taylor Swift')
# Check if search results are empty
if not search_results:
print("No results found for the search query.")
else:
# Find the first result with 'resultType' set to 'song'
song_result = next((item for item in search_results if item['resultType'] == 'song'), None)
if song_result:
print("Found song:", song_result)
# Fetch and print the song's metadata using the 'videoId'
song_metadata = ytmusic.get_song(song_result['videoId'])
print("Song metadata:", song_metadata)
else:
print("No song found in the search results.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment