Skip to content

Instantly share code, notes, and snippets.

@yodaluca23
Last active July 19, 2024 20:09
Show Gist options
  • Select an option

  • Save yodaluca23/5465128b2c622c95f790fb93e3532433 to your computer and use it in GitHub Desktop.

Select an option

Save yodaluca23/5465128b2c622c95f790fb93e3532433 to your computer and use it in GitHub Desktop.
Ultimate Lyrics fetcher, gets lyrics from Beautiful Lyrics API, using Spotify SongID, uses Spotify API, with hacky ways to get token, to match song and artist to ID.
import requests
import re
def extract_track_id(spotify_input):
# Check if the input is a full Spotify URL
match = re.search(r'track/([a-zA-Z0-9]+)', spotify_input)
if match:
return match.group(1)
# Check if the input is just the track ID
elif re.match(r'^[a-zA-Z0-9]+$', spotify_input):
return spotify_input
else:
return None
def fetch_lyrics(track_id):
url = f'https://beautiful-lyrics.socalifornian.live/lyrics/{track_id}'
headers = {
'authorization': 'Bearer litterallyAnythingCanGoHereItJustTakesItLOL'
}
response = requests.get(url, headers=headers)
if response.status_code == 200 and response.headers.get('content-length') != '0':
return response.json()
return None
def parse_lyrics(data):
lyrics = []
if data['Type'] == 'Line':
for line in data['Content']:
if line['Type'] == 'Vocal':
lyrics.append(line['Text'])
elif data['Type'] == 'Syllable':
for item in data['Content']:
if item['Type'] == 'Vocal':
line = ' '.join([syllable['Text'] for syllable in item['Lead']['Syllables']])
lyrics.append(line)
if 'Background' in item:
for bg in item['Background']:
line = ' '.join([syllable['Text'] for syllable in bg['Syllables']])
lyrics.append(line)
return lyrics
def print_lyrics(lyrics):
for line in lyrics:
print(line)
def main():
spotify_input = input("Enter Spotify Track URL: ").strip()
track_id = extract_track_id(spotify_input)
if not track_id:
print("Invalid Spotify URL or ID")
return
data = fetch_lyrics(track_id)
if not data:
print("No lyrics found for this track.")
return
lyrics = parse_lyrics(data)
print("")
print("Lyrics:")
print("")
print_lyrics(lyrics)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment