genius.py
                        
                             · 1.5 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            #!/usr/bin/env python3
# reqs: requests, beautifulsoup4
from bs4 import BeautifulSoup as Soup
from requests import get
from argparse import ArgumentParser
import sys
CLIENT_ACCESS_TOKEN = "" # generate at http://genius.com/api-clients
def main():
    parser = ArgumentParser(prog="genius")
    parser.add_argument("term", help="Search term")
    parser.add_argument("-n", help="Select different result number (defaults to 1)", type=int, default=1)
    args = parser.parse_args()
    data = get("https://api.genius.com/search", headers={"Authorization": "Bearer " + CLIENT_ACCESS_TOKEN}, params={"q": args.term}).json()
    if not data["response"]["hits"]:
        print("No results", file=sys.stderr)
        return 1
    if len(data["response"]["hits"]) < args.n:
        print("Not enough results ({} returned)".format(len(data["response"]["hits"])), file=sys.stderr)
        return 2
    result = data["response"]["hits"][args.n-1]["result"]
    print("{0}{1} result: {title} by {primary_artist[name]}".format(args.n,
                                                                    "st" if args.n == 1 else
                                                                    "nd" if args.n == 2 else
                                                                    "rd" if args.n == 3 else "th",
                                                                    **result), file=sys.stderr)
    soup = Soup(get("http://genius.com" + result["path"], "http.parser").text)
    print(soup.find('lyrics').text)
    return 0
if __name__ == "__main__":
    sys.exit(main())
                | 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()) |