From db04d89bd62b65581464f44a04e1f94469a4f8c7 Mon Sep 17 00:00:00 2001 From: Khaled ElMorshedy Date: Sat, 25 Jul 2020 13:14:46 +0200 Subject: [PATCH] Updated to v3.0.2 --- azapi/__init__.py | 4 ++-- azapi/azapi.py | 32 +++++++++++++++++++++++++------- azapi/tools.py | 33 +++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/azapi/__init__.py b/azapi/__init__.py index 1256219..0daee8f 100644 --- a/azapi/__init__.py +++ b/azapi/__init__.py @@ -4,5 +4,5 @@ __author__ = 'Khaled H. El-Morshedy' __url__ = 'https://github.com/elmoiv/azapi' __description__ = 'Get Lyrics from AZLyrics.com like a Boss ~(0_0)~' -__license__ = 'MIT' -__version__ = '3.0.1' +__license__ = 'GPL-v3.0' +__version__ = '3.0.2' diff --git a/azapi/azapi.py b/azapi/azapi.py index 7374d61..2edb614 100644 --- a/azapi/azapi.py +++ b/azapi/azapi.py @@ -25,7 +25,11 @@ def __init__(self, search_engine='', accuracy=0.6, proxies={}): self.proxies = proxies - def getLyrics(self, url=None, ext='txt', save=False, sleep=3): + self.lyrics_history = [] + self.lyrics = '' + self.songs = {} + + def getLyrics(self, url=None, ext='txt', save=False, path='', sleep=3): """ Reterive Lyrics for a given song details @@ -81,17 +85,29 @@ def getLyrics(self, url=None, ext='txt', save=False, sleep=3): title = filtr(metadata[1][1:-1], True) lyrics = ParseLyric(page) + self.lyrics = lyrics.strip() # Saving Lyrics if lyrics: if save: - with open('{} - {}.{}'.format( - title.title(), - artist.title(), - ext), 'w', encoding='utf-8') as f: + # v3.0.2: Adding custom path + p = os.path.join( + path, + '{} - {}.{}'.format( + title.title(), + artist.title(), + ext + ) + ) + + with open(p, 'w', encoding='utf-8') as f: f.write(lyrics.strip()) - return lyrics.strip() + + # Store lyrics for later usage + self.lyrics_history.append(lyrics) + return self.lyrics + self.lyrics = 'No lyrics found :(' return 2 def getSongs(self, sleep=3): @@ -132,4 +148,6 @@ def getSongs(self, sleep=3): print('Error 404!') return {} - return ParseSongs(albums_page) \ No newline at end of file + # Store songs for later usage + self.songs = ParseSongs(albums_page) + return self.songs \ No newline at end of file diff --git a/azapi/tools.py b/azapi/tools.py index b522396..bdf475b 100644 --- a/azapi/tools.py +++ b/azapi/tools.py @@ -1,4 +1,4 @@ -import bs4, re, time +import bs4, re, time, os from urllib.parse import quote from .jaro import jaro_distance @@ -7,13 +7,19 @@ def htmlFind(page): # v3.0 # Changed page.text -> page.content.decode() to support variant unicodes - soup = bs4.BeautifulSoup(page.content.decode(), "html.parser") + soup = bs4.BeautifulSoup( + page.content.decode(), + "html.parser" + ) return soup.find def htmlFindAll(page): # v3.0 # Changed page.text -> page.content.decode() to support variant unicodes - soup = bs4.BeautifulSoup(page.content.decode(), "html.parser") + soup = bs4.BeautifulSoup( + page.content.decode(), + "html.parser" + ) return soup.findAll def filtr(inpt, isFile=False): @@ -43,8 +49,10 @@ def GoogleGet(srch_eng, acc, get_func, artist='', title='', _type=0): slctd_srch_engn = srch_eng google_page = get_func('{}{}+site%3Aazlyrics.com'.format( - search_engines[slctd_srch_engn], - encoded_data)) + search_engines[slctd_srch_engn], + encoded_data + ) + ) # Choose between lyrics or song according to function used regex = [ @@ -54,7 +62,10 @@ def GoogleGet(srch_eng, acc, get_func, artist='', title='', _type=0): # ex result: [('azlyrics.com/t/taylorswift.html', 'taylorswift')] # result[0][0] = 'azlyrics.com/t/taylorswift.html' - results = re.findall(regex[_type], google_page.text) + results = re.findall( + regex[_type], + google_page.text + ) if len(results): # calculate jaro similarity for artist and title @@ -62,9 +73,15 @@ def GoogleGet(srch_eng, acc, get_func, artist='', title='', _type=0): jaro_title = 1.0 if artist: - jaro_artist = jaro_distance(artist.replace(' ', ''), results[0][1]) + jaro_artist = jaro_distance( + artist.replace(' ', ''), + results[0][1] + ) if title: - jaro_title = jaro_distance(title.replace(' ', ''), results[0][2]) + jaro_title = jaro_distance( + title.replace(' ', ''), + results[0][2] + ) if jaro_artist >= acc and jaro_title >= acc: return 'https://www.' + results[0][0]