Skip to content

Commit

Permalink
[Re-Fixed] Single albums return relative urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
elmoiv authored Jan 2, 2022
1 parent 496a17b commit 3e5e911
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 174 deletions.
12 changes: 6 additions & 6 deletions azapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .azapi import AZlyrics

__name__ = "azapi.azapi.AZlyrics"
__author__ = "Khaled H. El-Morshedy"
__url__ = "https://github.com/elmoiv/azapi"
__description__ = "Get Lyrics from AZLyrics.com like a Boss ~(0_0)~"
__license__ = "GPL-v3.0"
__version__ = "3.0.6"
__name__ = 'azapi.azapi.AZlyrics'
__author__ = 'Khaled H. El-Morshedy'
__url__ = 'https://github.com/elmoiv/azapi'
__description__ = 'Get Lyrics from AZLyrics.com like a Boss ~(0_0)~'
__license__ = 'GPL-v3.0'
__version__ = '3.0.7'
116 changes: 60 additions & 56 deletions azapi/azapi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os
import time

from .requester import Requester
from .tools import *


class AZlyrics(Requester):
"""
Fast and Secure API for AZLyrics.com
Expand All @@ -17,33 +13,32 @@ class AZlyrics(Requester):
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={}, title="", artist=""):
self.title = title
self.artist = artist
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.lyrics = ''
self.songs = {}

def getLyrics(self, url=None, ext="txt", save=False, path="", sleep=3):
def getLyrics(self, url=None, ext='txt', save=False, path='', sleep=3):
"""
Reterive Lyrics for a given song details
Parameters:
url (str): url of the song's Azlyrics page.
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.
sleep (float): cooldown before next request.
Returns:
lyrics (str): Lyrics of the detected song
"""
Expand All @@ -60,32 +55,35 @@ def getLyrics(self, url=None, ext="txt", save=False, path="", sleep=3):
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,
)
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)
link = normalGet(
self.artist,
self.title,
0)

page = self.get(link, self.proxies)
if page.status_code != 200:
print("Error", page.status_code)
print('Error 404!')
return 1

# Getting Basic metadata from azlyrics
metadata = [elm.text for elm in htmlFindAll(page)("b")]

metadata = [elm.text for elm in htmlFindAll(page)('b')]
# 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)
Expand All @@ -98,58 +96,64 @@ def getLyrics(self, url=None, ext="txt", save=False, path="", sleep=3):
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:
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 :("
self.lyrics = 'No lyrics found :('
return 2

def getSongs(self, sleep=3):
"""
Reterive a dictionary of songs with their links
Parameters:
sleep (float): cooldown before next request.
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,
)
self.search_engine,
self.accuracy,
self.get,
self.artist,
'',
1,
self.proxies
)
if not link:
return {}
else:
link = normalGet(self.artist, "", 1)

link = normalGet(
self.artist,
'',
1)

albums_page = self.get(link, self.proxies)
if albums_page.status_code != 200:
print("Error", albums_page.status_code)
print('Error 404!')
return {}

# Store songs for later usage
self.songs = parseSongs(albums_page)

return self.songs
return self.songs
35 changes: 18 additions & 17 deletions azapi/jaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@
# Source: https://www.geeksforgeeks.org/jaro-and-jaro-winkler-similarity/
# This code is contributed by mohit kumar 29 (GeeksforGeeks.com)
from math import floor


def jaro_distance(s1, s2):
if s1 == s2:

def jaro_distance(s1, s2):
if (s1 == s2):
return 1.0

len1, len2 = len(s1), len(s2)
max_dist = floor(max(len1, len2) / 2) - 1
match = 0
hash_s1, hash_s2 = [0] * len(s1), [0] * len(s2)

for i in range(len1):
for j in range(max(0, i - max_dist), min(len2, i + max_dist + 1)):
if s1[i] == s2[j] and hash_s2[j] == 0:
for j in range(max(0, i - max_dist),
min(len2, i + max_dist + 1)):
if (s1[i] == s2[j] and hash_s2[j] == 0):
hash_s1[i], hash_s2[j] = 1, 1
match += 1
break

if match == 0:
if (match == 0):
return 0.0

t = 0
point = 0

for i in range(len1):
if hash_s1[i]:
while hash_s2[point] == 0:
for i in range(len1):
if (hash_s1[i]):
while (hash_s2[point] == 0):
point += 1

if s1[i] != s2[point]:
if (s1[i] != s2[point]):
point += 1
t += 1
t = t // 2
t = t//2

return (match / len1 + match / len2 + (match - t + 1) / match) / 3.0
return (match/ len1 + match / len2 +
(match - t + 1) / match)/ 3.0
29 changes: 9 additions & 20 deletions azapi/requester.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import random
import requests, random

import requests

userAgents = """Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
userAgents = '''Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.11 Safari/535.19
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11
Expand Down Expand Up @@ -41,23 +39,14 @@
Mozilla/5.0 (Macintosh; U; Mac OS X 10_5_7; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/ Safari/530.5
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-US) AppleWebKit/530.9 (KHTML, like Gecko) Chrome/ Safari/530.9
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-US) AppleWebKit/530.6 (KHTML, like Gecko) Chrome/ Safari/530.6
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/ Safari/530.5"""


class Requester:
USER_AGENTS = userAgents.split("\n")
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/ Safari/530.5'''

class Requester():
USER_AGENTS = userAgents.split('\n')

# Inspired from: https://github.com/brianchesley/Lyrics/blob/master/lyrics_data_scrape.py
def get(self, url, _proxies={}):
return requests.get(
url,
headers={"User-Agent": random.choice(self.USER_AGENTS)},
proxies=_proxies,
)

return requests.get(url, headers={'User-Agent': random.choice(self.USER_AGENTS)}, proxies=_proxies)

def head(self, url, _proxies={}):
return requests.head(
url,
headers={"User-Agent": random.choice(self.USER_AGENTS)},
proxies=_proxies,
)
return requests.head(url, headers={'User-Agent': random.choice(self.USER_AGENTS)}, proxies=_proxies)
Loading

0 comments on commit 3e5e911

Please sign in to comment.