from lyricsgenius import Genius
import json
import re
import os

class GetLyrics():
    def __init__(self):
        self.GENIUS_TOKEN = "gMnJyj87FvjyP2W093rQ_mjo5ZwwLw1u2r0AmcVqYcJ8kkjjW6ZbObeGnS726SrH"
        self.notes_re = re.compile('((?:\[[0-9a-zA-Z :()&+-.]+\])(?: \+ \([a-zA-Z -.]+)?(?:\\n)?)')
        self.footer_re = re.compile('((?:EmbedShare)[ ]*(?:URLCopyEmbedCopy))')
        self.multiline_re = re.compile(('(\\n){2,}'))

    def get_songs(self, artists: dict = None):
        session = Genius(self.GENIUS_TOKEN, retries=2, timeout=20, sleep_time=0.3)

        if artists is not None:
            # get songs
            for artist in artists:
                try:
                    songlist = session.search_artist(artist, max_songs=75, sort='title')
                except Exception as e:
                    print(e)
                    continue

                songlist.save_lyrics()

                return songlist
        else:
            print("Please enter an artist")

    def sanitize_lyrics(self, input):
        sanitized_input = self.notes_re.sub('', input)
        sanitized_input = self.footer_re.sub('', sanitized_input)
        sanitized_input = sanitized_input.replace("[?]", '')
        sanitized_input = self.multiline_re.sub('\n', sanitized_input)
        return sanitized_input

    def get_lyrics_from_json(self, json_file):
        artist_dict = json.load(json_file)
        ready_lyrics = []
        print(artist_dict.keys())
        for song in artist_dict['songs']:
            sanitized_lyrics = self.sanitize_lyrics(song['lyrics'])
            print(sanitized_lyrics)
            ready_lyrics.append(sanitized_lyrics)
        return ready_lyrics

    def save_sanitized_lyrics(self):
        sanitized_lyrics_list = []
        for file in os.listdir("./"):
            if file.endswith(".json"):
                with open(file, 'r', encoding="utf-8") as read_file:
                    sanitized_lyrics_list.extend(self.get_lyrics_from_json(read_file))
                    print(sanitized_lyrics_list)
        with open('./lyrics.txt', 'a+', encoding="utf-8") as lyrics_file:
            for lyrics in sanitized_lyrics_list:
                print(lyrics)
                lyrics_file.write(f"{lyrics}\n")

    def sanitize_file(self):
        sanitized_lyrics = ''
        with open('./lyrics.txt', 'r', encoding="utf-8") as lyrics_file:
            file_read_lines = lyrics_file.read()
            sanitized_lyrics = self.sanitize_lyrics(file_read_lines)

        del lyrics_file

        with open('./lyrics.txt', 'w', encoding="utf-8") as lyrics_file:
            lyrics_file.write(sanitized_lyrics)


lyrics = GetLyrics()

#lyrics.get_songs([])
              #"noel miller", "tiny meat gang", "young gravy", "parov stelar", "mac miller", "marilyn manson"
              #"ozzy osborn", "eminem", "2 chainz", "elvis presley", "jim croce", "nwa", "nas", "biggie", "willie nelson", "snoop dog",
              #"lil nas x", "lil wayne", "post malone", "michael jackson", "misterwives", "p!nk", "labrinth", "elton john",
              #"the lovin spoonful", "blake shelton", "avenge sevenfold", "wiz khalifa", "we the kings", "leonard cohen",
              #"john denver", "yes", "pink floyd", "paul simon", "u2"

#lyrics.save_sanitized_lyrics()

lyrics.sanitize_file()
