lyrics.py - lyrics - Print lyrics of songs given the artist and title (HTM) hg clone https://bitbucket.org/iamleot/lyrics (DIR) Log (DIR) Files (DIR) Refs (DIR) README --- lyrics.py --- 1 #!/usr/pkg/bin/python3.7 2 3 # 4 # Copyright (c) 2018-2019 Leonardo Taccari 5 # All rights reserved. 6 # 7 # Redistribution and use in source and binary forms, with or without 8 # modification, are permitted provided that the following conditions 9 # are met: 10 # 11 # 1. Redistributions of source code must retain the above copyright 12 # notice, this list of conditions and the following disclaimer. 13 # 2. Redistributions in binary form must reproduce the above copyright 14 # notice, this list of conditions and the following disclaimer in the 15 # documentation and/or other materials provided with the distribution. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 21 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 # POSSIBILITY OF SUCH DAMAGE. 28 # 29 30 31 """ 32 fetch and print lyrics 33 34 lyrics is a Python script/module to print lyrics of songs given the artist 35 and title of the song 36 """ 37 38 39 from pydoc import pager 40 from urllib import request 41 from bs4 import BeautifulSoup, SoupStrainer 42 43 44 LYRICS_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 45 46 47 class LyricsFetcher: 48 def __init__(self, artist, title): 49 self.artist = artist 50 self.title = title 51 52 def _url(self): 53 pass 54 55 def lyrics(self): 56 pass 57 58 59 class Genius(LyricsFetcher): 60 def _url(self): 61 artist = self.artist.replace("'", "").replace(' ', '-').replace('&', 'and') 62 title = self.title.replace("'", "").replace(' ', '-').replace(',', '').replace('(', '').replace(')', '') 63 return 'https://genius.com/{}-{}-lyrics'.format(artist, title) 64 65 def lyrics(self): 66 try: 67 req = request.Request(self._url()) 68 req.add_header('User-Agent', LYRICS_USER_AGENT) 69 with request.urlopen(req) as r: 70 t = BeautifulSoup(r, 'html.parser', 71 parse_only=SoupStrainer(class_='lyrics')) 72 return t.text.strip() 73 except Exception: 74 raise Exception("Could not fetch lyric") 75 76 77 if __name__ == '__main__': 78 import sys 79 80 def usage(): 81 print('usage: {} artist title'.format(sys.argv[0])) 82 exit(1) 83 84 if len(sys.argv) != 3: 85 usage() 86 87 artist, title = sys.argv[1:] 88 89 try: 90 lf = Genius(artist, title) 91 lyric = lf.lyrics() 92 pager(lyric) 93 except Exception: 94 exit(1)