keivalya commited on
Commit
495d339
·
verified ·
1 Parent(s): 711729c

Create genius.py

Browse files
Files changed (1) hide show
  1. genius.py +46 -0
genius.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+
4
+ def get_song_info_from_lyrics(lyrics):
5
+ """
6
+ Fetch song information from Genius API using lyrics.
7
+
8
+ Args:
9
+ lyrics (str): A snippet of the song lyrics.
10
+ genius_api_token (str): Your Genius API token.
11
+
12
+ Returns:
13
+ dict: Information about the song (e.g., title, artist, URL) or None if not found.
14
+ """
15
+ base_url = "https://api.genius.com/search"
16
+ headers = {
17
+ "Authorization": f"Bearer {os.getenv("GENIUS_API_TOKEN")}"
18
+ }
19
+
20
+ params = {
21
+ "q": lyrics
22
+ }
23
+
24
+ try:
25
+ response = requests.get(base_url, headers=headers, params=params)
26
+ response.raise_for_status()
27
+
28
+ data = response.json()
29
+
30
+ # Check if there are hits in the response
31
+ if data.get("response") and data["response"].get("hits"):
32
+ hits = data["response"]["hits"]
33
+ if hits:
34
+ # Return the first song match's details
35
+ song = hits[0]["result"]
36
+ print(song)
37
+ return {
38
+ "title": song["title"],
39
+ "artist": song["primary_artist"]["name"],
40
+ "url": song["url"]
41
+ }
42
+ return None
43
+
44
+ except requests.exceptions.RequestException as e:
45
+ print(f"An error occurred while making the API call: {e}")
46
+ return None