Skip to content

Commit

Permalink
Merge pull request #70 from johnwmillr/use-argparse-for-command-line
Browse files Browse the repository at this point in the history
Use argparse for command line inputs
  • Loading branch information
johnwmillr authored Dec 1, 2018
2 parents 9e6e6fa + a1114f8 commit 89acbd0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ pip install git+https://github.com/johnwmillr/LyricsGenius.git
Import the package and search for songs by a given artist:

```python
lyricsgenius as genius
api = genius.Genius('my_client_access_token_here')
artist = api.search_artist('Andy Shauf', max_songs=3)
import lyricsgenius as genius
api = genius.Genius("my_client_access_token_here")
artist = api.search_artist("Andy Shauf", max_songs=3)
```

Search for a single song by the same artist:

```python
song = api.search_song('To You', artist.name)
song = api.search_song("To You", artist.name)
```

Add the song to the artist object:
Expand All @@ -54,8 +54,19 @@ You can also call the package from the command line:

```bash
export GENIUS_CLIENT_ACCESS_TOKEN="my_client_access_token_here"
python3 -m lyricsgenius --search-song 'Begin Again' 'Andy Shauf'
python3 -m lyricsgenius --search-artist 'Lupe Fiasco' 3
python3 -m lyricsgenius --help
```

Search for and save lyrics to a given song:

```bash
python3 -m lyricsgenius song "Begin Again" "Andy Shauf" --save
```

Search for five songs by 'The Beatles' and save the lyrics:

```bash
python3 -m lyricsgenius artist "The Beatles" --max-songs 5 --save
```

## Example projects
Expand Down
2 changes: 1 addition & 1 deletion lyricsgenius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__url__ = 'https://github.com/johnwmillr/LyricsGenius'
__description__ = 'A Python wrapper around the Genius API'
__license__ = 'MIT'
__version__ = '0.9.8'
__version__ = '0.9.9'

import sys
assert sys.version_info[0] == 3, "LyricsGenius requires Python 3."
Expand Down
76 changes: 48 additions & 28 deletions lyricsgenius/__main__.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
# Command line usage:
# $python3 -m lyricsgenius --search-song 'Begin Again' 'Andy Shauf'
# $python3 -m lyricsgenius --search-artist 'Lupe Fiasco' 3
# python3 -m lyricsgenius --help
# python3 -m lyricsgenius song "Begin Again" "Andy Shauf" --save
# python3 -m lyricsgenius artist "The Beatles" --max-songs 5 --save

import sys
import os
import lyricsgenius as genius
import argparse
import lyricsgenius


def main(args=None):
if args is None:
args = sys.argv[1:]

msg = "Download song lyrics from Genius.com"
parser = argparse.ArgumentParser(description=msg)
parser.add_argument("search_type", type=str.lower, choices=["song", "artist"],
help="Specify whether search is for 'song' or 'artist'")
parser.add_argument("terms", type=str, nargs="+",
help="Provide terms for search")
parser.add_argument("--save", action="store_true",
help="If specified, saves songs to JSON file")
parser.add_argument("--max-songs", type=int,
help="Specify number of songs when searching for artist")
parser.add_argument("-q", "--quiet", action="store_true",
help="Turn off the API verbosity")
args = parser.parse_args()

# Create an instance of the Genius class
client_access_token = os.environ.get("GENIUS_CLIENT_ACCESS_TOKEN", None)
assert client_access_token is not None, "Must declare environment variable: GENIUS_CLIENT_ACCESS_TOKEN"
api = genius.Genius(client_access_token)

# There must be a standard way to handle "--" inputs on the command line
if sys.argv[1] == '--search-song':
if len(sys.argv) == 4:
song = api.search_song(sys.argv[2],sys.argv[3])
elif len(sys.argv) == 3:
song = api.search_song(sys.argv[2])
print('"{title}" by {artist}:\n {lyrics}'.format(title=song.title,artist=song.artist,lyrics=song.lyrics.replace('\n','\n ')))
elif sys.argv[1] == '--search-artist':
if len(sys.argv) == 4:
max_songs = int(sys.argv[3])
else:
max_songs = 5
artist = api.search_artist(sys.argv[2], max_songs=max_songs)
print("Saving {} lyrics...".format(artist.name))
api.save_artist_lyrics(artist)
msg = "Must declare environment variable: GENIUS_CLIENT_ACCESS_TOKEN"
assert client_access_token, msg
api = lyricsgenius.Genius(client_access_token)
if args.quiet:
api.verbose = False

# Handle the command-line inputs
if args.search_type == "song":
song = api.search_song(*args.terms)
if not song:
if not args.quiet:
print("Could not find specified song. Check spelling?")
return
print('\n"{s}" by {a}:\n\n{lyrics}\n'.format(s=song.title,
a=song.artist,
lyrics=song.lyrics))
if args.save:
if not args.quiet:
print("Saving lyrics to '{s}'...".format(s=song.title))
song.save_lyrics()
else:
print("Usage: python -m lyricsgenius [--search-song song_name] [--search-artist artist_name num_songs]")
return
artist = api.search_artist(args.terms[0], max_songs=args.max_songs)
if args.save:
if not args.quiet:
print("Saving '{a}'' lyrics...".format(a=artist.name))
api.save_artists(artist)


if __name__ == "__main__":
main()
main()
2 changes: 1 addition & 1 deletion lyricsgenius/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def search_song(self, title, artist="",
return song

if self.verbose:
print("Could not find specified song.")
print("Could not find specified song. Check spelling?")
return None

def search_artist(self, artist_name, max_songs=None, sort='title',
Expand Down

0 comments on commit 89acbd0

Please sign in to comment.