Steven Smith revised this gist . Go to revision
1 file changed, 6 insertions, 3 deletions
genius.py
| @@ -5,7 +5,7 @@ from requests import get | |||
| 5 | 5 | from argparse import ArgumentParser | |
| 6 | 6 | import sys | |
| 7 | 7 | ||
| 8 | - | CLIENT_ACCESS_TOKEN = "" # generate at http://genius.com/api-clients | |
| 8 | + | CLIENT_ACCESS_TOKEN = "" # generate at https://genius.com/api-clients | |
| 9 | 9 | ||
| 10 | 10 | def main(): | |
| 11 | 11 | parser = ArgumentParser(prog="genius") | |
| @@ -25,9 +25,12 @@ def main(): | |||
| 25 | 25 | "nd" if args.n == 2 else | |
| 26 | 26 | "rd" if args.n == 3 else "th", | |
| 27 | 27 | **result), file=sys.stderr) | |
| 28 | - | soup = Soup(get("http://genius.com" + result["path"], "http.parser").text) | |
| 28 | + | soup = Soup(get("http://genius.com" + result["path"], "html.parser").text) | |
| 29 | 29 | print(soup.find('lyrics').text) | |
| 30 | 30 | return 0 | |
| 31 | 31 | ||
| 32 | 32 | if __name__ == "__main__": | |
| 33 | - | sys.exit(main()) | |
| 33 | + | try: | |
| 34 | + | sys.exit(main()) | |
| 35 | + | except KeyboardInterrupt: | |
| 36 | + | print("Aborting", file=sys.stderr) | |
Steven Smith revised this gist . Go to revision
1 file changed, 33 insertions
genius.py(file created)
| @@ -0,0 +1,33 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | # reqs: requests, beautifulsoup4 | |
| 3 | + | from bs4 import BeautifulSoup as Soup | |
| 4 | + | from requests import get | |
| 5 | + | from argparse import ArgumentParser | |
| 6 | + | import sys | |
| 7 | + | ||
| 8 | + | CLIENT_ACCESS_TOKEN = "" # generate at http://genius.com/api-clients | |
| 9 | + | ||
| 10 | + | def main(): | |
| 11 | + | parser = ArgumentParser(prog="genius") | |
| 12 | + | parser.add_argument("term", help="Search term") | |
| 13 | + | parser.add_argument("-n", help="Select different result number (defaults to 1)", type=int, default=1) | |
| 14 | + | args = parser.parse_args() | |
| 15 | + | data = get("https://api.genius.com/search", headers={"Authorization": "Bearer " + CLIENT_ACCESS_TOKEN}, params={"q": args.term}).json() | |
| 16 | + | if not data["response"]["hits"]: | |
| 17 | + | print("No results", file=sys.stderr) | |
| 18 | + | return 1 | |
| 19 | + | if len(data["response"]["hits"]) < args.n: | |
| 20 | + | print("Not enough results ({} returned)".format(len(data["response"]["hits"])), file=sys.stderr) | |
| 21 | + | return 2 | |
| 22 | + | result = data["response"]["hits"][args.n-1]["result"] | |
| 23 | + | print("{0}{1} result: {title} by {primary_artist[name]}".format(args.n, | |
| 24 | + | "st" if args.n == 1 else | |
| 25 | + | "nd" if args.n == 2 else | |
| 26 | + | "rd" if args.n == 3 else "th", | |
| 27 | + | **result), file=sys.stderr) | |
| 28 | + | soup = Soup(get("http://genius.com" + result["path"], "http.parser").text) | |
| 29 | + | print(soup.find('lyrics').text) | |
| 30 | + | return 0 | |
| 31 | + | ||
| 32 | + | if __name__ == "__main__": | |
| 33 | + | sys.exit(main()) | |