Skip to content

Commit

Permalink
Updated to v3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
elmoiv authored Jul 25, 2020
1 parent 5c56c03 commit db04d89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
4 changes: 2 additions & 2 deletions azapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
32 changes: 25 additions & 7 deletions azapi/azapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -132,4 +148,6 @@ def getSongs(self, sleep=3):
print('Error 404!')
return {}

return ParseSongs(albums_page)
# Store songs for later usage
self.songs = ParseSongs(albums_page)
return self.songs
33 changes: 25 additions & 8 deletions azapi/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bs4, re, time
import bs4, re, time, os
from urllib.parse import quote
from .jaro import jaro_distance

Expand All @@ -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):
Expand Down Expand Up @@ -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 = [
Expand All @@ -54,17 +62,26 @@ 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
jaro_artist = 1.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]
Expand Down

0 comments on commit db04d89

Please sign in to comment.