import acrcloud import os import eyed3 import requests import json from…
- User
import acrcloud import os import eyed3 import requests import json from acrcloud.recognizer import ACRCloudRecognizer from bs4 import BeautifulSoup
ACRCloud API credentials
ACR_HOST = "" ACR_ACCESS_KEY = "" ACR_ACCESS_SECRET = ""
Genius API access token
GENIUS_ACCESS_TOKEN = ""
ACR Cloud setup
config = { 'host': ACR_HOST, 'access_key': ACR_ACCESS_KEY, 'access_secret': ACR_ACCESS_SECRET, 'timeout': 10 # seconds } dir(acrcloud)
Initialize the ACRCloud recognizer
recognizer = ACRCloudRecognizer(config)
Function to recognize the song from an audio file
def recognize_song(audio_file_path): buffer = open(audio_file_path, 'rb').read() result = recognizer.recognize_by_filebuffer(buffer, 0) try: result_dict = json.loads(result) # Parse the JSON string into a dictionary return result_dict['metadata']['music'][0] except (KeyError, IndexError, json.JSONDecodeError) as e: print(f"Error while parsing result: {e}") return None
Function to get lyrics from Genius
Function to get lyrics from Genius
def get_lyrics_from_genius(song_title, artist_name): base_url = "https://api.genius.com" headers = {'Authorization': f'Bearer {GENIUS_ACCESS_TOKEN}'} search_url = base_url + "/search" data = {'q': f"{song_title} {artist_name}"} params = {'text_format': 'plain'} # Filter out unnecessary information response = requests.get(search_url, params=params, data=data, headers=headers)
if response.ok: # Grab the first song from the results search_response = response.json() hit_list = search_response['response']['hits'] if hit_list: # Extract the API path for the top hit lyrics song_api_path = hit_list[0]['result']['api_path'] # Now we access the actual lyrics song_url = base_url + song_api_path song_response = requests.get(song_url, headers=headers) if song_response.ok: song_info = song_response.json() lyrics_path = song_info['response']['song']['path'] # Construct the URL for the lyrics page lyrics_page_url = "https://genius.com" + lyrics_path page = requests.get(lyrics_page_url) # Use BeautifulSoup (or similar) to scrape the lyrics html = BeautifulSoup(page.text, "html.parser") # This will vary depending on the site's layout at the time you check. # You need to inspect the page to find the correct container for the lyrics. # For instance, if lyrics reside within a <p> tag within a <div> with a class like 'lyric-box', you may use: # Modify this selector based on the actual structure of the Genius lyrics page. lyrics_div = html.find("div", class_="lyrics") if lyrics_div: # Join all paragraphs within the lyrics division (if structured as such) lyrics_p_tags = lyrics_div.find_all("p") lyrics = "\n".join(p_tag.get_text(strip=True) for p_tag in lyrics_p_tags) return lyrics else: print("Couldn't find the lyrics division.") return None else: print("Couldn't fetch the song info from Genius.") return None else: print("No search hits on Genius for the provided song title and artist name.") return None else: print("Search request to Genius failed.") return None - Assistant