-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrics_parser.py
73 lines (67 loc) · 2.59 KB
/
lyrics_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from dotenv import load_dotenv
from bs4 import BeautifulSoup
import requests
import json
import os
class GeniusApi():
def __init__(self, access_token, song, artist):
self.token = access_token
self.song = song
self.artist = artist
def search(self, page_limit):
url = "http://api.genius.com/search"
headers = {'Authorization': "Bearer " + self.token}
data = {'q': self.song, 'per_page': page_limit}
response = requests.get(url, headers=headers, params=data)
if response.status_code == 200:
data = response.json()
# with open('api-data.json', 'w+') as f:
# json.dump(data, f, indent=2, sort_keys=True)
#f = open('api-data.json', 'r')
#api_data = f.read()
#data = json.loads(api_data)
try:
for hit in data["response"]["hits"]:
if hit["result"]["primary_artist"]["name"] == self.artist:
song_info = hit
break
if song_info:
song_api_path = song_info["result"]["api_path"]
return song_api_path
except Exception:
return None
else:
print("Request Failed!")
# print(response.text)
return None
def get_lyrics(self, song_param):
base_url = "http://api.genius.com"
song_url = base_url + song_param
headers = {'Authorization': "Bearer " + self.token}
response = requests.get(song_url, headers=headers)
json = response.json()
path = json["response"]["song"]["path"]
page_url = "http://genius.com" + path
page = requests.get(page_url)
soup = BeautifulSoup(page.text, "html.parser")
# Removing annotation script tags
[h.extract() for h in soup('script')]
# Searching the css class lyrics
lyrics = soup.find('div', class_='lyrics').get_text()
return lyrics
if __name__ == "__main__":
env_file = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(env_file)
# Requires access token from genius
token = os.environ.get('GENIUS_TOKEN')
music_name = 'Wanna Know'
music_artist = 'Dave'
g = GeniusApi(token, music_name, music_artist)
song_param = g.search(10)
if song_param is not None:
lyrics = g.get_lyrics(song_param)
if lyrics:
with open(music_name + " Lyrics.txt", 'w+') as f:
f.write(lyrics.rstrip().strip())
else:
print(f"Lyrics for {music_name} by {music_artist} not found!")