forked from dilrajsingh1997/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyrics.py
More file actions
44 lines (35 loc) · 1.07 KB
/
lyrics.py
File metadata and controls
44 lines (35 loc) · 1.07 KB
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
from bs4 import BeautifulSoup
import requests
SEARCH_URL = 'https://www.googleapis.com/customsearch/v1?key=XXXXXXXXXXX&cx=005425014426727164707:7cg0pjnvla0&q='
def get_lyrics(url):
if 'http' not in url:
url='http://' + url
r=requests.get(url)
soup=BeautifulSoup(r.content)
soup=str(soup).replace("<br/> ", "\n")
soup=BeautifulSoup(soup)
details=[]
for detail in soup.findAll('p'):
details.append(detail.get_text())
'''
metadata={}
for data in details[3].split('\n'):
info=data.split(':')
metadata[info[0]]=info[1].strip()
data.append(metadata)
'''
lyrics = '\n\n'.join(para for para in details)
return lyrics
def search_song(query):
r = requests.get(SEARCH_URL + query.replace(' ','+'))
items=r.json()['items']
result=[]
for item in items[:3]:
data={}
data['title'] = item['title']
data['url'] = item['formattedUrl']
result.append(data)
return result
while 1:
url = search_song(raw_input())[0]['url']
print get_lyrics(url)