QB64.org Forum

Active Forums => Programs => Topic started by: SpriggsySpriggs on July 20, 2020, 07:31:36 pm

Title: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs 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
 

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)
Title: Re: Get Lyrics to songs using Online API!
Post by: FellippeHeitor on July 20, 2020, 07:32:00 pm
Cool!
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen 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.  
Title: Re: Get Lyrics to songs using Online API!
Post by: Dav 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
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs 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.
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs on July 20, 2020, 10:40:43 pm
@SierraKen That's pretty neat! I like the idea of opening Notepad after the download.
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen on July 21, 2020, 12:24:11 am
Thanks! Glad you like it. :)
Title: Re: Get Lyrics to songs using Online API!
Post by: Ashish on July 21, 2020, 01:34:26 am
Nice work! @SpriggsySpriggs
It works fine for American songs but it not working for Indian songs...
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs 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.
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen 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.
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs 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.
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen 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
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs 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)
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen 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.



Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen 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. :)

Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs on July 22, 2020, 06:55:39 am
@SierraKen , When I run my code I have NO extra lines between anything, even in the output file. Are you sure you ran my latest code?
Strange.... When I run this exact same code at home on my desktop I have no lines between anything except where they should be. Running them on my work laptop I get lines like you described...... I'll have to see why this is happening. That latest code I posted works perfectly on my home PC.
EDIT: Even weirder, I ran the code again and had no lines like my picture shows. Then I ran it again and had the same lines like before. This is some weird goings-on.
EDIT AGAIN: I ran the code back to back several times without annoying lines like you were showing. *shrug*
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs on July 22, 2020, 07:12:11 am
Never mind, it's going to require a few IF ELSE statements. I think the majority of the issue is that the site is inconsistent with usage of \r and \n. I tried one song, it looked perfect. I tried another, there were absolutely no lines in between, even between verses so it looked worse. I'll have to add some conditional statements for sure.
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen on July 22, 2020, 03:16:29 pm
Wild Spriggs. Like I said, their website where the program gets the data does the same thing. So yeah, it would need some IF/THEN statements, somehow.
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs on July 22, 2020, 05:01:15 pm
Oops, sorry. I didn't realize my previous reply was there. Ignore the previous version of this message.
Title: Re: Get Lyrics to songs using Online API!
Post by: SierraKen on July 22, 2020, 05:12:43 pm
Yeah, probably some loose variable on their end or something. Anyway, no big deal. Like I said, we can always use  the Enter key or Back Space on Notepad. :)
Title: Re: Get Lyrics to songs using Online API!
Post by: SpriggsySpriggs on July 22, 2020, 05:55:18 pm
Yeah. Something. I'll look at it in depth this weekend, perhaps. I might find the perfect IF ELSE scenario.