forked from FrancescoGuarneri/AzLyricsAPI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
azlyrics.py
74 lines (57 loc) · 2.22 KB
/
azlyrics.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
74
import argparse, sys, re
import urllib.request, urllib.error
from bs4 import BeautifulSoup
from azlyrics.cache import Cache
class Azlyrics(object):
def __init__(self, artist, music):
self.artist = artist
self.music = music
def normalize_str(self, str):
return re.sub(r'\W+', '', str.lower())
def normalize_artist_music(self):
return self.normalize_str(self.artist), self.normalize_str(self.music)
def url(self):
if not self.artist and not self.music:
self.artist = "rickastley"
self.music = "nevergonnagiveyouup"
return "http://azlyrics.com/lyrics/{}/{}.html".format(*self.normalize_artist_music())
def get_page(self):
try:
page = urllib.request.urlopen(self.url())
return page.read()
except urllib.error.HTTPError as e:
if e.code == 404:
print("Music not found")
sys.exit(1)
def extract_lyrics(self, page):
soup = BeautifulSoup(page, "html.parser")
lyrics_tags = soup.find_all("div", attrs={"class": None, "id": None})
lyrics = [tag.getText() for tag in lyrics_tags]
return lyrics
def format_lyrics(self, lyrics):
formated_lyrics = "\n".join(lyrics)
return formated_lyrics
def format_title(self):
return "{} by {}".format(self.music.title(), self.artist.title())
def get_lyrics(self):
page = self.get_page()
lyrics = self.extract_lyrics(page)
return lyrics
def run():
parser = argparse.ArgumentParser(description="Fetch lyric from azlyric")
parser.add_argument("artist", metavar="Artist", type=str)
parser.add_argument("music", metavar="Music", type=str)
parser.add_argument("-s", "--save", metavar="path", help="Save to the file", default=False, dest="path")
args = parser.parse_args()
c = Cache()
az = Azlyrics(args.artist, args.music)
cache_key = '_'.join(az.normalize_artist_music())
lyrics = c.get(cache_key)
if lyrics is None:
lyrics = az.format_lyrics(az.get_lyrics())
c.add(cache_key, lyrics)
if args.path:
save_lyrics_to_file(args.path, lyrics)
else:
print(az.format_title())
print(lyrics)