Last active
July 19, 2024 20:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from bs4 import BeautifulSoup | |
| import urllib.parse | |
| import re | |
| import json | |
| def get_bearer_token(): | |
| playlist_id = "6U7CJSEwgsrb41wXEPcr96" | |
| fetch_url = f"https://open.spotify.com/playlist/{playlist_id}" | |
| response = requests.get(fetch_url) | |
| response.raise_for_status() # Check if the request was successful | |
| html_content = response.text | |
| soup = BeautifulSoup(html_content, 'html.parser') | |
| session_element = soup.find(id="session") | |
| session_html = session_element.get_text() | |
| tokens = json.loads(session_html) | |
| access_token = tokens['accessToken'] | |
| return access_token | |
| def search_spotify(artist, song): | |
| token = get_bearer_token() | |
| url = f'https://api.spotify.com/v1/search?q=remaster%2520track%3A{song}%2520artist%253A{artist}&type=track&limit=1' | |
| headers = { | |
| 'Authorization': f'Bearer {token}' | |
| } | |
| response = requests.get(url, headers=headers) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data['tracks']['items']: | |
| href = data['tracks']['items'][0]['href'] | |
| match = re.search(r'tracks/([a-zA-Z0-9]+)', href) | |
| if match: | |
| song_id = match.group(1) | |
| return song_id | |
| else: | |
| raise ValueError("Song ID not found in the href.") | |
| else: | |
| raise ValueError("No tracks found for the given artist and song.") | |
| else: | |
| raise Exception(f"Spotify API request failed with status code {response.status_code}") | |
| 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(): | |
| artist = input("Enter Artist Name: ").strip() | |
| song = input("Enter Song Title: ").strip() | |
| # Search Spotify and get track ID | |
| track_id = search_spotify(artist, song) | |
| if not track_id: | |
| print("No track found on Spotify.") | |
| return | |
| # Fetch lyrics using track ID | |
| data = fetch_lyrics(track_id) | |
| if not data: | |
| print("No lyrics found for this track.") | |
| return | |
| # Parse and print lyrics | |
| lyrics = parse_lyrics(data) | |
| print("") | |
| print("Lyrics:") | |
| print("") | |
| print_lyrics(lyrics) | |
| print("") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment