-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics_find.py
64 lines (55 loc) · 1.89 KB
/
lyrics_find.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
"""
find_lyrics, a simple text based lyrics retrieval application.
Srinidhi Kaushik, shrinidhi.kaushik@gmail.com, 06/06/13.
For colorama:
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL
"""
#!/usr/bin/python
import re
import urllib2
from colorama import init, Fore, Back, Style
from bs4 import BeautifulSoup
def find_lyrics(song, band):
song_copy = song
band_copy = band
song = re.sub('[^A-Za-z0-9]', '+', song.lower())
band = re.sub('[^A-Za-z0-9]', '+', band.lower())
song = re.sub('\+\+', '', song)
band = re.sub('\+\+', '', band)
match_expr = band + "\/" + song
flag = 0
possible = []
print(song)
html_page = urllib2.urlopen("http://search.azlyrics.com/search.php?q=" + str(song))
soup = BeautifulSoup(html_page)
for link in soup.find_all('a', {'href': re.compile(match_expr)}):
possible.append(link.get('href'))
print(possible)
if (len(possible) == 0):
print(Fore.RED + Style.BRIGHT + "Error: Couldn\'t retrieve lyrics.")
exit()
print("\n" + Fore.WHITE + Back.GREEN + Style.BRIGHT + song_copy + Style.RESET_ALL + " by " + Fore.WHITE + Back.CYAN + Style.BRIGHT + band_copy + Style.RESET_ALL + ":")
new_html = urllib2.urlopen(possible[0]).readlines()
"""for line in new_html:
if (re.match("<!-- start of lyrics -->", line)):
flag = 1
elif (re.match("<!-- end of lyrics -->", line)):
flag = 0
if (flag == 1):
line = re.sub('<[^<]+?>', '', line)
if (line != "\n" or line != ""):
line = re.sub("\n", '', line)
print(line)"""
new_soup = BeautifulSoup(new_html)
for i in new_soup.find_all("div", {"style":"margin-left:10px;margin-right:10px;"}):
print(i)
def main():
print("find_lyrics")
song = raw_input("Song: ")
band = raw_input("Artist/Band name: ")
find_lyrics(str(song), str(band))
print("")
if __name__ == '__main__':
main()