#!/usr/bin/env python3
# get lyrics from genius.com
import requests
from bs4 import BeautifulSoup
import sys
import re

user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'

def get_bs(song_name, artist_name):
    CHARS_TO_REMOVE = ".'?!,"
    song_name = song_name.replace(' - Remastered', '').replace('/', '-')
    artist_name = artist_name.replace('/', '-')
    for char_to_remove in CHARS_TO_REMOVE:
        artist_name = artist_name.replace(char_to_remove, "")
        song_name = song_name.replace(char_to_remove, "")
    url = "https://genius.com/{}-{}-lyrics".format(
            artist_name.replace('& ', 'and ').replace(' ', '-').replace(':', '-').capitalize(),
            song_name.replace('& ', 'and ').replace(' ', '-'))
    bs = BeautifulSoup(requests.get(url, headers={'user-agent': user_agent}
        ).content.decode(), 'html.parser')
    return bs

if __name__ == '__main__':
    try:
        artist_name = sys.argv[2]
        song_name   = sys.argv[1]
        bs = get_bs(song_name, artist_name)
        print(bs.find_all("div", {"class": re.compile(".*[l,L]yrics.*")})[1].get_text(separator="\n"))
        # lyrics = bs.find_all("div", {"class": "lyrics"})[0].text.strip()
        # print(lyrics)
    except Exception as e:
        print(e, file=sys.stderr)
        exit(1)
