Converted lyrics generator to a class

This commit is contained in:
Logan Cusano
2022-01-22 23:27:39 -05:00
parent 410924c5cc
commit 73176b35b4

View File

@@ -3,48 +3,81 @@ 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(artists=["Notorious B.I.G", "outkast", "nwa"]):
GENIUS_TOKEN = "gMnJyj87FvjyP2W093rQ_mjo5ZwwLw1u2r0AmcVqYcJ8kkjjW6ZbObeGnS726SrH"
session = Genius(GENIUS_TOKEN, retries=2, timeout=20, sleep_time=0.3)
def get_songs(self, artists: dict = None):
session = Genius(self.GENIUS_TOKEN, retries=2, timeout=20, sleep_time=0.3)
lyrics = []
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
# get songs
for artist in artists:
songlist = session.search_artist(artist, max_songs=75, sort='title')
songlist.save_lyrics()
songlist.save_lyrics()
def sanitize_lyrics(input):
notes_re = re.compile('((?:\[[0-9a-zA-Z :()&+-.]+\])(?: \+ \([a-zA-Z -.]+)?(?:\\n)?)')
footer_re = re.compile('((?:EmbedShare)[ ]*(?:URLCopyEmbedCopy))')
multiline_re = re.compile(('(\\n){3,}'))
sanitized_input = notes_re.sub('', input)
sanitized_input = footer_re.sub('', sanitized_input)
sanitized_input = multiline_re.sub('\n\n', sanitized_input)
return sanitized_input
return songlist
else:
print("Please enter an artist")
def get_lyrics_from_json(json_file):
artist_dict = json.load(json_file)
ready_lyrics = []
print(artist_dict.keys())
for song in artist_dict['songs']:
sanitized_lyrics = sanitize_lyrics(song['lyrics'])
print(sanitized_lyrics)
ready_lyrics.append(sanitized_lyrics)
return ready_lyrics
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 save_sanitized_lyrics():
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(get_lyrics_from_json(read_file))
print(sanitized_lyrics_list)
with open('./lyrics.txt', 'w+', encoding="utf-8") as lyrics_file:
for lyrics in sanitized_lyrics_list:
print(lyrics)
lyrics_file.write(f"{lyrics}\n")
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)
save_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()