From 9c36db915273c9cb709a4777377210877ef4539b Mon Sep 17 00:00:00 2001 From: Aadi Bajpai Date: Mon, 29 Jul 2019 03:08:48 +0530 Subject: [PATCH] refactor get_lyrics to only return the lyrics or None Minor UX tweaks as well Signed-off-by: Aadi Bajpai --- LICENSE.md | 2 +- README.md | 2 +- swaglyrics/__main__.py | 7 +++--- swaglyrics/cli.py | 52 ++++++++++++++++++++++++++---------------- tests/test_cli.py | 20 +++++----------- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index e1c3eab7..f6d5c073 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,7 +1,7 @@ The MIT License (MIT) -Copyright (c) 2018 +Copyright (c) 2018 Aadi Bajpai Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index bd62f2a8..211b53ee 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ distribution and usage easier. ## Why SwagLyrics? SwagLyrics is THE fastest and the most accurate package for getting lyrics.1 -Provided optimal internet, SwagLyrics can fetch lyrics for a track in as less as 0.38s.2 +Provided optimal internet, SwagLyrics can fetch lyrics for a track in as less as 0.28s.2 It also does not require the user to generate any sort of API token (Spotify or Genius) and serves functionality right off the bat. This is possible as the song identification is done using our in-house library diff --git a/swaglyrics/__main__.py b/swaglyrics/__main__.py index da1dfcf8..7e6b671c 100644 --- a/swaglyrics/__main__.py +++ b/swaglyrics/__main__.py @@ -1,8 +1,6 @@ import argparse import os import sys -import webbrowser -import threading import requests import time from SwSpotify import spotify @@ -16,7 +14,7 @@ def unsupported_precheck(): v = requests.get('https://aadibajpai.pythonanywhere.com/version') ver = v.text if ver > version: - print("Update SwagLyrics to the latest version {ver}\n".format(ver=ver)) + print("New version of SwagLyrics available: v{ver}\nPlease update :)".format(ver=ver)) except requests.exceptions.RequestException: pass print('Updating unsupported.txt from server.') @@ -54,6 +52,8 @@ def main(): unsupported_precheck() if args.tab: + import webbrowser + import threading print('Firing up a browser tab!') app.template_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') app.static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') @@ -82,6 +82,7 @@ def main(): print(lyrics(song, artist, make_issue)) print('\n(Press Ctrl+C to quit)') except KeyboardInterrupt: + print('\nSure boss, exiting.') exit() if os.environ.get("TESTING", "False") != "False": break diff --git a/swaglyrics/cli.py b/swaglyrics/cli.py index 40b348ac..196bd1d3 100644 --- a/swaglyrics/cli.py +++ b/swaglyrics/cli.py @@ -67,22 +67,18 @@ def get_lyrics(song, artist, make_issue=True): """ url_data = stripper(song, artist) # generate url path using stripper() url = 'https://genius.com/{}-lyrics'.format(url_data) # format the url with the url path - page = requests.get(url) - if page.status_code != 200: - url_data = requests.get('https://aadibajpai.pythonanywhere.com/stripper', - data={'song': song, 'artist': artist}).text - url = 'https://genius.com/{}-lyrics'.format(url_data) - page = requests.get(url) - html = BeautifulSoup(page.text, "html.parser") - # TODO: Add error handling - lyrics_path = html.find("div", class_="lyrics") # finding div on Genius containing the lyrics - if lyrics_path is None: - with open(unsupported_txt, 'a') as f: - f.write('{song} by {artist} \n'.format(song=song, artist=artist)) - f.close() - lyrics = 'Couldn\'t get lyrics for {song} by {artist}.\n'.format(song=song, artist=artist) - try: + try: + page = requests.get(url).raise_for_status() + except requests.exceptions.HTTPError: + url_data = requests.get('https://aadibajpai.pythonanywhere.com/stripper', data={ + 'song': song, + 'artist': artist}).text + if not url_data: + lyrics = None # Log song and artist for which lyrics couldn't be obtained + with open(unsupported_txt, 'a') as f: + f.write('{song} by {artist} \n'.format(song=song, artist=artist)) + f.close() if make_issue: r = requests.post('https://aadibajpai.pythonanywhere.com/unsupported', data={ 'song': song, @@ -91,10 +87,12 @@ def get_lyrics(song, artist, make_issue=True): }) if r.status_code == 200: lyrics += r.text - except requests.exceptions.RequestException: - pass - else: - lyrics = UnicodeDammit(lyrics_path.get_text().strip()).unicode_markup + url = 'https://genius.com/{}-lyrics'.format(url_data) + page = requests.get(url) + + html = BeautifulSoup(page.text, "html.parser") + lyrics_path = html.find("div", class_="lyrics") # finding div on Genius containing the lyrics + lyrics = UnicodeDammit(lyrics_path.get_text().strip()).unicode_markup return lyrics @@ -114,8 +112,22 @@ def lyrics(song, artist, make_issue=True): except FileNotFoundError: pass init(autoreset=True) - print(Fore.CYAN + '\nGetting lyrics for {song} by {artist}.\n'.format(song=song, artist=artist)) + print(Fore.CYAN + 'Getting lyrics for {song} by {artist}.\n'.format(song=song, artist=artist)) lyrics = get_lyrics(song, artist, make_issue) + if not lyrics: + lyrics = 'Couldn\'t get lyrics for {song} by {artist}.\n'.format(song=song, artist=artist) + # Log song and artist for which lyrics couldn't be obtained + with open(unsupported_txt, 'a') as f: + f.write('{song} by {artist} \n'.format(song=song, artist=artist)) + f.close() + if make_issue: + r = requests.post('https://aadibajpai.pythonanywhere.com/unsupported', data={ + 'song': song, + 'artist': artist, + 'version': __version__ + }) + if r.status_code == 200: + lyrics += r.text return lyrics else: return 'Nothing playing at the moment.' diff --git a/tests/test_cli.py b/tests/test_cli.py index 8ecd240f..c082ca91 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -70,27 +70,19 @@ def test_that_get_lyrics_does_not_break_with_wrong_data(self): Test that get_lyrics function does not break with wrong data """ self.assertEqual(get_lyrics( - "xyzzy", "Yeet", False), "Couldn't get lyrics for xyzzy by Yeet.\n") - self.assertEqual(get_lyrics("wiuegi", "Muhmello", False), "Couldn't get lyrics for wiuegi by Muhmello.\n") - self.assertEqual(get_lyrics("Pixel2XL", "Elgoog", False), "Couldn't get lyrics for Pixel2XL by Elgoog.\n") - - # Deleting above songs and artists from unsupported.txt - with open(unsupported_txt, "r") as f: - lines = f.readlines() - with open(unsupported_txt, "w") as f: - for line in lines: - if line not in ["xyzzy by Yeet \n", "wiuegi by Muhmello \n", "Pixel2XL by Elgoog \n"]: - f.write(line) + "xyzzy", "Yeet", False), None) + self.assertEqual(get_lyrics("wiuegi", "Muhmello", False), None) + self.assertEqual(get_lyrics("Pixel2XL", "Elgoog", False), None) def test_that_lyrics_works_for_unsupported_songs(self): """ Test that lyrics function gives 'unsupported' message to unsupported files """ - get_lyrics("xyzzy", "Yeet", False) + lyrics("xyzzy", "Yeet", False) self.assertEqual(lyrics("xyzzy", "Yeet"), "Lyrics unavailable for xyzzy by Yeet.\n") - get_lyrics("wiuegi", "Muhmello", False) + lyrics("wiuegi", "Muhmello", False) self.assertEqual(lyrics("wiuegi", "Muhmello"), "Lyrics unavailable for wiuegi by Muhmello.\n") - get_lyrics("Pixel2XL", "Elgoog", False) + lyrics("Pixel2XL", "Elgoog", False) self.assertEqual(lyrics("Pixel2XL", "Elgoog"), "Lyrics unavailable for Pixel2XL by Elgoog.\n") # Deleting above songs and artists from unsupported.txt