Spaces:
Sleeping
Sleeping
File size: 6,062 Bytes
dfd2747 610e92c dfd2747 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# from .requester import Requester
# from .tools import *
from requester import Requester
from tools import *
class AZlyrics(Requester):
"""
Fast and Secure API for AZLyrics.com.
Attributes:
title (str): song title.
artist (str): artist name.
search_engine (str): search engine used to assist scraping lyrics.
- currently available: 'google', 'duckduckgo'.
accuracy (float): used to determine accuracy via jaro algorithm.
proxies (dict): if you want to use proxy while connecting to 'AZLyrics.com'.
"""
def __init__(self, search_engine='', accuracy=0.6, proxies={}):
self.title = ''
self.artist = ''
self.search_engine = search_engine
self.accuracy = accuracy
if not 0 < accuracy <= 1:
self.accuracy = 0.6
self.proxies = proxies
self.lyrics_history = []
self.lyrics = ''
self.songs = {}
def getLyrics(self, url=None, ext='txt', save=False, path='', sleep=3):
"""
Retrieve Lyrics for a given song details.
Parameters:
url (str): url of the song's Azlyrics page.
ext (str): extension of the lyrics saved file, default is ".txt".
save (bool): allow to or not to save lyrics in a file.
sleep (float): cooldown before next request.
Returns:
lyrics (str): Lyrics of the detected song.
"""
# Best cooldown is 5 sec
time.sleep(sleep)
link = url
if not url:
# v3.0.5: No need for artist and title if url is found
if not self.artist + self.title:
raise ValueError("Both artist and title can't be empty!")
if self.search_engine:
# If user can't remember the artist,
# he can search by title only
# Get AZlyrics url via Google Search
link = googleGet(
self.search_engine,
self.accuracy,
self.get,
self.artist,
self.title,
0,
self.proxies
)
if not link:
return 0
else:
# Sometimes search engines block you
# If happened use the normal get method
link = normalGet(
self.artist,
self.title,
0)
page = self.get(link, self.proxies)
if page.status_code != 200:
if not self.search_engine:
print('Failed to find lyrics. Trying to get link from Google')
self.search_engine = 'google'
lyrics = self.getLyrics(url=url, ext=ext, save=save, path=path, sleep=sleep)
self.search_engine = ''
return lyrics
else:
print('Error',page.status_code)
return 1
# Getting Basic metadata from azlyrics
metadata = [elm.text for elm in htmlFindAll(page)('b')]
# if metadata is empty, then it's not a valid page
if not metadata:
print('Error', 'no metadata')
return 1
# v3.0.4: Update title and artist attributes with exact names
self.artist = filtr(metadata[0][:-7], True)
self.title = filtr(metadata[1][1:-1], True)
lyrics = parseLyric(page)
self.lyrics = lyrics.strip()
# Saving Lyrics
if lyrics:
if save:
# v3.0.2: Adding custom path
p = os.path.join(
path,
'{} - {}.{}'.format(
self.title.title(),
self.artist.title(),
ext
)
)
with open(p, 'w', encoding='utf-8') as f:
f.write(lyrics.strip())
# Store lyrics for later usage
self.lyrics_history.append(self.lyrics)
return self.lyrics
self.lyrics = 'No lyrics found :('
return 2
def getSongs(self, sleep=3):
"""
Retrieve a dictionary of songs with their links.
Parameters:
sleep (float): cooldown before next request.
Returns:
dict: dictionary of songs with their links.
"""
if not self.artist:
raise Exception("Artist can't be empty!")
# Best cooldown is 5 sec
time.sleep(sleep)
if self.search_engine:
link = googleGet(
self.search_engine,
self.accuracy,
self.get,
self.artist,
'',
1,
self.proxies
)
if not link:
return {}
else:
link = normalGet(
self.artist,
'',
1)
albums_page = self.get(link, self.proxies)
if albums_page.status_code != 200:
if not self.search_engine:
print('Failed to find songs. Trying to get link from Google')
self.search_engine = 'google'
songs = self.getLyrics(sleep=sleep)
self.search_engine = ''
return songs
else:
print('Error',albums_page.status_code)
return {}
# Store songs for later usage
self.songs = parseSongs(albums_page)
return self.songs
|