-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics.py
156 lines (128 loc) · 4.25 KB
/
lyrics.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import unicode_literals
import requests
from bs4 import BeautifulSoup as bs
from queue import Queue
import sys
import webbrowser
reload(sys)
sys.setdefaultencoding('utf8')
supported = ['azlyrics',
'metrolyrics',
'lyricsmint',
'glamsham',
'allthelyrics']
def getLyrics(query, q, show=True):
query += " azlyrics"
# headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }
base = "https://www.google.co.in/search"
try:
r = requests.get(base, params={'q': query})
except:
lyr = "Unable to connect to Intenet."
q.put(lyr)
return
soup = bs(r.text, "html.parser")
tags = soup.find_all('h3')
url = ''
for i in range(8):
try:
url = "https://www.google.co.in/" + tags[i].contents[0]['href']
except:
lyr = "Sorry, Lyrics not found."
q.put(lyr)
return
for site in supported:
if site in url:
break
else:
url = ''
continue
break
# print url
try:
if url == '': # No supported site
lyr = "Sorry, Lyrics not found.2"
elif show is False:
webbrowser.open(url)
lyr = "Opened in browser."
elif 'azlyrics' in url:
lyr = azScrape(url)
elif 'metrolyrics' in url:
lyr = metroScrape(url)
elif 'lyricsmint' in url:
lyr = lyricsmintScrape(url)
elif 'allthelyrics' in url:
lyr = allthelyricsScrape(url)
elif 'glamsham Lyrics' in url:
lyr = glamshamScrape(url)
else:
lyr = "Sorry, Lyrics not found."
except Exception as e:
lyr = "Sorry, Lyrics not found."
print "Error.", str(e)
q.put(lyr)
def azScrape(url):
# Faking user agent using headers as azlyrics blocks scraper scripts.
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
r = requests.get(url, headers=headers)
soup = bs(r.text, "html.parser")
t = soup.find_all('div', {'class': 'div-share'})[1].findChild()
title = t.contents[0].strip('').replace('"', '').replace('lyrics', '')
s = soup.find_all('div')
lyrics = s[21].text.strip()
s = "\n%s\n\n" % (title)
s += lyrics
return s
def metroScrape(url):
r = requests.get(url)
soup = bs(r.text, "html.parser")
s = soup.find('div', id="lyrics-body-text")
title = soup.find('h1', style="font-size:2.3em;").text.strip().replace('Lyrics', '')
lyrics = s.text
# print "Lyrics fetched from metrolyrics."
s = "\n%s\n\n" % (title)
s += lyrics
return s
def lyricsmintScrape(url):
r = requests.get(url)
soup = bs(r.text, "html.parser")
s = soup.find('div', id="lyric")
s = unicode(s)
# print type(s)
items = ['<br/>', '<p>', '</p>', '<div id="lyric"><h2>', 'Lyrics</h2>',
'<i><b>', '</b></i>', '</div>']
replacements = ['\n', '\n', '\n', '', '\n', 'By-', '', '']
for i, item in enumerate(items):
try:
s = s.replace(item, replacements[i])
except Exception as e:
print "Error- ", e
# title = soup.find('h2').text.strip().replace('Lyrics', '')
return s
def allthelyricsScrape(url):
r = requests.get(url)
soup = bs(r.text, "html.parser")
s = soup.find('div', class_="content-text-inner")
title = soup.find('h1', class_="page-title").text
lyrics = s.text.strip()
return title + '\n' + lyrics
def glamshamScrape(url):
r = requests.get(url)
soup = bs(r.text, "html.parser")
s = soup.find('div', class_="col-sm-6")
s = unicode(s)
title = soup.find('font', class_="general").text
title = title[7:]
items = ['<br>', '<div class="col-sm-6">', '<font class="general">', '</br>', '/div', '/font', '<>']
replacements = ['\n', '', '', '', '', '', '']
for i, item in enumerate(items):
try:
s = s.replace(item, replacements[i])
except Exception as e:
print "Error- ", e
lyrics = s.strip()
return title + '\n' + lyrics
if __name__ == '__main__':
q = Queue()
getLyrics("Into you", q)
print q.get()