kmaurinjones commited on
Commit
610e92c
1 Parent(s): dd7b091

Upload azapi.py

Browse files
Files changed (1) hide show
  1. azapi.py +178 -0
azapi.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .requester import Requester
2
+ from .tools import *
3
+
4
+ class AZlyrics(Requester):
5
+ """
6
+ Fast and Secure API for AZLyrics.com.
7
+
8
+ Attributes:
9
+ title (str): song title.
10
+ artist (str): artist name.
11
+ search_engine (str): search engine used to assist scraping lyrics.
12
+ - currently available: 'google', 'duckduckgo'.
13
+ accuracy (float): used to determine accuracy via jaro algorithm.
14
+ proxies (dict): if you want to use proxy while connecting to 'AZLyrics.com'.
15
+ """
16
+
17
+ def __init__(self, search_engine='', accuracy=0.6, proxies={}):
18
+ self.title = ''
19
+ self.artist = ''
20
+ self.search_engine = search_engine
21
+
22
+ self.accuracy = accuracy
23
+ if not 0 < accuracy <= 1:
24
+ self.accuracy = 0.6
25
+
26
+ self.proxies = proxies
27
+
28
+ self.lyrics_history = []
29
+ self.lyrics = ''
30
+ self.songs = {}
31
+
32
+ def getLyrics(self, url=None, ext='txt', save=False, path='', sleep=3):
33
+ """
34
+ Retrieve Lyrics for a given song details.
35
+
36
+ Parameters:
37
+ url (str): url of the song's Azlyrics page.
38
+ ext (str): extension of the lyrics saved file, default is ".txt".
39
+ save (bool): allow to or not to save lyrics in a file.
40
+ sleep (float): cooldown before next request.
41
+
42
+ Returns:
43
+ lyrics (str): Lyrics of the detected song.
44
+ """
45
+
46
+ # Best cooldown is 5 sec
47
+ time.sleep(sleep)
48
+
49
+ link = url
50
+
51
+ if not url:
52
+ # v3.0.5: No need for artist and title if url is found
53
+ if not self.artist + self.title:
54
+ raise ValueError("Both artist and title can't be empty!")
55
+ if self.search_engine:
56
+ # If user can't remember the artist,
57
+ # he can search by title only
58
+
59
+ # Get AZlyrics url via Google Search
60
+ link = googleGet(
61
+ self.search_engine,
62
+ self.accuracy,
63
+ self.get,
64
+ self.artist,
65
+ self.title,
66
+ 0,
67
+ self.proxies
68
+ )
69
+ if not link:
70
+ return 0
71
+ else:
72
+ # Sometimes search engines block you
73
+ # If happened use the normal get method
74
+ link = normalGet(
75
+ self.artist,
76
+ self.title,
77
+ 0)
78
+
79
+ page = self.get(link, self.proxies)
80
+ if page.status_code != 200:
81
+ if not self.search_engine:
82
+ print('Failed to find lyrics. Trying to get link from Google')
83
+ self.search_engine = 'google'
84
+ lyrics = self.getLyrics(url=url, ext=ext, save=save, path=path, sleep=sleep)
85
+ self.search_engine = ''
86
+ return lyrics
87
+ else:
88
+ print('Error',page.status_code)
89
+ return 1
90
+
91
+ # Getting Basic metadata from azlyrics
92
+ metadata = [elm.text for elm in htmlFindAll(page)('b')]
93
+
94
+ # if metadata is empty, then it's not a valid page
95
+ if not metadata:
96
+ print('Error', 'no metadata')
97
+ return 1
98
+
99
+ # v3.0.4: Update title and artist attributes with exact names
100
+ self.artist = filtr(metadata[0][:-7], True)
101
+ self.title = filtr(metadata[1][1:-1], True)
102
+
103
+ lyrics = parseLyric(page)
104
+ self.lyrics = lyrics.strip()
105
+
106
+ # Saving Lyrics
107
+ if lyrics:
108
+ if save:
109
+ # v3.0.2: Adding custom path
110
+ p = os.path.join(
111
+ path,
112
+ '{} - {}.{}'.format(
113
+ self.title.title(),
114
+ self.artist.title(),
115
+ ext
116
+ )
117
+ )
118
+
119
+ with open(p, 'w', encoding='utf-8') as f:
120
+ f.write(lyrics.strip())
121
+
122
+ # Store lyrics for later usage
123
+ self.lyrics_history.append(self.lyrics)
124
+ return self.lyrics
125
+
126
+ self.lyrics = 'No lyrics found :('
127
+ return 2
128
+
129
+ def getSongs(self, sleep=3):
130
+ """
131
+ Retrieve a dictionary of songs with their links.
132
+
133
+ Parameters:
134
+ sleep (float): cooldown before next request.
135
+
136
+ Returns:
137
+ dict: dictionary of songs with their links.
138
+ """
139
+
140
+ if not self.artist:
141
+ raise Exception("Artist can't be empty!")
142
+
143
+ # Best cooldown is 5 sec
144
+ time.sleep(sleep)
145
+
146
+ if self.search_engine:
147
+ link = googleGet(
148
+ self.search_engine,
149
+ self.accuracy,
150
+ self.get,
151
+ self.artist,
152
+ '',
153
+ 1,
154
+ self.proxies
155
+ )
156
+ if not link:
157
+ return {}
158
+ else:
159
+ link = normalGet(
160
+ self.artist,
161
+ '',
162
+ 1)
163
+
164
+ albums_page = self.get(link, self.proxies)
165
+ if albums_page.status_code != 200:
166
+ if not self.search_engine:
167
+ print('Failed to find songs. Trying to get link from Google')
168
+ self.search_engine = 'google'
169
+ songs = self.getLyrics(sleep=sleep)
170
+ self.search_engine = ''
171
+ return songs
172
+ else:
173
+ print('Error',albums_page.status_code)
174
+ return {}
175
+
176
+ # Store songs for later usage
177
+ self.songs = parseSongs(albums_page)
178
+ return self.songs