From 510ec2b117b99da23c566e85206af6f2bbe20c9c Mon Sep 17 00:00:00 2001 From: Andrew Barlow Date: Sat, 15 Aug 2020 23:24:52 -0400 Subject: [PATCH 1/2] fixed reference to song instead of song.name, used JMaynor's updates --- PyLyrics/functions.py | 73 +++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/PyLyrics/functions.py b/PyLyrics/functions.py index e4c7e19..2723971 100644 --- a/PyLyrics/functions.py +++ b/PyLyrics/functions.py @@ -3,35 +3,41 @@ import sys, codecs, json class Track(object): - def __init__(self,trackName,album,artist): + def __init__(self, trackName, album, artist): self.name = trackName self.album = album self.artist = artist def __repr__(self): return self.name + def __str__(self): + return self.name def link(self): - return 'http://lyrics.wikia.com/{0}:{1}'.format(self.artist.replace(' ', '-'),self.name.replace(' ','-')) + return 'http://lyrics.wikia.com/{0}:{1}'.format(self.artist.replace(' ', '-'), self.name.replace(' ', '-')) def getLyrics(self): - return PyLyrics.getLyrics(self.artist,self.name) + return PyLyrics.getLyrics(self.artist, self.name) class Artist(object): def __init__(self, name): - self.name = name + self.name = name def getAlbums(self): return PyLyrics.getAlbums(self.name) def __repr__(self): return self.name.encode('utf-8') + def __str__(self): + return self.name class Album(object): - def __init__(self, name, link,singer): + def __init__(self, name, link, singer): self.year = name.split(' ')[-1] - self.name = name.replace(self.year,' ').rstrip() - self.url = link + self.name = name.replace(self.year, ' ').rstrip() + self.url = link self.singer = singer def link(self): - return self.url + return self.url def __repr__(self): if sys.version_info[0] == 2: - return self.name.encode('utf-8','replace') + return self.name.encode('utf-8', 'replace') return self.name + def __str__(self): + return self.name def artist(self): return self.singer def tracks(self): @@ -41,59 +47,58 @@ class PyLyrics: @staticmethod def getAlbums(singer): singer = singer.replace(' ', '_') - s = BeautifulSoup(requests.get('http://lyrics.wikia.com/{0}'.format(singer)).text) - spans = s.findAll('span',{'class':'mw-headline'}) - + s = BeautifulSoup(requests.get('http://lyrics.wikia.com/{0}'.format(singer)).text, 'html.parser') + spans = s.findAll('span', {'class':'mw-headline'}) + als = [] - + for tag in spans: try: a = tag.findAll('a')[0] - als.append(Album(a.text,'http://lyrics.wikia.com' + a['href'],singer)) + als.append(Album(a.text, 'http://lyrics.wikia.com' + a['href'], singer)) except: pass - + if als == []: raise ValueError("Unknown Artist Name given") - return None return als - @staticmethod + @staticmethod def getTracks(album): url = "http://lyrics.wikia.com/api.php?action=lyrics&artist={0}&fmt=xml".format(album.artist()) - soup = BeautifulSoup(requests.get(url).text) - + soup = BeautifulSoup(requests.get(url).text, 'html.parser') + currentAlbum = None for al in soup.find_all('album'): if al.text.lower().strip() == album.name.strip().lower(): currentAlbum = al break - songs =[Track(song.text,album,album.artist()) for song in currentAlbum.findNext('songs').findAll('item')] - return songs + if currentAlbum is not None: + songs = [Track(song.text, album, album.artist()) for song in currentAlbum.findNext('songs').findAll('item')] + return songs @staticmethod def getLyrics(singer, song): #Replace spaces with _ singer = singer.replace(' ', '_') - song = song.replace(' ', '_') - r = requests.get('http://lyrics.wikia.com/{0}:{1}'.format(singer,song)) - s = BeautifulSoup(r.text) + song = song.name.replace(' ', '_') + r = requests.get('http://lyrics.wikia.com/{0}:{1}'.format(singer, song)) + s = BeautifulSoup(r.text, 'html.parser') #Get main lyrics holder - lyrics = s.find("div",{'class':'lyricbox'}) + lyrics = s.find("div", {'class':'lyricbox'}) if lyrics is None: raise ValueError("Song or Singer does not exist or the API does not have Lyrics") - return None #Remove Scripts [s.extract() for s in lyrics('script')] #Remove Comments - comments = lyrics.findAll(text=lambda text:isinstance(text, Comment)) + comments = lyrics.findAll(text=lambda text: isinstance(text, Comment)) [comment.extract() for comment in comments] #Remove unecessary tags - for tag in ['div','i','b','a']: + for tag in ['div', 'i', 'b', 'a']: for match in lyrics.findAll(tag): match.replaceWithChildren() #Get output as a string and remove non unicode characters and replace
with newlines - output = str(lyrics).encode('utf-8', errors='replace')[22:-6:].decode("utf-8").replace('\n','').replace('
','\n') + output = str(lyrics).encode('utf-8', errors='replace')[22:-6:].decode("utf-8").replace('\n', '').replace('
', '\n') try: return output except: @@ -101,10 +106,10 @@ def getLyrics(singer, song): def main(): albums = PyLyrics.getAlbums('OneRepublic') - print (albums) + print(albums) tracks = PyLyrics.getTracks(albums[-1]) - print (tracks[7].getLyrics()) - + print(tracks[7].getLyrics()) + -if __name__=='__main__': - main() \ No newline at end of file +if __name__ == '__main__': + main() From f984c6d8a748b4a753411f2d099cb38560d335d0 Mon Sep 17 00:00:00 2001 From: Andrew Barlow Date: Sat, 15 Aug 2020 23:37:56 -0400 Subject: [PATCH 2/2] added check to function w/ vague inputso it can use a str or a Track --- PyLyrics/functions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/PyLyrics/functions.py b/PyLyrics/functions.py index 2723971..d656e01 100644 --- a/PyLyrics/functions.py +++ b/PyLyrics/functions.py @@ -79,7 +79,10 @@ def getTracks(album): def getLyrics(singer, song): #Replace spaces with _ singer = singer.replace(' ', '_') - song = song.name.replace(' ', '_') + if ( isinstance(song, str) ): + song = song.replace(' ', '_') + elif ( isinstance(song, Track) ): + song = song.name.replace(' ', '_') r = requests.get('http://lyrics.wikia.com/{0}:{1}'.format(singer, song)) s = BeautifulSoup(r.text, 'html.parser') #Get main lyrics holder