-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrics.py
48 lines (37 loc) · 1.28 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
import googlesearch as Google
import requests
from bs4 import BeautifulSoup
import os
SITE = 'tekstovi.net'
WRITE = 'w+'
LYRICS_TXT = 'lyrics.txt'
class Lyrics:
def __init__(self) -> None:
self.links = None
def __search(self, str, numRes=3):
search = f'site:{SITE} {str}'
return Google.search(search, numRes)
def get_lyrics(self, str):
self.links = self.__search(str)
retval = []
for link in self.links:
try:
page = requests.get(link)
soup = BeautifulSoup(page.content, 'html.parser')
author = soup.find('h1', class_='lyricCapt').text
songName = soup.find('h2', class_='lyricCapt').text
text = ' '.join(
[word.text for word in soup.find_all('p', class_='lyric')])
except:
pass
else:
retval.append(dict(author=author, songName=songName, text=text))
return retval
def save(self, path, lyrics):
path = os.path.join(path, LYRICS_TXT)
file = open(path, WRITE)
lyrics_str = f'{lyrics["author"]}\n'
lyrics_str += f'{lyrics["songName"]}\n\n'
lyrics_str += lyrics["text"]
file.writelines(lyrics_str)
file.close()