Introduction: "Alexa, Send Me the Lyrics to _____"
Listening to a song that you want to sing along with? You'd normally have to go through the arduous task of typing out the song's name into google, followed by the word "lyrics". You'd also normally make an ample amount of typos, misclick a certain amount of keys, and have to deal with your device's inevitable lag. By the time you've found lyrics, you find that your song is over and all that work was for nothing.
Maybe you're in the middle of a global pandemic and borrowing your friend's phone doesn't seem like the greatest idea, much less touching that Karaoke Machine tucked in the corner. You want to be protected, but knowing the lyrics to the song is almost as essential. You need a convenient, contactless method to search for lyrics.
Perhaps both those scenarios are slight exaggerations (the first one definitely more so), but still, having song lyrics sent right to your email the second you ask for them to be sent would certainly be pretty convenient. That's where this Instructable (along with Alexa, Wayscript and a bit of Python) comes in. Just ask Alexa for the lyrics to your favorite song and in a few seconds they'll be delivered right to your inbox.
As a high school student going into my senior year, I've found this project to be pretty useful and have actually used it a significant amount since having built it (a rarity - considering most of my projects are found untouched months later).
This tutorial is pretty beginner-level, but it is helpful to know some Python before heading in.
Supplies
Materials:
- Amazon Echo/Any Alexa-Based Smart Speaker
- WayScript account (Will get set-up in the following steps)
- RapidAPI account
- Some Python Knowledge (Not necessary, but helpful)
Step 1: Set Up Wayscript and Alexa
What is Wayscript?
Wayscript is a powerful tool that allows you to run programs using various triggers. We'll be using the Alexa trigger in this Instructable, but feel free to experiment/explore the software - there's all sorts of functionality you can add.
To begin, let's make an account with Wayscript:
- Click on the 'Get Started For Free' or 'Sign Up' buttons. I used the 'Register with Github' option as it felt the most convenient.
- Once you've made an account, you'll be lead to Wayscript's development environment. It is helpful to go through the tutorial before we start.
- Once you've completed the tutorial, navigate to your profile. Click on the 'Create a new script' button and you'll be asked to name your project. I've named it 'Song Lyrics', but you can name it anything you want. Just ensure it's easy to say as you'll be using the program name when asking Alexa to send you the lyrics.
- Once you've made your name, click the 'create' button and you'll be lead to a new Wayscript development environment. We'll get back to this soon.
Before we continue, let's connect your Wayscript account to Alexa:
- Head on over to amazon.com and log-in to your account - specifically the one that is connected to your Alexa-enabled speaker.
- Go to this link: https://www.amazon.com/WayScript-Inc/dp/B07QXXG32... and click 'enable'. If the link does not work for whatever reason, search for the Wayscript Alexa Skill - again, click 'enable'
- Once you've clicked enable, you have to link it to your Wayscript account. Click 'Link Account' and you should be led to wayscript's website. Once you sign in, a screen saying 'You're account has been successfully linked' should pop up.
Once you've set all of this up, we can (almost) begin programming!
Step 2: Make a RapidAPI Account
In order to search for the song's lyrics, we'll be using the 'Canarado Lyrics API' which is accessible through RapidAPI. If you already have a RapidAPI account, you can skip this step, but if not, continue reading.
Begin by making your RapidAPI account:
- Click the 'Sign Up' Button near the top right-hand corner and sign up with any of the options they have (Google, Github, Facebook or regular sign up).
- Once you've signed up, head on over to the Canarado Lyrics API: https://rapidapi.com/canarado/api/canarado-lyrics
If you see a screen similar to the third image attached above, you've completed this step. We're finally ready to begin programming.
Step 3: Write a Basic Program
As mentioned in the previous step, the Canarado Lyrics API is located at: https://rapidapi.com/canarado/api/canarado-lyrics
Once there, head to the right half of the screen with the header 'Code Snippets'. I'll be using Python in this instructable, but if you prefer another language go ahead and use that instead.
Click on the dropdown menu with the label '(Node.js) Unrest' and navigate to your language of choice. Again, I'll be using Python - specifically Python (Requests) - as this program requires a request to the API's host for most of the information. Copy the code by clicking the 'copy code' button near the top right. Paste this code into your code editor of choice - I'll be using Jupyter's Notebook environment.
Once you've pasted the program, import Python's json module. This will enable us to print out the lyrics. Add the following code near the top (below import requests)
import json
Once you've imported the json module, your program should look like this:
import requests import json url = "https://canarado-lyrics.p.rapidapi.com/lyrics/zenith%2520denzel%2520curry"<br> headers = { 'x-rapidapi-host': "canarado-lyrics.p.rapidapi.com", 'x-rapidapi-key': "YOUR-API-KEY-HERE" } response = requests.request("GET", url, headers=headers) print(response.text)
Ensure that your program looks similar (with a different API Key) and then head over to the 'url' variable. You'll notice that Canarado's default parameter is 'Zenith' by Denzel Curry. You can change that by inputting any song you like. For example, if I wanted to search for lyrics information regarding Michael Jackson's Thriller, I can enter "thriller" in place of "zenith denzel curry", such that the url now looks like:
url = "https://canarado-lyrics.p.rapidapi.com/lyrics/thriller"
You can also add spaces without any hesitation - the browser will automatically add the %2520 characters (You'll notice them in place of spaces in the default url). For example, a search for Gangstas Paradise might look like this:
url = "https://canarado-lyrics.p.rapidapi.com/lyrics/gangstas paradise"
Run the program by substituting the default value "zenith%2520denzel%2520curry" with any song of your choice. You'll notice that the lyrics are printed out, but in a hard to read fashion. What's more - other information, including the song's release date, title and the API's status information obstruct the view of the lyrics. Let's fix this in the next step.
Step 4: Formatting the Lyrics
Formatting the lyrics such that they're readable isn't too hard. Most people with some knowledge of programming will be able to do this pretty easily. Instead of simply printing out "response.text", let's use the .json() method. This will organize the API's information into a dictionary and make everything a lot more accessible.
To begin, create a variable to hold 'response.json()'. I've named my variable 'data'.
data = response.json()
Next, use the following 'for' loop to access the lyrics.
for i in data['content']: print(i['lyrics'])
The above code navigates to the data dictionary's 'content' key. Within the content key, the for loop allows you to navigate to the 'lyrics' key. The code should now look similar to this:
import requests import json url = "https://canarado-lyrics.p.rapidapi.com/lyrics/YOUR-SONG-HERE" headers = { 'x-rapidapi-host': "canarado-lyrics.p.rapidapi.com", 'x-rapidapi-key': "YOUR-API-KEY-HERE" } response = requests.request("GET", url, headers=headers) data = response.json() for i in data['content']: print(i['lyrics'])
Run this program and you'll notice that the lyrics are a lot more readable. In the next step we'll be adding a few extra variables, so that we can use a song's title as an input and so that we can output the lyrics to Alexa.
Step 5: Input's and Output's
So far, we've been typing in the song's title at the end of the URL. It would be nice if we could use a variable from an input and concatenate the two strings (url and song title). Doing that is actually pretty easy.
For the time being, let's use Python's input function. Once we upload the code to Wayscript, we'll be using our speech as an input instead. Let's create a song title variable set to an input that asks for the user to "enter a song title":
song_title = input("Enter a song to find the lyrics for: ")
Then, concatenate this string to the url, using Python's '+' operator for Strings
url = "https://canarado-lyrics.p.rapidapi.com/lyrics/" + song_title
Now, when you run the program, you no longer need to edit the url within the program. Instead, simply reply to the input with your song's title, and the song's lyrics should be printed out.
You're program should now look similar to this:
import requests import json song = input("Enter a song to find the lyrics for: ") url = "https://canarado-lyrics.p.rapidapi.com/lyrics/" + song headers = { 'x-rapidapi-host': "canarado-lyrics.p.rapidapi.com", 'x-rapidapi-key': "YOUR-API-KEY-HERE" } response = requests.request("GET", url, headers=headers) data = response.json() for i in data['content']: print(i['lyrics'])
Finally, let's create a variable to output the lyrics. Start by creating a variable set to an empty string. Then, concatenate 'i['lyrics']' to the end of it. Print out the variable outside of the for loop.
lyrics = "" for i in data['content']: lyrics += i['lyrics'] print(lyrics)
The final program should look like this:
import requests import json song = input("Enter a song to find the lyrics for: ") url = "https://canarado-lyrics.p.rapidapi.com/lyrics/" + song headers = { 'x-rapidapi-host': "canarado-lyrics.p.rapidapi.com", 'x-rapidapi-key': "YOUR-API-KEY-HERE" } response = requests.request("GET", url, headers=headers) data = response.json() lyrics = "" for i in data['content']: lyrics += i['lyrics'] print(lyrics)
The program will print out the exact same thing, but now you have a variable that you can use as an output. Now, we can finally add the Alexa and Email functionality with Wayscript.
Step 6: Adding the Alexa Trigger
In order to add your first trigger, click on the 'Add Trigger' button:
- A search bar will show up - search for 'Alexa' and you'll notice the Alexa trigger. If you haven't used the Alexa trigger prior to this, or if this is your first project, you may have to click 'import'. Once imported, simply click on it, and it should be added.
- Enable the trigger by clicking on the pill-shaped switch. It should become green. On the left-hand side, you'll notice a section that says 'Outputs'. Click on the checkbox that says 'Spoken Input' and enter a value similar to our previous song_title variable. We can now use your spoken input in place of the input statement we had used previously
- Ensure that you have written a default value (your favorite songs perhaps), as without one you may encounter a few errors.
In the next steps, we'll be adding our Python Program, the Send Email method and we'll have Alexa confirm that the method has been complete.
Step 7: Adding Your Python Program
In order to add your program to your current Wayscript flow, click on 'Add Step'
- Search for Python and click on the Python action. This should refresh the trigger and you'll notice a left-hand tab appear.
- Hover over the program on the side panel and you'll notice an 'Edit Code' button pop up. Click on it and a full-screen editor will pop up.
- In the full-screen editor, copy and paste the python program we had created previously.
- Delete the song_title = input("....") variable we had written and add the spoken input variable we created in the previous step. Variables created in the previous steps can be found at the bottom left corner.
- Your new program should look similar to the 6th image attached above.
- Finally, we want to set our lyrics variable as an output. In order to do this, copy and paste the following code at the bottom of your program (either below or in place of the print statement)
variables[ 'Song_Lyrics' ] = lyrics
This creates an output variable called Song_Lyrics and sets it equal to the 'lyrics' variable we had created before. Your final program should look just like the last image attached above. Run the code just once - so that Wayscript knows what is going on and can create your output variables. This may take a few moments, but if it takes too long, you might have to reload the page. Wayscript does contain a few bugs, so the worst-case scenario may entail having to recreate the script.
Step 8: Send the Lyrics and Confirm With Alexa
We have two more steps to add to our WayScript flow - a step that allows you to send an email to yourself and another one that confirms that an email has been sent. Let's begin by creating the email step.
- Click 'Add Step', search for 'Send Email' and locate the left-hand side tab
- Use variables created in the previous steps like 'Song_Name' (or Song_Title) and 'Song_Lyrics'
- You can format the email anyway you wish. For example, I've added the word "Lyrics" after the 'Song_Name' variable and have added the sentence "Here's the lyrics to the song you requested" near the top.
This script will send a message to the email you had registered with. If you would like to send it to a different email, you may have to use the SMTP Email Action instead.
Finally, let's confirm that the email has been sent with Alexa.
- Click 'Add Step' and search for 'Alexa' again. Add the Alexa action.
- I've set it up so that once the email is sent, Alexa says 'Lyrics for [Song_Name] have been sent'
You're finished! Run the program once by clicking the 'Run' button. If there are no errors, you can now use it with Alexa.
Step 9: You're Done!
In order to use it with Alexa, you'll have to use the following commands:
'Alexa, launch Wayscript'
Once Alexa has confirmed that Wayscript has been launched, say:
'Run [Name of Program] with [Song Name]'
For example, my program is called 'Song Lyrics' - If I want Alexa to send me the lyrics to Thriller, I'll say:
'Run Song Lyrics with Thriller'
Check out the video above if you're curious to see how the project should work. Note that the video was edited a little strangely (one of the parallel videos is a little faster than the other), and as a result, the program looks a little faster than it actually is. (It generally takes a few seconds longer to send the email).
If you made it or if you have any suggestions/improvements, let me know in the comments below!