Author Topic: Get Lyrics to songs using Online API!  (Read 6209 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Get Lyrics to songs using Online API!
« on: July 20, 2020, 07:31:36 pm »
Here is some code you can use to get lyrics for your favorite songs from your favorite artists! No API key required! Maybe it would be a good thing to use in an InForm application and display the lyrics in a listbox? Maybe even encode some timing into the resulting JSON from the API call and display lyrics in time with MP3 files that are playing? HMMMM
 
lyricsapi.png

Code: QB64: [Select]
  1.  
  2. PRINT GetLyrics("Newsboys", "Joy")
  3.  
  4. FUNCTION GetLyrics$ (artist AS STRING, title AS STRING)
  5.     DIM URL AS STRING
  6.     DIM URLFile AS STRING
  7.     DIM a%
  8.     DIM Request AS STRING
  9.     URLFile = "lyricsapirequest"
  10.     URL = "https://api.lyrics.ovh/v1/" + FormatAsHTTP(artist) + "/" + FormatAsHTTP(title)
  11.     a% = API_Request(URL, URLFile)
  12.     DIM U AS INTEGER
  13.     U = FREEFILE
  14.     OPEN URLFile FOR BINARY AS #U
  15.     IF LOF(U) <> 0 THEN
  16.         Request = SPACE$(LOF(U))
  17.         GET #U, , Request
  18.     ELSE
  19.         CLOSE #U
  20.         KILL URLFile
  21.         GetLyrics = ""
  22.         EXIT FUNCTION
  23.     END IF
  24.     CLOSE #U
  25.     KILL URLFile
  26.     Request = MID$(String.Replace(Request, "\n", CHR$(10)), 12)
  27.     Request = String.Replace(Request, "\r", CHR$(13))
  28.     Request = String.Replace(Request, "\" + CHR$(34), CHR$(34))
  29.     GetLyrics = MID$(Request, 1, LEN(Request) - 2)
  30.  
  31. FUNCTION FormatAsHTTP$ (Request AS STRING)
  32.     start% = 1
  33.     DO
  34.         position% = INSTR(start%, Request, " ")
  35.         IF position% THEN
  36.             MID$(Request, position%, 1) = "+"
  37.             start% = position% + 1
  38.         END IF
  39.     LOOP UNTIL position% = 0
  40.     FormatAsHTTP = Request
  41.  
  42. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  43.     j = INSTR(a, b)
  44.     IF j > 0 THEN
  45.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  46.     ELSE
  47.         r$ = a
  48.     END IF
  49.     String.Replace = r$
  50.  
  51.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  52.  
  53. FUNCTION API_Request (URL AS STRING, File AS STRING)
  54.     API_Request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 20, 2020, 07:33:43 pm by SpriggsySpriggs »
Shuwatch!

FellippeHeitor

  • Guest
Re: Get Lyrics to songs using Online API!
« Reply #1 on: July 20, 2020, 07:32:00 pm »
Cool!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #2 on: July 20, 2020, 08:15:11 pm »
AWESOME Spriggsy! I use websites like this all the time. If you don't want to use InForm, you can use what I did here. I made it so it creates a .txt file and it has a SHELL command that opens it up with Notepad when it's done. That way you can save it or print it or do whatever you want with it. This will only work with Windows I think.
But what you did is awesome. I was just thinking about something like this yesterday, if we could make a simple text web browser. :)
Make sure and put this in its own folder so you can easily see the text files it makes.

Code: QB64: [Select]
  1. '$CONSOLE:ONLY
  2. '_DEST _CONSOLE
  3. start:
  4. INPUT "Band name:", band$
  5. INPUT "Song name:", nm$
  6.  
  7. filename$ = nm$ + " by " + band$ + ".txt"
  8. PRINT GetLyrics(band$, nm$)
  9. OPEN filename$ FOR OUTPUT AS #1
  10. PRINT #1, GetLyrics(band$, nm$)
  11. SHELL _DONTWAIT filename$
  12.  
  13. INPUT "Another one (Y/N)"; yn$
  14. IF LEFT$(yn$, 1) = "y" OR LEFT$(yn$, 1) = "Y" THEN GOTO start:
  15.  
  16. FUNCTION GetLyrics$ (artist AS STRING, title AS STRING)
  17.     DIM URL AS STRING
  18.     DIM URLFile AS STRING
  19.     DIM a%
  20.     DIM Request AS STRING
  21.     URLFile = "lyricsapirequest"
  22.     URL = "https://api.lyrics.ovh/v1/" + FormatAsHTTP(artist) + "/" + FormatAsHTTP(title)
  23.     a% = API_Request(URL, URLFile)
  24.     DIM U AS INTEGER
  25.     U = FREEFILE
  26.     OPEN URLFile FOR BINARY AS #U
  27.     IF LOF(U) <> 0 THEN
  28.         Request = SPACE$(LOF(U))
  29.         GET #U, , Request
  30.     ELSE
  31.         CLOSE #U
  32.         KILL URLFile
  33.         GetLyrics = ""
  34.         EXIT FUNCTION
  35.     END IF
  36.     CLOSE #U
  37.     KILL URLFile
  38.     Request = MID$(STRING.Replace(Request, "\n", CHR$(10)), 12)
  39.     Request = STRING.Replace(Request, "\r", CHR$(13))
  40.     Request = STRING.Replace(Request, "\" + CHR$(34), CHR$(34))
  41.     GetLyrics = MID$(Request, 1, LEN(Request) - 2)
  42.  
  43. FUNCTION FormatAsHTTP$ (Request AS STRING)
  44.     start% = 1
  45.     DO
  46.         position% = INSTR(start%, Request, " ")
  47.         IF position% THEN
  48.             MID$(Request, position%, 1) = "+"
  49.             start% = position% + 1
  50.         END IF
  51.     LOOP UNTIL position% = 0
  52.     FormatAsHTTP = Request
  53.  
  54. FUNCTION STRING.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  55.     j = INSTR(a, b)
  56.     IF j > 0 THEN
  57.         r$ = LEFT$(a, j - 1) + c + STRING.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  58.     ELSE
  59.         r$ = a
  60.     END IF
  61.     STRING.Replace = r$
  62.  
  63.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  64.  
  65. FUNCTION API_Request (URL AS STRING, File AS STRING)
  66.     API_Request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  67.  
  68.  
« Last Edit: July 20, 2020, 08:20:37 pm by SierraKen »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #3 on: July 20, 2020, 08:55:39 pm »
Neat!  Thanks for sharing.  Have you done a weather type program yet?  I was thinking of doing something like that lately with the url downloader - pull weather data from a website based on zipcode, and then some little images for rain, cloudy, to make a nice display.

- Dav

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Lyrics to songs using Online API!
« Reply #4 on: July 20, 2020, 10:36:06 pm »
Neat!  Thanks for sharing.  Have you done a weather type program yet?  I was thinking of doing something like that lately with the url downloader - pull weather data from a website based on zipcode, and then some little images for rain, cloudy, to make a nice display.
@Dav That's an excellent idea! I'll look into that.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Lyrics to songs using Online API!
« Reply #5 on: July 20, 2020, 10:40:43 pm »
@SierraKen That's pretty neat! I like the idea of opening Notepad after the download.
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #6 on: July 21, 2020, 12:24:11 am »
Thanks! Glad you like it. :)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #7 on: July 21, 2020, 01:34:26 am »
Nice work! @SpriggsySpriggs
It works fine for American songs but it not working for Indian songs...
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Lyrics to songs using Online API!
« Reply #8 on: July 21, 2020, 06:07:17 am »
Nice work! @SpriggsySpriggs
It works fine for American songs but it not working for Indian songs...
I think the website only has user submitted songs. Chances are it won't even find very many American songs unless they're popular.
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #9 on: July 21, 2020, 05:55:14 pm »
I see the website has the first 2 or so stanzas (groups of lines) without empty lines between each line. But the rest of the song does every other line. Would be cool to be able to patch it somehow.
« Last Edit: July 21, 2020, 06:18:19 pm by SierraKen »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Lyrics to songs using Online API!
« Reply #10 on: July 21, 2020, 06:37:22 pm »
@SierraKen I'll look into it. You mean like extra carriage returns, right? I think it is mostly a matter of how Windows VS QB64 interprets new lines.
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #11 on: July 21, 2020, 09:57:55 pm »
Actually it's exactly how it shows on a web browser. But I was just wondering if that can be changed because the first 2 stanzas don't have any blank lines in between the lines. But after that they have one empty line between each line. Except between the stanzas which have a couple blank lines. I don't know if it's possible or not, although maybe an IF/THEN statement with a counter on blank lines would work, somehow. lol

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Lyrics to songs using Online API!
« Reply #12 on: July 21, 2020, 10:19:34 pm »
@SierraKen Here is how I get it now with replacing CHR$(13) + CHR$(10) with CHR$(10)
Is this better?
There are no extra lines in the first two sections

Code: QB64: [Select]
  1. PRINT GetLyrics("Newsboys", "Joy")
  2.  
  3. FUNCTION GetLyrics$ (artist AS STRING, title AS STRING)
  4.     DIM URL AS STRING
  5.     DIM URLFile AS STRING
  6.     DIM a%
  7.     DIM Request AS STRING
  8.     URLFile = "lyricsapirequest"
  9.     URL = "https://api.lyrics.ovh/v1/" + FormatAsHTTP(artist) + "/" + FormatAsHTTP(title)
  10.     a% = API_Request(URL, URLFile)
  11.     DIM U AS INTEGER
  12.     U = FREEFILE
  13.     OPEN URLFile FOR BINARY AS #U
  14.     IF LOF(U) <> 0 THEN
  15.         Request = SPACE$(LOF(U))
  16.         GET #U, , Request
  17.     ELSE
  18.         CLOSE #U
  19.         KILL URLFile
  20.         GetLyrics = ""
  21.         EXIT FUNCTION
  22.     END IF
  23.     CLOSE #U
  24.     KILL URLFile
  25.     Request = String.Replace(Request, CHR$(13) + CHR$(10), CHR$(10))
  26.     Request = MID$(String.Replace(Request, "\n", CHR$(10)), 12)
  27.     Request = String.Replace(Request, "\r", CHR$(13))
  28.     Request = String.Replace(Request, "\" + CHR$(34), CHR$(34))
  29.     GetLyrics = MID$(Request, 1, LEN(Request) - 2)
  30.  
  31. FUNCTION FormatAsHTTP$ (Request AS STRING)
  32.     start% = 1
  33.     DO
  34.         position% = INSTR(start%, Request, " ")
  35.         IF position% THEN
  36.             MID$(Request, position%, 1) = "+"
  37.             start% = position% + 1
  38.         END IF
  39.     LOOP UNTIL position% = 0
  40.     FormatAsHTTP = Request
  41.  
  42. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  43.     j = INSTR(a, b)
  44.     IF j > 0 THEN
  45.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  46.     ELSE
  47.         r$ = a
  48.     END IF
  49.     String.Replace = r$
  50.  
  51.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  52.  
  53. FUNCTION API_Request (URL AS STRING, File AS STRING)
  54.     API_Request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 21, 2020, 10:20:38 pm by SpriggsySpriggs »
Shuwatch!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #13 on: July 22, 2020, 12:49:46 am »
It still does the same thing. I think you might not understand what I'm saying. Here is a picture that helps you understand it better. Just remember though, it's the same way also on my web browser on that website. So in actuality, your program is doing everything perfectly. I was just wondering if you could patch it so it fixes this problem that they originally have on their website, to be seen better on this program. Look at where my arrows are and what I say on this picture of it.



Pic-7-21-2020.jpg
* Pic-7-21-2020.jpg (Filesize: 53.78 KB, Dimensions: 496x335, Views: 153)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get Lyrics to songs using Online API!
« Reply #14 on: July 22, 2020, 12:55:49 am »
Here's another picture, this time of the website and you can notice that your program is just showing exactly what's already there on the website. If you can't patch it or do anything, it's no big deal, I can just use the Enter key and make blank lines in the first 2 stanzas as the rest of the lines are. Sometimes I'm too picky. :)

Pic2-7-21-2020.jpg
* Pic2-7-21-2020.jpg (Filesize: 127.68 KB, Dimensions: 1901x931, Views: 173)