Initial commit - 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
---
(DIR) changeset 02421a80bfa1df745b03c0d0974488ccf5ebb05d
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sun, 12 Aug 2018 13:49:01
Initial commit
Diffstat:
lyrics.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 92 insertions(+), 0 deletions(-)
---
diff -r 000000000000 -r 02421a80bfa1 lyrics.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lyrics.py Sun Aug 12 13:49:01 2018 +0200
@@ -0,0 +1,92 @@
+#!/usr/pkg/bin/python3.6
+
+#
+# Copyright (c) 2018 Leonardo Taccari
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+
+"""
+fetch and print lyrics
+
+lyrics is a Python script/module to print lyrics of songs given the artist
+and title of the song
+"""
+
+
+from urllib import request
+from bs4 import BeautifulSoup, SoupStrainer
+
+
+LYRICS_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0'
+
+
+class LyricsFetcher:
+ def __init__(self, artist, title):
+ self.artist = artist
+ self.title = title
+
+ def _url(self):
+ pass
+
+ def lyrics(self):
+ pass
+
+
+class Genius(LyricsFetcher):
+ def _url(self):
+ artist = self.artist.replace("'", "").replace(' ', '-')
+ title = self.title.replace("'", "").replace(' ', '-')
+ return 'https://genius.com/{}-{}-lyrics'.format(artist, title)
+
+ def lyrics(self):
+ try:
+ req = request.Request(self._url())
+ req.add_header('User-Agent', LYRICS_USER_AGENT)
+ with request.urlopen(req) as r:
+ t = BeautifulSoup(r, 'html.parser',
+ parse_only=SoupStrainer(class_='lyrics'))
+ return t.text.strip()
+ except Exception:
+ raise Exception("Could not fetch lyric")
+
+
+if __name__ == '__main__':
+ import sys
+
+ def usage():
+ print('usage: {} artist title'.format(sys.argv[0]))
+ exit(1)
+
+ if len(sys.argv) != 3:
+ usage()
+
+ artist, title = sys.argv[1:]
+
+ try:
+ lf = Genius(artist, title)
+ print(lf.lyrics())
+ except Exception:
+ exit(1)