-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLyricsFetcher.py
72 lines (54 loc) · 1.7 KB
/
LyricsFetcher.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
from bs4 import BeautifulSoup
import re
import urllib2
import urllib
import sys
def GetLyrics(url,filename,artist):
soup = BeautifulSoup(urllib2.urlopen(url))
a = soup.find('div' , class_ = 'ringtone')
b = a.next_siblings #returns a list of all the elements which are on same level as element a
for sib in b:
if type(sib)!= str :
tmp = str(sib)
if tmp[0:5]=='<div>': #the first div tag after ringtone contains the lyrics
lyrics = sib
break
#print lyrics
f = open(filename , 'w')
f.write(filename[:-4].upper()+'('+ artist+')\n\n')
for string in lyrics.strings:
string = string.encode('utf-8') #in python2 we cant print unicode strings which cant be converted to ascii
f.write(string)
f.close()
def RemoveSpaces(A):
B = ''
for i in xrange(0,len(A)):
if(A[i]==' '):
continue
else:
B+=A[i]
return B
def main():
try:
artist = raw_input('Enter name of artist : ')
artist = RemoveSpaces(artist.lower())
song_name = raw_input('Enter name of song : ')
song_name = RemoveSpaces(song_name.lower())
url = 'http://www.azlyrics.com/lyrics/'+artist + '/'+ song_name+ '.html'
urllib2.urlopen(url)
except urllib2.HTTPError as err:
#print err.code
#print 'Please check the name of the artist and the song.'
if err.code == 404:
print 'Please check the name of the artist and the song.'
print 'The url',url,'does not exist'
elif err.code == 403:
print 'Access Denied!'
else:
print 'The following error occured : err',err.code
else:
print 'Fetching lyrics for song "' + song_name + ' "by '+ artist + '...'
GetLyrics(url,song_name+'.txt',artist)
print 'Lyrics saved in file "'+ song_name+'.txt" '
if __name__ == '__main__':
main()