'Using Genius API

The docs are here

I'm new to using api's and I've been trying to figure out how to use this with no luck. I have the client id, secret id, and access token already.

 

What I'm trying to do is:

-get artist id  
-get all the songs by an artist   
-get the lyrics to a song

 

I don't really have any code right now because I can't figure out how to call the api besides like

import requests

genius_client_id = ''
genius_secret_id = ''
genius_client_access_token = ''

base_url = 'https://api.genius.com/'

r = requests.get(*insert api call here*)
print(r)

Any guidance would be appreciated. Thanks.

 


 

edit:

I have this working right now - input artist name and a song and it will return data. Now how would I go through that data - like how would I get the 'full_title', 'id', or the lyrics?

#search for song
import requests

client_access_token = ''
base_url = 'https://api.genius.com'

user_input = input('artist and song: ').replace(" ", "-")

path = 'search/'
request_uri = '/'.join([base_url, path])
print(request_uri + user_input)

params = {'q': user_input}

token = 'Bearer {}'.format(client_access_token)
headers = {'Authorization': token}

r = requests.get(request_uri, params=params, headers=headers)
print(r.text)


Solution 1:[1]

Looks like you're pretty close. I just took a quick look, but bet you need something like this.

import requests

payload={
'genius_client_id' : '',
'genius_secret_id' : '',
'genius_client_access_token' : ''}

base_url = 'https://api.genius.com/'

r = requests.get(base_url, params=payload)
print(r.status_code) #200 is good

P.S. the requests docs are very helpful, might take a look there too

Solution 2:[2]

You are pretty close to getting the details of the songs(lyrics, id,etc..). All you have to do is go through the JSON element. The JSON file contains information about your search. In this case, the file will contain data about many songs that appear in the search but I will show how to navigate to the lyrics of the first song.

#this gives the link for the lyrics page of the searched song.
#This is for the first result of the search. You can use a for loop if you want the URL for more results.

URL = json['response']['hits'][0]['result']['url']
print(URL)

#to get the lyrics out of the page I used beautifulsoup in the following way.
page = requests.get(url)
html = BeautifulSoup(page.text, 'html.parser')
lyrics = html.find('div', class_='lyrics').get_text()
lyrics = re.sub(r'[\(\[].*?[\)\]]', '', lyrics)
print(lyrics)

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SuperStew
Solution 2 murali krishna