songs2slides

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

commit 8873af858c9bb79fb21abdda058e5cd0047216de
parent b9b5f24771818f6f988a3609288ee3670904dce7
Author: AsherMorgan <59518073+AsherMorgan@users.noreply.github.com>
Date:   Tue, 11 Aug 2020 15:18:03 -0700

Add support for custom songs.

Diffstat:
MSongs2Slides/config.py | 16++++++++++++++++
MSongs2Slides/core.py | 36++++++++++++++++++++++--------------
2 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/Songs2Slides/config.py b/Songs2Slides/config.py @@ -1,3 +1,4 @@ +# Contains default parsing and PowerPoint settings defaultSettings = { # Parsing settings "title-slides": True, @@ -28,3 +29,18 @@ defaultSettings = { "line-spacing": 1.25, "word-wrap": True } + + + +# Contains cached and custom song information +cachedSongs = { + # Keys should be lowercase ASCII strings without whitespace or special characters + "testartist-testsong": { + # Title and Artist of the song (formated however you want) + "title":"Test Song", + "artist":"Test Artist", + + # Lyrics with two newlines between each stanza and no newlines at the beginning or end + "lyrics":"test1\ntest2\n\ntest3\ntest4" + } +} diff --git a/Songs2Slides/core.py b/Songs2Slides/core.py @@ -6,6 +6,7 @@ from pptx.enum.text import MSO_ANCHOR, PP_ALIGN from pptx.util import Inches, Pt import re import requests +from Songs2Slides import config from unidecode import unidecode @@ -31,14 +32,27 @@ def GetLyrics(title, artist): artist = "-".join(list(filter(lambda a: a != "", artist.split("-")))) title = "-".join(list(filter(lambda a: a != "", title.split("-")))) - # Get page - page = requests.get("https://genius.com/{0}-{1}-lyrics".format(artist, title)) - soup = BeautifulSoup(page.text, "html.parser") - - # Find lyrics and song information - lyrics = soup.find("div", class_="lyrics").get_text() - title = soup.find("h1", class_="header_with_cover_art-primary_info-title").get_text() - artist = soup.find("a", class_="header_with_cover_art-primary_info-primary_artist").get_text() + # Get song info + if (f"{artist}-{title}" in config.cachedSongs): + # Get the cache key + key = f"{artist}-{title}" + + # Get info from cache + lyrics = config.cachedSongs[key]["lyrics"] + title = config.cachedSongs[key]["title"] + artist = config.cachedSongs[key]["artist"] + else: + # Get page from the internet + page = requests.get(f"https://genius.com/{artist}-{title}-lyrics") + soup = BeautifulSoup(page.text, "html.parser") + + # Find song info + lyrics = soup.find("div", class_="lyrics").get_text() + title = soup.find("h1", class_="header_with_cover_art-primary_info-title").get_text() + artist = soup.find("a", class_="header_with_cover_art-primary_info-primary_artist").get_text() + + # Remove starting and ending newlines + lyrics = lyrics[2:-2] # Return lyrics return lyrics, title, artist @@ -60,12 +74,6 @@ def ParseLyrics(title, artist, settings): # Parse Lyrics rawLines = rawLyrics.split("\n") - # Remove starting and ending newlines - del rawLines[0] - del rawLines[0] - del rawLines[-1] - del rawLines[-1] - # Add title slide slides = [] if (settings["title-slides"]):