songs2slides

A tool that automatically finds song lyrics and creates lyric slideshows
git clone https://git.ashermorgan.net/songs2slides/
Log | Files | Refs | README

commit e1accc79e8ae5fd51e1ce3258f3f780eafb3d387
parent b66fb812506500153c966ff1061ee7b248aa6916
Author: AsherMorgan <59518073+AsherMorgan@users.noreply.github.com>
Date:   Fri, 24 Apr 2020 08:01:47 -0700

Make ParseLyrics add title and blank slides.

Diffstat:
MSongs2Slides/models.py | 43+++++++++++++++++++++++++++----------------
MSongs2Slides/routes.py | 7+------
Mcliapp.py | 7+------
3 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/Songs2Slides/models.py b/Songs2Slides/models.py @@ -10,24 +10,24 @@ import requests # Gets the lyrics -def GetLyrics(artist, song): +def GetLyrics(title, artist): # Convert to lowercase artist = artist.lower() - song = song.lower() + title = title.lower() # Replace invalid characters old = [" ", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "'", "?", "/", "|", "\\", ".", ",", "á", "é", "í", "ó", "ñ", "ú"] new = ["-", "", "", "", "s", "", "-", "-and-", "", "", "", "-", "-", "", "", "", "", "", "", "", "a", "e", "i", "o", "n", "u"] for i in range(0, len(old)): artist = artist.replace(old[i], new[i]) - song = song.replace(old[i], new[i]) + title = title.replace(old[i], new[i]) # Remove unnecessary dashes artist = "-".join(list(filter(lambda a: a != "", artist.split("-")))) - song = "-".join(list(filter(lambda a: a != "", song.split("-")))) + title = "-".join(list(filter(lambda a: a != "", title.split("-")))) # Get lyrics - page = requests.get("https://genius.com/{0}-{1}-lyrics".format(artist, song)) + page = requests.get("https://genius.com/{0}-{1}-lyrics".format(artist, title)) lyrics = BeautifulSoup(page.text, "html.parser").find("div", class_="lyrics").get_text() # Return lyrics @@ -35,10 +35,11 @@ def GetLyrics(artist, song): -# Parses the lyrics into blocks -def ParseLyrics(lyrics): - # Split lyrics - rawLines = lyrics.split("\n") +# Parses the lyrics of a song into slides +def ParseLyrics(title, artist): + # Get lyrics + rawLyrics = GetLyrics(title, artist) + rawLines = rawLyrics.split("\n") # Remove starting and ending newlines del rawLines[0] @@ -46,30 +47,40 @@ def ParseLyrics(lyrics): del rawLines[-1] del rawLines[-1] - # Parse lyrics into lines - lines = [] + # Add title slide + slides = [] + if (config.parsing["title-slides"]): + slides += ["{0}\n{1}".format(title, artist)] + + # Parse lyrics into slides slideSize = config.parsing["lines-per-slide"] for i in range(0, len(rawLines)): if (rawLines[i] == ""): # Start a new slide without content - lines.append("") + slides.append("") slideSize = 0 elif (rawLines[i][0] == "["): # Ignore pass elif (slideSize == config.parsing["lines-per-slide"]): # Start a new slide with content - lines.append(rawLines[i]) + slides.append(rawLines[i]) slideSize = 1 elif (slideSize == 0): # Continue a blank slide - lines[-1] = lines[-1] + rawLines[i] + slides[-1] = slides[-1] + rawLines[i] slideSize += 1 else: # Continue a slide - lines[-1] = lines[-1] + "\n" + rawLines[i] + slides[-1] = slides[-1] + "\n" + rawLines[i] slideSize += 1 - return lines + + # Add blank slide + if (slides[-1] != ""): + slides += [""] + + # Return parsed lyrics + return slides diff --git a/Songs2Slides/routes.py b/Songs2Slides/routes.py @@ -25,12 +25,7 @@ def pptx(): lyrics = [] for song in data: try: - parsedLyrics = models.ParseLyrics(models.GetLyrics(song[1], song[0])) - if (config.parsing["title-slides"]): - lyrics += ["{0}\n{1}".format(song[0], song[1])] - lyrics += parsedLyrics - if (lyrics[-1] != ""): - lyrics += [""] + lyrics += models.ParseLyrics(song[0], song[1]) except: pass diff --git a/cliapp.py b/cliapp.py @@ -24,12 +24,7 @@ if (__name__ == "__main__"): # Get song lyrics try: - parsedLyrics = models.ParseLyrics(models.GetLyrics(artist, title)) - if (config.parsing["title-slides"]): - lyrics += ["{0}\n{1}".format(title, artist)] - lyrics += parsedLyrics - if (lyrics[-1] != ""): - lyrics += [""] + lyrics += models.ParseLyrics(title, artist) except: print("The song could not be found. Make sure that you spelled it correctly.") song -= 1